|laziness, impatience, and hubris|
sub DESTROY: Strange ordering of object destruction (SOLVED)by ELISHEVA (Prior)
|on Mar 03, 2011 at 09:10 UTC||Need Help??|
ELISHEVA has asked for the wisdom of the Perl Monks concerning the following question:
I have a wierd situation where A holds a reference to B, but B seems to be destroyed before A. I've ruled out the most obvious causes:
According to the perldocs (perlobj), an object is destroyed when the last reference to it is removed:
I would take that to mean that if object A is the sole holder of a reference to object B and A goes out of scope [ and has no package or in scope my variables holding a reference, i.e. refcount=0 ] then Perl would walk the object graph, first calling the destructor to A and then calling the destructors for each reference held solely by A. B cannot be destroyed before A because as long as A exists there is at least one reference to B.
This happens both on global destruction (exit) and controlled explicit destruction, i.e. undef $A.
In short, I'm stumped.
The platform is Perl 5.10.0 running on Debian Lenny. The Perl is threaded, but there is no explicit use of threads, so presumably A and B are on the same thread.
Many thanks to the monks who helped me better test what was really happening and who spurred my problem solving thinking in new directions.
Update 1: added info about threading:
Update 2: Added solution
Update 3: clarified "goes out of scope" to response to (anonymized user)'s observation that "out of scope" is not necessarily synonymous with ref count = 0