kingkongrevenge has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|