When you have a complex data structure, the various parts of it normally get destroyed in a useful order because each object holds a reference to the things it contains: when you free up the top level object, its reference count goes to zero, which triggers its destruction, and that then releases all the things it refers to, reducing their reference counts, and allowing the destruction to cascade down from the top level object.

When the program terminates, there is an additional process called "global destruction", and now it is a bit trickier: perl has no intrinsic information about which things are "top level" objects and which aren't, and so it goes around dropping reference counts on things in an effectively random order. That can mean that if you have a class like this:

package MyClass; sub new { my $class = shift; bless { socket => $class->open_new_socket }, $class; } sub DESTROY { my $self = shift; $self->{socket}->close; }
.. if the DESTROY is triggered during the global destruction phase it is quite possible that perl will already have freed $self->{socket} before freeing $self.

I believe that DBD::mysqlPP has a DESTROY method very similar to the above to try and guarantee that its socket to the MySQL server is always cleanly closed down when the dbh goes away. There are various answers to "where is the problem", but I think I'd vote for the documentation (if this isn't mentioned there) or your reading of it (if it is). :)

For more complex code, it can be helpful to keep the cleanup code close to the creation code, and you can do that by taking advantage of the fact that END handlers are run before global destruction:

my $dbh = DBI->connect(...); END { $dbh = undef }

Hugo

In reply to Re: Re: Re: mysqlPP.pm error by hv
in thread mysqlPP.pm error by terrencebrown

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.