Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: sub DESTROY: Strange ordering of object destruction

by ikegami (Patriarch)
on Mar 03, 2011 at 09:51 UTC ( [id://891182]=note: print w/replies, xml ) Need Help??


in reply to sub DESTROY: Strange ordering of object destruction (SOLVED)

This happens both on global destruction (exit) and controlled explicit destruction, i.e. undef $A.

Saying you used undef $A doesn't tell us anything about when the referenced object was destroyed.

You should only get the stated behaviour during global destruction, and I find it hard to believe it has occurred at any other time.

Without a demonstration, we can't really give anything but coulds and shoulds.

Replies are listed 'Best First'.
Re^2: sub DESTROY: Strange ordering of object destruction
by ELISHEVA (Prior) on Mar 03, 2011 at 11:45 UTC

    ikegami - you are spot on here. To get a better understanding of the actual timing of the call to DESTROY, I used your suggestion (in the cb) of adding the following END block:

    END { warn "Global Destruction starting"; }

    Sure enough, the destruction is happening during global destruction, despite the undef $A much earlier. As clarified in the CB, during global destruction objects are destroyed in any order, so one should never assume that just because A holds a reference to B that A a will be destroyed before B.

    To be honest, this still surprises me. Once I called undef $A there were no more variables storing a reference to A. i.e. the code was essentially nothing more than

    sub { my $a=SomeClass->new($arg1, $arg2, ...); undef $a; exit(); }

    Wouldn't that undef result in the reference count for whatever was created by SomeClass->new going down to 0? If not, why not? There are no circular refrences in SomeClass - it stores only a reference to B and some pure scalars (strings, numbers). B is a third party class that has no knowledge of A and no way to pass in a reference to A, so a circular reference A->B->A isn't possible.

    If yes, in the CB you said that destruction happens as soon as the reference count goes to 0. However, destruction is still happening only after the END block executes. Why?

    In any case, thanks, again, for the END block suggestion in the CB.

    Update: added comment about circular references.

      Maybe the object itself keeps itself alive? Maybe it has child objects that keep the parent alive too?

      My guess is that your object SomeClass will never get released until global destruction.

      My advice is to rip out code from SomeClass until it starts getting released at the appropriate time, and then slowly putting the code (and the SomeClass-internal calls to the code) back in until the leak reappears.

      going down to 0? If not, why not?

      Yes it would, but only as long as nothing else held a reference to $a, including $a itself

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://891182]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-19 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found