XP is just a number | |
PerlMonks |
Re: Inline::C self-referential struct idioms and memoryby syphilis (Archbishop) |
on Sep 08, 2015 at 02:38 UTC ( [id://1141297]=note: print w/replies, xml ) | Need Help?? |
There are memory management issues here. Why does this function return "self" ? IOW, why is it not written as: When it returns "self", it apparently decrements the reference count of "self". Here's a supporting demo: Which outputs: I note that the return of set_parent() is not being captured, which reinforces my view that it could be rewritten as I've suggested. But if it really does need to return "self" && the refcnt is not to be altered (as the comment specifies), then I think it's necessary to increment the refcnt of "self" before returning. Either way worked fine for me. However, for the same reason, I had to rewrite get_parent() as: After that, it all seems to work fine for me (using your script and your typemap): One other thing - I changed destroy_node() to: That didn't blow anything up, from which I deduce that self->parent does need to be Safefree'd in order to avoid memory leaks. OTOH, attempting to Safefree(self->sv) did crash the script - and I therefore deduce that doing so is not the right thing ;-) Update: Whilst the above holds for my perls 5.10.0, 5.12.0, 5.14.0, 5.16.0, 5.18.0 and 5.20.0, on perl-5.22.0 the Safefree(self->parent) leads to a crash for me (on cleanup after the 2 calls to destroy_node have successfully completed) - so there's still a bit more to be unravelled. Cheers, Rob
In Section
Seekers of Perl Wisdom
|
|