Steven N. Hirsch <[EMAIL PROTECTED]> writes: >On Wed, 23 Feb 2005, William Ahern wrote: > >> Has anyone run into the issue of av_undef not doing what is advertized in >> perlguts, namely freeing the object?
That isn't what it does. av_undef is like @array = (); i.e. it frees the contents (REFCNTs permitting) but not the AV itself. >I realize the solution is just to >> decrement the ref count like anything else, but in uncovering my memory leak >> I found a lot of other people were bit by this. Has the man page ever been >> updated? > >I have spent an enormous amount of time chasing memory leaks in my XS >code, but I'm not sure it's the same mechanism. One application in >particular parses a text file using flex/bison and sequentially builds >complex perl data structures to pass into registered perl callbacks. > >For reasons I've never been able to figure out, this memory never gets >freed; even though no references are taken on the perl side and all >structures are recursively undefined in C. Building "complex perl data structures" needs care with REFCNTs. It is particulary important to use newRV_noinc() for references that only exist to be stored in AVs or HVs as scalars. And of course _something_ has to SvREFCNT_dec the top level data structure when you are done with it. This is usually done by making it "mortal". > >Documentation claims that the storage is freed when flow of control >"leaves the current context", but I wonder what that means in concrete >terms. The execution of a LEAVE macro, or rather the FREETMPS that normaly goes with it. >Does a return from an XS call count as "leaving the context"? No. You can set up a context in XS but XS call/return does not create one by default. This allows XS code to return "mortals" to its caller. ENTER; // start a context SAVETMPS; // Note top of "mortal" stack SV * foo = newSVxx(); // REFCNT = 1 sv_2mortal(foo); // REFCNT = 1, but pushed to cleanup stack .... FREETMPS; // mortals e.g. 'foo' SvREFCNT_dec'ed LEAVE; // end a context Unless XS does that mortals live to next perl-side end of context typically end of statement - the ';'. > >This one does hit close to home.. > >Steve