develooper Front page | perl.perl5.porters | Postings from July 2013

[perl #119049] referencing some variables hides others from PadWalker starting 5.17.2

Thread Previous | Thread Next
From:
Father Chrysostomos via RT
Date:
July 30, 2013 19:33
Subject:
[perl #119049] referencing some variables hides others from PadWalker starting 5.17.2
Message ID:
rt-3.6.HEAD-2552-1375212813-1756.119049-15-0@perl.org
On Tue Jul 30 09:43:27 2013, demerphq wrote:
> On 30 July 2013 16:32, Dave Mitchell <davem@iabyn.com> wrote:
> > On Tue, Jul 30, 2013 at 01:31:30PM +0200, demerphq wrote:
> >> While I agree with your analysis I thought that PADMY vars werent
> >> freed until global destruction.
> >
> > PADMY vars are freed when the CV is freed whose pad they reside in
> > (as well as any other CVs which might also have captured them).
> >
> > For named CVs this is typically during global destruction; for anon CVs
> > this is typically  when the ref to the anon CV is freed.
> 
> This was in response to:
> 
> > I do not consider this a bug, because the closure does not close over
> > $var1 or $var3.  Those variables may already have been freed by the time
> > you call peek_my.
> 
> Given what you say I dont understand how $var1 or $var3 could ever
> freed by the time we call peek_my().

Not in the example given in the original report, but it can happen
easily other ways:

use PadWalker 'peek_my';
use Data::Dumper;
use Scalar::Util 'weaken';

my @vars;
sub outer {
    my ($var1, $var2, $var3);
    push @vars, \$var1, \$var2, \$var3;
    weaken $_ for @vars;
    return sub { $var2; @vars; warn Dumper peek_my(0); }
}

outer->()
__END__
$VAR1 = {
          '$var2' => \undef,
          '@vars' => [
                       undef,
                       $VAR1->{'$var2'},
                       undef
                     ]
        };

You see that two of the weak references in @vars have gone stale by the
time the closure is called.

-- 

Father Chrysostomos


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

Thread Previous | Thread Next


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