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

So I am trying to find and plug memory leaks in SDL perl. Most of the memory leaks occur because in XS a reference is still held. One very expensive mem leak was caught by TELS really early on here. The problem occurs where in XS there is

RETVAL = (cast *) safemalloc( ... );
 and safefree(...) is never called.

Here is a in code example lines 1082 on.

My question is how I can mitigate these mem leaks? One idea I had was to store all malloc'd pointers to an array that is safefree'd on DESTROY or hooked into perl GC somehow .

Any guidance is helpful.
  • Comment on Handing Memory Leaks due to XS holding on to references

Replies are listed 'Best First'.
Re: Handing Memory Leaks due to XS holding on to references
by jettero (Monsignor) on Aug 28, 2009 at 10:38 UTC
    When I'm writing C, I typically have structures like this:
    #ifdef DEBUG_MEM fprintf(memlog, "malloced(%d)\n", pointer); #endif #ifdef DEBUG_MEM fprintf(memlog, "freed(%d)\n", pointer); #endif

    Then I use perl to analyze the logfile. You can't just do it with eyeballs, because sometimes the same memory gets malloc()ed and freed a few times. Also, with XS, it's a little less explained what's happening when which macro is called... but the strategy is sound -- even if it's a small-project kind of strategy.

    I also think, trying to do the demalloc in DESTROY is the wrong approach. It would be better to pull as much of the XS up into perl as possible. If you want automatic, use perl. If you want explicitly-detailed-and-micromanaged, use XS/C.

    -Paul

      valgrind is a tool to detect memory leaks and overflows, and it works with Perl.
Re: Handing Memory Leaks due to XS holding on to references
by ww (Archbishop) on Aug 28, 2009 at 10:34 UTC
    Here's guidance re posting: Cut the "1082" lines down to the smallest snippet that illustrates the problem. See "2." in On asking for help
Re: Handing Memory Leaks due to XS holding on to references
by ikegami (Patriarch) on Aug 28, 2009 at 14:23 UTC
    You need to create a Perl-side destructor which frees the memory.