develooper Front page | perl.perl5.porters | Postings from August 2014

[perl #122556] DESTROY handler not firing until after function return.

Thread Previous
From:
Father Chrysostomos via RT
Date:
August 28, 2014 05:24
Subject:
[perl #122556] DESTROY handler not firing until after function return.
Message ID:
rt-4.0.18-29707-1409203462-641.122556-15-0@perl.org
On Thu Aug 21 07:59:10 2014, LeonT wrote:
> On Mon, Aug 18, 2014 at 8:59 PM, felipe@felipegasper.com <
> perlbug-followup@perl.org> wrote:
> 
> > Consider the following:
> >
> > ==============================
> > #!/usr/bin/perl
> >
> > package Thingie;
> >
> > use Carp;
> >
> > sub DESTROY { print Carp::longmess() }
> >
> > package main;
> >
> > use strict;
> > use warnings;
> >
> > sub body {
> >     sub {
> >         my $t = bless [], 'Thingie';
> >         undef $t;
> >
> >         #When the below is commented out, $t isn't garbage-collected until
> >         #body()'s return. When it's uncommented, $t is garbage-collected
> >         #right here.
> >         #
> >         #return;    #<== UNCOMMENT, AND COMPARE Carp::longmess OUTPUT.
> >     }->();
> >
> >     return;
> > }
> >
> > body();
> > ====================
> >
> > When the "undef $t" is the last statement in the anonymous sub, Perl is
> > waiting until after that scope is finished to do the GC. This runs counter
> > to expectation since the last reference to the Thingie is removed by
> > setting $t to undef.
> >
> > Moreover, this can produce errors if Thingie references a global in its
> > DESTROY and the anonsub did a "local" on that global.
> >
> > The proper course would seem to be to do GC within the scope. The fact
> > that Perl doesn't behave this way currently looks to me like a bug.
> >
> 
> I can see why this happens (it mortalized the referenced SV, instead of
> destroying it immediately). The obvious fix breaks assignment under some
> circumstances. I think this can be fixed, but it needs to be thought out
> well.

undef is distinct from assignment.  We can fix this case by having pp_undef pass the SV_IMMEDIATE_UNREF flag, no?  (I am testing a patch right now.)  Would that cause any unforeseen problems?

-- 

Father Chrysostomos


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=122556

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About