weismat has asked for the wisdom of the Perl Monks concerning the following question:

Is it a feature or a bug if a deconstructor is not called when a program is stopped via a signal. e.g. my small test program looks like this. Class A implements a destructor.
my $a=A->new(); while() { sleep 1; } exit;
If I call this program in the debugger, I see that the destructor gets called. If I use it interactively with Solaris 10 and Perl 5.8 and terminate the program with Ctrl+C or a signal, then the constructor is not called.
I have seen this issue with single and also multi-threaded applications.
What do you suggestion to ensure that the destructor gets called. I need to call the destructor as I am using remote sources which seem to have an issue if the destructor does not get called.

Replies are listed 'Best First'.
Re: Destructors when the program is stopped via a signal
by ikegami (Patriarch) on Mar 18, 2008 at 06:21 UTC

    I'm not too familiar with the mechanics of signals, but I think that
    $SIG{INT} = sub { die "SIGINT\n" };
    and
    $SIG{INT} = sub { print STDERR "Exiting due to SIGINT\n"; exit(1) };
    would cause Perl to exit while calling destructors.

      This works fine for the main thread, but it does not call the destructor for objects in the detached child thread.
      Do you have a suggestion how to trigger this?
      I am usually using a detached thread pool where every child thread contains the one instance object.
        Just like you'd do if you wanted to exit the main program for any other reason. Send some kind of message to the threads that they need to terminate, and wait for them to terminate.
Re: Destructors when the program is stopped via a signal
by pc88mxer (Vicar) on Mar 18, 2008 at 07:55 UTC
    What ikegami suggests will work. Simply installing a signal handler for INT will allow perl to call destructors before exiting. Otherwise, the default action for the INT signal is to terminate the program (abruptly).

    Note that the KILL signal cannot be caught, so that will always result in the abrupt termination of the program.