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:

use strict; use warnings; my $parent = Node->new('Parent'); my $child = Node->new('Child'); # Should print 'Parent' print $child->set_parent($parent)->get_parent->get_name, "\n"; $child->set_name('New name'); # Should print 'New name' print $child->get_name, "\n"; # At end, should see # Freeing 0xnnnnnnn (New name) # Freeing 0xnnnnnnn (Parent) ################################# package Node; use Inline Config => CLEAN_AFTER_BUILD => 0, PRINT_INFO => 1; use Inline C => q{ typedef struct { char* name; double branch_length; SV* parent; struct Node* previous_sister; struct Node* next_sister; struct Node* first_daughter; struct Node* last_daughter; } Node; SV* new(char* class, char* name) { Node* node = calloc(sizeof(Node), 1); SV* obj_ref = newSViv(0); SV* obj = newSVrv(obj_ref, class); node->name = strdup(name); sv_setiv(obj, (IV)node); SvREADONLY_on(obj); return obj_ref; } SV* set_name(SV* self, char* name) { ((Node*)SvIV(SvRV(self)))->name = strdup(name); SvREFCNT_inc(self); return self; } char* get_name(SV* self) { return ((Node*)SvIV(SvRV(self)))->name; } SV* set_parent(SV* self, SV* parent) { Node* me = (Node*)SvIV(SvRV(self)); SvREFCNT_inc(parent); if (me->parent) SvREFCNT_dec(me->parent); me->parent = parent; SvREFCNT_inc(self); return self; } SV* get_parent(SV* self) { SV* parent = ((Node*)SvIV(SvRV(self)))->parent; SvREFCNT_inc(parent); return parent; } void DESTROY(SV* self) { Node* node = (Node*)SvIV(SvRV(self)); fprintf(stderr, "Freeing %p (%s)\n", self, node->name) +; free(node->name); free(node); } };

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:

SV* set_parent(SV* self, SV* parent) { Node* parent_node = (Node*)SvIV(SvRV(parent)); ((Node*)SvIV(SvRV(self)))->parent = parent_node; SvREFCNT_inc(self); return self; }
(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 reply to Re^3: OO Inline::C - returning $self not working by sfink
in thread OO Inline::C - returning $self not working by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.