Hi,
 
I have a OOP problem. I wrote a script that forks several child
processes that in turn create instances of the same class:
 
    for (my $i = 0; $i <= 10; $i++) {
        defined(my $pid = fork()) or die "Unable to fork: $!";
 
        if ($pid) { # parent process
            next;
        } else {    # child process
            setsid() or die "Unable to start a new instance: $!";

            my $instance = MyModules::MyClass->new();
 
            $instance->start();
            last;
        }
    }

 
In MyModules/MyClass.pm there is a DESTROY method for taking some
houskeeping actions (closing database handle and deleting PID file):
 
    sub DESTROY {
        my $self      = shift;
        my $exit_code = $? || 0; # use exit code from previous error
exit call
 
        # close DB connection
        $self->get_attribute("db_handle")->disconnect() if
($self->get_attribute("db_handle"));
 
        # remove PID file and exit unless PID error
        remove_pid_file($self->get_attribute("pid_file")) || exit(1);
 
        exit($exit_code);
    }
 
When running the script, everything works fine except for each
instance's error message reading "Unable to remove <PID file name>: No
such file or directory". By examing the log file and debugging the
script I found that the DESTROY method is executed twice. So the PID
file is deleted the first time the destructor is invoked, the second
time the deletion fails, of course, and results in the error message.
 
I read that the DESTROY method might be executed more than once if one
re-blesses the object in the destructor. But to my mind, this is not the
case in my script.
By the way, it works fine with Perl 5.6.1 on Solaris 8 for SPARC, the
problem occurs with 5.8.8 on Solaris 10 for x86.
 
Any help is appreciated.
 
Best regards,
 
Danilo
 
PS: The problem can be solved by making the objects remember if they
have been destroyed before and doing housekeeping only in case they have
not, though. However, I would like to find out about the root cause.

Reply via email to