On Mon, Jul 14, 2008 at 09:14:05PM -0500, Jonathan Rockway wrote: > Hmm, it might be a good idea to ask about this on p5p.
The CODE ref one turned out to be a matter of optimisations in perl
core. It tries to share CODE refs if it can, when they don't have a
lexical pad.
push @array, sub {} for 1 .. 10;
will end up having 10 CODE refs to the same object, with a REFCNT of 11
(one per item in the array, plus one in the root code optree).
The solution here is
CODE => do { my $var; sub { $var } },
which forces a new pad, and hence a new distinct CODE ref of count 1.
That now fixes all the reference issues.
> It would be good
> to get this right Once And For All and make it really easy to use.
> Memory cycles are the most common cause of memory leaks in Perl, and
> they're really easy to fix once you know about them. So a module that
> makes them easy to find would be very good for everyone :)
I've relented now, and written a Devel::Refcount; see
http://search.cpan.org/~pevans/Devel-Refcount-0.01/lib/Devel/Refcount.pm
I decided in the end to opt for an XS implementation, given how small it
was, instead of using B - #perl/Freenode's suggestion.
return SvREFCNT(SvRV(ref));
I'll give that a while for comments and initial smoketests to settle,
then see about uploading my Test::Refcount, now changed to use this
instead.
--
Paul "LeoNerd" Evans
[EMAIL PROTECTED]
ICQ# 4135350 | Registered Linux# 179460
http://www.leonerd.org.uk/
signature.asc
Description: Digital signature
