Object destruction order is not respected anymore once Global Destruction sets in.
Global destruction happens after your main script "falls off the end" (or calls exit) and all END blocks have executed.
A way I've used to better localize the memory cycle that keeps objects alive until Global Destruction is to explicitly release the "master object" at the last (executed) line of the program:
undef $object;
If that works and everything gets released properly, this means you have a simple cycle and $object is kept alive somehow. There are some cases of closures that keep your lexical
variables alive beyond the main program. One especially nasty case (for me) is:
my $object;
sub frobnitz {
$object->do_frobnitz;
};
print "Done";
... but also, creating and handing around other closures might create problems, especially if these closures are kept alive somewhere:
sub make_frobnitz {
my $frobnitz;
my $obj = $_[0]; # $object
$frobnitz = sub {
$obj->frobnitz;
};
return $frobnitz # $frobnitz might stay alive forever, keeping $ob
+j alive
};
Careful use of Scalar::Util::weaken or careful undeffing of $object within (one-shot) closures might help to locate and eliminate the problem.
If all else fails, defensive programming in the destructors also helps (taken from MozRepl::RemoteObject, which has this problem):
sub DESTROY {
my $self = shift;
local $@;
...
my $id = $self->__id();
return unless $self->__id();
my $release_action;
if ($release_action = ($self->__release_action || '')) {
...
};
if ($self->bridge) { # not always there during global destruction
my $rn = $self->bridge->name;
if ($rn) { # not always there during global destruction
...
} else {
# warn "Repl '$rn' has gone away already";
};
1
} else {
if ($MozRepl::RemoteObject::WARN_ON_LEAKS) {
warn "Can't release JS part of object $self / $id ($releas
+e_action)";
};
};
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.