As I understand it, object destruction happens in garbage collection, which is not guaranteed to happen at any particular time (before process termination). I must not count on actions in the destructor happening promptly when the last reference to an object goes away.

I've got a class that indexes members as they're created, and I need to keep that index clean because due to outside factors I don't control the same keys may come back fairly soon (outside forces prevent actual conflicts, but they do allow the same key to come up in close proximity in time).

So, I need to be sure that when I get rid of an object of this class, the cleanup happens immediately (before the next time I poll the outside forces for new work, which might give me new work with the same ID).

First, I put the cleanup in the destructor and just expected it to run. Then I realized that wasn't safe (due to the uncertain time of actual destruction), and changed to calling the destructor manually (I believe that's fairly safe, since this is a class used locally in a small controller program and I actually know when the objects aren't needed any more).

Then, after more thought, I added an instance method to take the current object out of the indexes, and just called that. (I also call it in the destructor; and because of that, it's become slightly more complicated and must handle the case where the object isn't currently in the indexes.)

(This maintaining an index is probably a recognized design pattern, but I'm not finding its name on a quick search. Sometimes factories do it, but this isn't a factory.)

So, just how undesirable is it to manually call object destructors? Is my current solution reasonably respectable? Or should I be using some completely different pattern, not trying to have the class index the class members for me or something? (Somebody has to index them.)


In reply to Timing of garbage collection by dd-b

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.