Do you know where your variables are? | |
PerlMonks |
Re^3: OO Inline::C - returning $self not workingby sfink (Deacon) |
on Mar 08, 2006 at 17:16 UTC ( [id://535216]=note: print w/replies, xml ) | Need Help?? |
Oh, right. That happened to me when I was playing with your example, too. It's because you don't initialize anything, so me->parent starts out as a garbage pointer, so dereferencing it with SvREFCNT_dec() seg faults. Fix it by using calloc() instead of malloc().
Here's my version of your code:
demerphq: parent is declared as a Node*, but only actually used as an SV*. Thinking about it, it might work better to leave the definition of the parent field as you originally had it, but extract the actual parent Node* out of the passed-in SV* before storing it. Then set_parent() can be reduced to: (and correspondingly, get_parent() would have to wrap up the fetched parent in an SV.) This approach has one major problem, though -- you can no longer use DESTROY to free up the memory for the node, because there is no longer a single SV* that owns the memory to the node. So you'd either need to manage the memory externally, keep your own ref counts in the Node structure, or something. Hmm. So maybe polluting your structure with SV*'s really is the simplest way to deal with it. When I've been doing this stuff, I haven't used the DESTROY trick much. Mostly, I'm providing a temporary view into data structures that are managed by my main application. I could easily have many SV's that all point to the same object, and it doesn't matter. I guess I'm doing more of embedding Perl within my app than extending Perl with functionality from my app? To the script writer, it looks about the same.
In Section
Seekers of Perl Wisdom
|
|