http://www.perlmonks.org?node_id=982346


in reply to Reference to guard not released

Because you never call the sub that contains the Scope::Guard;
Try:
use strict; use Scope::Guard; my $x; my $code = do { my $g; my $s=sub { $g = Scope::Guard->new( sub { warn "destroyed"; $x ; +} ) }; $s->(); }; $code->(); $code=undef; warn "end"; ---- Not a CODE reference at /tmp/pt1 line 11. destroyed at /tmp/pt1 line 7 during global destruction.

You are also creating a sub that references a 'global' ($x) that won't go out of scope until BOTH the global is 'done' and until the anonymous sub (with a reference in Scope::Guard, presumably), goes out of scope -- and it may not destroy it's reference until the program has exited (i.e. it may be in an allocated hash, once the allocated hash has been destroyed, the 'sub' goes away, and then the '$x' goes away, and everyone is destroyed...

AAiieee....