----- Original Message -----
From: "Sam Tregar" <[EMAIL PROTECTED]>

> Danger, Will Robinson!  You just freed a pointer and then tried to use it
> in a function call.  Instead, you need to do:
>
>   SV *ret;
>
>   ret = newSVpvn((char *)array, i);
>   SafeFree(array);
>   return ret;
>

Indeed, I was way off base initially, however, that syntax still has a bit
of memory leak for me, so what I've done is the following:

void somefunc(short something, int size, SV* variable) {

    int i;

    short *output;

    Newz(0, output, size, short);

    for (i = 0; i <= size; i++) {
        /* fill array with something */
        }

    sv_setpvn(variable, output, i * sizeof(short));

    Safefree(output);
}

    It seemed that no matter what I did with a return value, I either ran
into a problem of a memory leak or at some random point I would get a memory
not 'read' error, so I ended up using sv_setpvn() to set the variable in
place.  Seems more efficient overall --  Considering that it's run like
this:

        # 300,000 calls

    foreach (1..600) {
        my $xdat;
        foreach (1..500) {
            my $cur_dat;
            somefunc1(...., $cur_dat);
            somefunc2(....,$xdat);
            }
        $outdat .= $xdat;
        }

    Where any extra memory left hanging around quickly shows up in the
system monitor.

Also, I've found that if you cast the aray of shorts 'output' as (char *)
before setting its value or returning its value, (from previous posts)
you'll blow up the results, that is, there'll be a mismatch between what you
had in the C function and what you get back, aparantly perl handles strings
of shorts just fine.

That is:

sv_setpvn(variable,newSVpvn((char *)output, i * sizeof(short)), i *
sizeof(short));

will not return the data properly, all of the 16bit numeric values will be
converted to 8bit values.

Thanks for the info and the pointers!

!c

C. Church
http://www.digitalKOMA.com/church/
http://www.DroneColony.com/


Reply via email to