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":



  • 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.