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:

When the last reference to an object goes away, the object is automatically destroyed.

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.

Solution

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 468275's observation that "out of scope" is not necessarily synonymous with ref count = 0


In reply to 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.