develooper Front page | perl.perl5.porters | Postings from December 2011

[perl #106864] refcount for anonymous sub is 2 instead of 1

Thread Previous
Father Chrysostomos via RT
December 30, 2011 08:27
[perl #106864] refcount for anonymous sub is 2 instead of 1
Message ID:
On Fri Dec 30 06:31:34 2011, dolmen wrote:
> Le Sam. Déc. 24 18:55:03 2011, sprout a écrit :
> >
> > I think this is a bug, in that mentioning a variable in a sub shouldn’t
> > change whether $x has the only reference after ‘$x = sub{}’.
> Well, it does.
> In my use case, I use weaken on closures to break circular references
> (an object method that stores as a member a closure that references
> $self, and I want the closure to disappear when $self is not anymore
> referenced). This is quite common when working with AnyEvent.
> The bug I had in my code was that I did use weaken "as usual" on the
> sub. So my mistake was to do a weaken on something which was not a
> closure: there was no circular reference problem in the first place. I
> was trying to use 'weaken' to workaround a problem which didn't exist.
> To avoid such mistakes in the future, would it be possible to detect and
> report such cases of usage of weaken on non-closure subs?

But holding a weak reference to a non-closure sub is not necessarily a
bug.  Weak references are not just for breaking circularities, you know.
:-)  Sometimes they are useful for maintaining mappings between
references.  See, e.g., Tie::RefHash::Weak.

> > It also causes the blessing bug in ticket #3306.
> Thanks for that reference. I submitted an idea on how to fix that one too.
> --
> Olivier


Father Chrysostomos

via perlbug:  queue: perl5 status: open

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About