On Mar 5, 2005, at 9:29 AM, Steven N. Hirsch wrote:
On Sat, 5 Mar 2005, muppet wrote:
SvREFCNT_dec() is #defined to sv_free(). sv_free() (Perl_sv_free in sv.c)
does the decrement and destroys the SV right then if the refcount is zero.
Ok, so if the SV is actually an reference to an AV, and the latter transitively goes to zero refcnt, is the AV freed? And, if so, are the elements of the AV similarly handled?
If the mortal scalar was the only thing holding a ref to that AV, that AV will be destroyed, yes. Similarly for things pointed to by the AV.
This is why newRV_noinc() is very important.
void some_func () PPCODE: ... PUSHs (sv_2mortal (newRV_noinc ((SV*)av)));
SV* some_func () CODE: ... RETVAL = newRV_noinc ((SV*)av); OUTPUT RETVAL /* RETVAL will be made mortal by the OUTPUT code. */
When making a list-of-list, hash-of-lists, or list-of-hashes in XS code, you need to add the child structures with newRV_noinc() for the same reasons.
See, for example, the gdk_pixbuf_get_formats() xsub (the last thing in http://cvs.sourceforge.net/viewcvs.py/gtk2-perl/gtk2-perl-xs/Gtk2/xs/ GdkPixbuf.xs?rev=1.33&view=auto ), which pushes onto the stack a bunch of hash references containing two keys whose values are arrays ('mime_types' and 'extensions').
I'm confused as to whether I get comprehensive management for "free" in the XS world, or whether the burden is on me to iterate into complex data structures.
If you create your complex structures correctly, yes, it's free.
--
"that's it! you're a genius!" "yes. that's what i think. do you think i deserve a raise?"
- dialogue from 'Godzilla versus Mothra', 1964