develooper Front page | perl.perl5.porters | Postings from March 2017

[perl #32714] Objects destroyed in the wrong order during globaldestruction

Thread Previous | Thread Next
Nick Wellnhofer via RT
March 11, 2017 22:21
[perl #32714] Objects destroyed in the wrong order during globaldestruction
Message ID:
On Fri, 10 Mar 2017 02:16:13 -0800, LeonT wrote:
> Destructors can and do depend on package variables, this would not solve
> all non-circular cases.

Right, but it would solve a large class of cases. Also note that Perl seems to do the right thing for "my" variables with package scope. Only for "our" variables, it seems impossible to get a sane destruction order. If you run the program below, $my1 if always destroyed before $my2, regardless of initialization order. But "our" variables are always destroyed in the DESTRUCT phase without taking references between objects into account.

> The proper solution would be to topologically sort the objects and then
> destroy them in that order (ideally even taking into account that the
> destruction can add new nodes to the graph), but this is non-trivial to
> implement and to execute; and obviously would still not solve circular
> references.

Just because a solution isn't perfect doesn't mean that it shouldn't be implemented. Anyway, I'm only asking to acknowledge that this bug isn't fixed and to reopen it.

use strict;
use warnings;

package TestObj;

sub new {
    my ( $class, $name ) = @_;
    return bless { name => $name }, $class;

sub set_ref {
    my ( $self, $ref ) = @_;
    $self->{ref} = $ref;

    my $self = shift;
    print "Destroying $self->{name}, phase is ${^GLOBAL_PHASE}\n";

my $my1 = TestObj->new('my1');
my $my2 = TestObj->new('my2');

our $our1 = TestObj->new('our1');
our $our2 = TestObj->new('our2');

via perlbug:  queue: perl5 status: resolved

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About