// should NOT alter refcount Node* set_parent(Node* self, Node* parent) { self->parent = parent; return self; } #### void set_parent(Node* self, Node* parent) { self->parent = parent; } #### #!perl -l use warnings; no warnings 'once'; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'EOC'; SV * foo(SV * in) { return in; } SV * get_ref(SV * in) { return newSVuv(SvREFCNT(in)); } EOC $x = 42; # $x refcnt == 1 %h = (fu => \$x); # $x refcnt == 2 print "x refcnt: ", get_ref($x); foo($x); # decrement $x refcnt print "x refcnt: ", get_ref($x); $y = foo($x); # decrement $x refcnt print "x refcnt: ", get_ref($x); print "y refcnt: ", get_ref($y); print "x: $x"; print "y: $y"; #### x refcnt: 2 x refcnt: 1 x refcnt: 0 y refcnt: 1 Use of uninitialized value $x in concatenation (.) or string at try.pl line 31. x: y: 42 #### Node* get_parent(Node* self) { SvREFCNT_inc(self->parent); return self->parent; } #### NODE: SV = IV(0x2c22740) at 0x2c22744 REFCNT = 1 FLAGS = (PADMY,ROK) RV = 0x45812c SV = PVMG(0x2a15cac) at 0x45812c REFCNT = 1 FLAGS = (OBJECT,IOK,READONLY,pIOK) IV = 40118476 NV = 0 PV = 0 STASH = 0x1d0edb4 "Node" --- PARENT: SV = IV(0x2c227f0) at 0x2c227f4 REFCNT = 1 FLAGS = (PADMY,ROK) RV = 0x45807c SV = PVMG(0x2a155ac) at 0x45807c REFCNT = 1 FLAGS = (OBJECT,IOK,READONLY,pIOK) IV = 40119340 NV = 0 PV = 0 STASH = 0x1d0edb4 "Node" --- FROM FIELD: SV = NULL(0x1d0e7) at 0x45817d REFCNT = -16777216 FLAGS = (TEMP,BREAK,OVERLOAD,EVALED,IsUV) --- destroying Node=SCALAR(0x45807c) at rut.pl line 58. destroying Node=SCALAR(0x45812c) at rut.pl line 58. #### void destroy_node(Node* self) { Safefree(self->parent); Safefree(self); }