I have perl embedded in a C++ program. Various C++ objects are wrapped up with XS interfaces. The objects live on the C++ side of things; the perl scripting bits just need to talk to the C++ objects for small time slices. Perl functions get called with the relevant objects as arguments. This all works fine.

My problem is how to invalidate (set to undef) all scalar references to a C++ object when the object is destroyed on the C++ side. $some_cpp_obj->do_it; will crash with memory access violations if the relevant object has been destroyed. I want the C++ destructor to reach into the perl interpreter and set $some_cpp_obj to undef. Any copies of $some_cpp_obj also need to be undefined.

I guess conceptually I want something like *sv = PL_sv_undef, so all SvRefs to that piece of memory now point to undef. Except I think that would crash the application. My best guess is I actually need to scan the whole interpreter for SvRefs for the now invalid pointer.

sub some_event { my $cpp_obj = shift; # It's an arbitrary time later. We have no g +uaranty # the object is still around. return unless $cpp_obj; # I want this to work: if the underlying C +++ object # is gone $cpp_obj should return undef in scalar context. # Do stuff that will blow up if $cpp_obj is gone. # ... } sub i_get_called_from_cpp_code { my ($cpp_obj, $time) = @_; my $event = sub { some_event($cpp_obj) }; Scheduler::new_event($time + 10, $event); }

I guess another way to represent the problem in pure perl is thusly.

package Whatever; sub new { my $self = 0; bless \$self } # $self is Whatever=SCALAR(0x97 +42b30) sub blah { my $self = shift; say $self } package main; my $a = Whatever->new; my $b = $a; # I want to kill $a such that $b is now also undef. How? $a->blah; $$a = undef; # I really want to undefine ALL references to Whatever=SC +ALAR(0x9742b30) $b->blah; # This should die.

In reply to perl embedded in C++: how to undefine perl objects that are blessed references to C++ objects when the C++ object destructs by kingkongrevenge

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.