On Jan 8, 2007, at 10:16 AM, Alberto Simões wrote:

   array = newAV();
   av_push(array, newSVuv(dictionary_get_occ(D, wid)));

   for (j = 0; j < MAXENTRY; j++) {
            twid = 0;
            prob = 0.0;
            twid = dictionary_get_id(D, wid, j);
            if (twid) {
                prob = dictionary_get_val(D, wid, j);
                # tword can't be freed...
                tword = word_list_get_by_id(T, twid);
                if (!tword) { fprintf(stderr, "Id: %d\n", twid); }
                av_push(array, newSVpvn(tword, strlen(tword)));
                av_push(array, newSVnv((double)prob));
            }
        }

        RETVAL = newRV_noinc((SV*)array);
    OUTPUT:
        RETVAL

Your handling of the perl array looks correct. You create a new AV with a refcount of one. You add a bunch of scalars to the array, each of which is created with a refcount of 1. Crucially, you handle the arrayref correctly, creating a single reference which does not increase the refcount of the AV. This one arrayref ends up on the stack, mortalized via the a call to sv_2mortal from the OUTPUT/RETVAL apparatus. Assuming for the sake of simplicity that we don't copy anything out of @_, when that mortalized ref comes due, your array and all of its members should fall like dominoes.

I think you'll need to look elsewhere. Coincidentally, I'm debugging memory leaks right this moment using valgrind. If you have access to a Linux system, I'd strongly recommend you give it a try.

Marvin Humphrey
Rectangular Research
http://www.rectangular.com/


Reply via email to