kopolov has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need help with suspected memory leak in this XS code:

u32 findEAsForGivenRAAndContext(u64 raAddress, u64 lpid, u64 pid, AV* eaAr +ray) PREINIT: u32 resultCount=0; int i=0; int maxCount=10*MAX_NUM_OF_ALIASES; unsigned long long int array[maxCount]; CODE: RETVAL = findEAsForGivenRAAndContext(raAddress, lpid, pid, arr +ay); resultCount = RETVAL; for(i=0; i<resultCount; i++) { SV * sv = newSVnv(array[i]); av_push(eaArray, sv); } OUTPUT: RETVAL

(I'm not sure this is the root cause of my problem, but I have a strong feeling about it)

update - no memory leaks, problem was found elsewhere

Replies are listed 'Best First'.
Re: perl XS - suspected memory leak
by syphilis (Archbishop) on Apr 04, 2016 at 15:22 UTC
    If you do something like:
    for(1.. $large_integer) { findEAsForGivenRAAndContext($arg1, $arg2, $arg3, \@eaArray); }
    then @eaArray is going to get bigger and bigger, consuming more and more memory.
    I suppose this could perhaps be misinterpreted as a memory leak ... but it's not.

    *I* can't see anything else. (But I don't really understand this form of XS code.)

    Cheers,
    Rob
Re: perl XS - suspected memory leak
by ikegami (Patriarch) on Apr 04, 2016 at 21:29 UTC

    av_push calls av_store, which takes ownership of a refcount, so no, no memory leak.

    You can confirm this using use Devel::Peek; Dump($eaArray[0]);. It should have a REFCNT of 1.

Re: perl XS - suspected memory leak
by Anonymous Monk on Apr 04, 2016 at 16:02 UTC
    newSVnv takes an NV (double), not an unsigned long long int. Maybe that's the cause of your feelings? Anyway, read the documentation of Devel::Peek and use it, the REFCNT should help you in debugging memory leaks.
      newSVnv takes an NV (double), not an unsigned long long int.
      Oops, disregard that. I checked and it's a function, not a macro.