Wow! That did it. A funny thing is that it is not in any of the documentation. What is your guess as to the problem? Is it the module?
| [reply] |
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 | [reply] [d/l] [select] |
Your information is very helpful. Thank you! MysqlPP.pm has very little documentation as it is a new pure-perl module (0.04) to replace mysql.pm. The module does have a DESTROY subroutine. But, its implementation is undocumented. I want to help write the module as my understanding of it grows. The author has not responded to e-mails but I will be persistent.
| [reply] |