c - XS Memory leak in this code? -
unable find memory leak happening in code.
basically want write xs wrapper c-function returns two-dimensional array.
c-function:
int cw_returnarray(double** arraydouble, int* count) { int number = 10; int index, index1; for(index = 0; index < number; index++) { for(index1 = 0; index1 < 10000; index1++) { arraydouble[index][index1] = 12.51; } count[index] = 10000; } return number; } array -> output param hold 2 dimensional array count -> output param hold number of element in each 1d array
xs wrapper:
void returnarray() ppcode: { /** variable declaration **/ double** array; int = 0, j=0, status; int* count; int totalarrays; sv** svarrays; // hold references of 1d arrays sv** svtemparray; // temporary array hold elements of 1d array /** allocate memory c-type variables **/ new(0, array, 10, double*); for(i = 0; i<10;i++) { new(0, array[i], 10000, double); } new(0, count, 10, int); /** call c function **/ status = cw_returnarray(array, count); /** check status , retrieve array store in stack **/ if(status > 0) { totalarrays = status; new(0, svarrays, totalarrays, sv*); for(i = 0; i<totalarrays; i++) { /** allocate memory temporary sv array **/ new(0, svtemparray, count[i], sv*); for(j = 0; j<count[i]; j++) { svtemparray[j] = newsvnv(array[i][j]); } /** make array (av) out of temporary sv array , store reference in svarrays **/ svarrays[i] = newrv_noinc((sv*) av_make(count[i], svtemparray)); /** free memory allocated temp sv array **/ for(j = 0; j<count[i]; j++) { sv_free(svtemparray[j]); } safefree(svtemparray); svtemparray = null; } } else { totalarrays = 0; } /** push return values stack **/ extend(sp, 2); pushs(sv_2mortal(newsviv(status))); pushs(sv_2mortal(newrv_noinc((sv*) av_make(totalarrays, svarrays)))); /** clean allocated memory sv "array of array" , if needed **/ if(totalarrays > 0) { safefree(svarrays); svarrays = null; } /** clean allocated memory c-type variables **/ for(i = 0; i<10;i++) { safefree(array[i]); } safefree(array); array = null; safefree(count); count = null; }
an "array of array" returned xs.
testing in perl script:
for(1..100) { ($status, $arrayref) = returnarray(); undef $status; $arrayref = []; system('pause'); }
every time function returnarray()
called, commit size of perl process increasing. expect $arrayref
variable should garbage collected every time , memory usage should not increase.
i hope, freeing allocated memory in xs. still there memory leak. wrong xs code memory leak?
well, pattern of "create template array, av_make()
, free template" not -- you'd better creating array newav()
, av_extend()
ing right size, , doing av_store(newsvnv(...))
each element. lets avoid intermediate svtemparray
allocations entirely.
however, that's not asked about. think problem safefree(svarrays)
without first sv_free()
ing each element. since av_make()
duplicates contents of source array, afaict you're leaking reference created by
svarrays[i] = newrv_noinc((sv*) av_make(count[i], svtemparray));
you'll need iterate on svarrays , call sv_free()
on each element before safefree(svarrays)
.
Comments
Post a Comment