Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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)"; }; }; }

In reply to Re: sub DESTROY: Strange ordering of object destruction by Corion
in thread sub DESTROY: Strange ordering of object destruction (SOLVED) by ELISHEVA

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-28 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found