in reply to Reference to guard not released

In your example, here is what happens:

$g is never released until global destruction. So the Scope::Guard object is never called until that time. This is one problem in your code.

The difference between when you put $x or not is that in the first case it is a closure that keeps a reference to $x while in the second case this is just a bare anonymous sub. In the second case there is no circular reference.

You should also try to call $code->() multiple times to see when the Scope::Guard object is destroyed (because the content of $g is replaced).

You can explore more what happens to references by using Devel::Refcount:

use strict; use Scope::Guard; use Devel::Refcount 'refcount'; sub showcount ($\$) { printf "line %d: \$%s => %d\n", (caller)[2], $_[0], refcount($_[1] +)-1; } my $x; showcount(x => $x); my $code = do { my $g; showcount(g => $g); # By creating a closure that reference $g, we increase its refcoun +t to 2 sub { # Here the only reference to $g is the closure showcount(g => $g); $g = Scope::Guard->new( sub { warn "destroyed"; $x; } ); } # Here $g is out of scope, so refcount of $g decreases to 1 }; showcount(x => $x); $code->(); showcount(x => $x); #$code->(); showcount(x => $x); undef $code; # Here, I expect the refcount of $x to decrease, but it doesn't showcount(x => $x); warn "end";

Replies are listed 'Best First'.
Re^2: Reference to guard not released
by dolmen (Beadle) on Jul 18, 2012 at 14:04 UTC

    Well, it is more interesting to show when the closure stored in $code is effectively destroyed:

    use strict; use Scope::Guard; my $x; my $code = do { my $g; sub { my $z; $g = Scope::Guard->new( sub { warn "destroyed"; $z } ); } }; use Scalar::Util (); Scalar::Util::weaken(my $code2 = $code); $code->(); printf "code2: %d\n", !!(defined $code2); undef $code; printf "code2: %d\n", !!(defined $code2); warn "end";
    Output on perl 5.14.2:
    code2: 1 code2: 1 end at x.pl line 21. destroyed at x.pl line 10 during global destruction.
    As shown above, the issue is independent of $x being global: here I'm using $z which is local to the enclosing closure and the behavior is the same: the closure pointed by $code is not freed.