in reply to Re: Unpredicted late destruction
in thread Unpredicted late destruction

Update: ack, this appears to be a pre-5.8.0 bug as it's fine after 5.8.0 e.g
shell> perl5.8.0 pmsopw_304778.pl loop: pre buh-bye: sub buh-bye: sub loop: inside buh-bye: for loop: inside buh-bye: for loop: post

Perhaps this is a better illustration of the problem

sub DESTROY { print "bye: @{$_[0]}\n"; } sub f { my $funcobj = bless["sub"]; return 1; } print "loop: pre\n"; for( f(), f() ) { print "loop: inside\n"; my $forobj = bless ["for"]; } print "loop: post\n"; __output__ loop: pre loop: inside bye: for loop: inside bye: for bye: sub bye: sub loop: post
So $funcobj, which one would assume to be GCed upon the exit of f(), sticks around until the end of the for loop regardless of the fact that nothing is referencing it.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Re: Unpredicted late destruction
by ferrency (Deacon) on Nov 12, 2003 at 15:36 UTC
    Thank you for the great example of a minimal failure case! And thanks for determining that this has already been fixed :)

    I've discovered that the same problem exists for functions called in an if expression as well. Adding the following test to the bottom of your script does the wrong thing in 5.6.1 but the right thing in 5.8.0:

    print "if: pre\n"; if (f()) { print "if: inside\n"; my $ifobj = bless ["if"]; } print "if: post\n";
    Interestingly (to me, anyway), there didn't seem to be a problem with while() clauses:

    print "while: pre\n"; while (f()) { print "while: inside\n"; my $wobj = bless ["while"]; last; } print "while: post\n";
    This does the right thing on 5.6.1.

    Thanks again!

    Alan