roboticus:

Thanks for the explanation. As a test, I augmented the example I gave above with an equivalent function in C. (This was my first experience using Inline::C. To call the Perl sub from within C, I copied the example in the section “Calling Perl from C” of the Inline::C-Cookbook.)

#! perl use strict; use warnings; $| = 1; use Inline C => <<'END_C'; void baz(SV* foo_ref) { printf("Begin baz()\n"); fflush(stdin); dSP; ENTER; SAVETMPS; XPUSHs(sv_2mortal(newSVpvf("Plus an extra line"))); PUTBACK; call_pv("destruct", G_DISCARD); printf("Before FREETMPS\n"); fflush(stdin); FREETMPS; LEAVE; printf("-End- baz()\n"); fflush(stdin); } END_C { package Foo; sub new { my ($class, $num) = @_; my $self = { id => $num, }; return bless $self, $class; } sub DESTROY { my ($self) = @_; print 'Foo::DESTROY(', $self->{id}, ")\n"; } } my $foo1 = Foo->new('first'); my $foo2 = Foo->new('second'); bar($foo1); print "Back in main (1)\n"; baz($foo2); print "Back in main (2)\n"; sub bar { print "Begin bar()\n"; destruct($_[0]); print "-End- bar()\n"; } sub destruct { undef $_[0]; }

Output:

22:55 >perl 552c_SoPW.pl Begin bar() Foo::DESTROY(first) -End- bar() Back in main (1) Begin baz() Foo::DESTROY(second) Before FREETMPS -End- baz() Back in main (2) 22:56 >

From which it seems that the object is destroyed — meaning its reference count has fallen to zero — before the FREETMPS macro is reached. In which case the addition of the object to the sub’s call stack has not caused its reference count to be incremented? (Or am I simply misunderstanding here?)

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^7: when is destroy function called by Athanasius
in thread when is destroy function called by david2008

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.