Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: What's the weaken here for?

by Anonymous Monk
on Jan 20, 2012 at 02:29 UTC ( #948869=note: print w/ replies, xml ) Need Help??


in reply to What's the weaken here for?

I don't see any difference with or without weaken $wrapper;,what's it there for?

Where exactly?

How are you checking to see a difference?

$ perl -MDevel::Peek -MScalar::Util=weaken -le " sub fudge { my $f = s +ub { print 1; }; Dump($f); weaken $f; Dump($f); $f; } $f = fudge(); $ +f->(); Dump($f);" SV = IV(0x3f8d18) at 0x3f8d1c REFCNT = 1 FLAGS = (PADMY,ROK) RV = 0x3f8cfc SV = PVCV(0x9a263c) at 0x3f8cfc REFCNT = 2 FLAGS = (PADSTALE,PADMY,ANON,WEAKOUTSIDE,CVGV_RC) COMP_STASH = 0x3f8b5c "main" START = 0x9d9178 ===> 0 ROOT = 0x9d913c GVGV::GV = 0x99a9f4 "main" :: "__ANON__" FILE = "-e" DEPTH = 0 FLAGS = 0x490 OUTSIDE_SEQ = 272 PADLIST = 0x99aa24 PADNAME = 0x99aae4(0x9a7d1c) PAD = 0x3f8d0c(0x9a7b44) OUTSIDE = 0x99aa74 (fudge) SV = IV(0x3f8d18) at 0x3f8d1c REFCNT = 1 FLAGS = (PADMY,ROK,WEAKREF) RV = 0x3f8cfc SV = PVCV(0x9a263c) at 0x3f8cfc REFCNT = 1 FLAGS = (PADSTALE,PADMY,RMG,ANON,WEAKOUTSIDE,CVGV_RC) MAGIC = 0x3fff2c MG_VIRTUAL = &PL_vtbl_backref MG_TYPE = PERL_MAGIC_backref(<) MG_OBJ = 0x3f8d1c COMP_STASH = 0x3f8b5c "main" START = 0x9d9178 ===> 0 ROOT = 0x9d913c GVGV::GV = 0x99a9f4 "main" :: "__ANON__" FILE = "-e" DEPTH = 0 FLAGS = 0x490 OUTSIDE_SEQ = 272 PADLIST = 0x99aa24 PADNAME = 0x99aae4(0x9a7d1c) PAD = 0x3f8d0c(0x9a7b44) OUTSIDE = 0x99aa74 (fudge) 1 SV = IV(0x9c2e40) at 0x9c2e44 REFCNT = 1 FLAGS = (ROK) RV = 0x3f8cfc SV = PVCV(0x9a263c) at 0x3f8cfc REFCNT = 2 FLAGS = (PADSTALE,PADMY,RMG,ANON,WEAKOUTSIDE,CVGV_RC) MAGIC = 0x3fff2c MG_VIRTUAL = &PL_vtbl_backref MG_TYPE = PERL_MAGIC_backref(<) COMP_STASH = 0x3f8b5c "main" START = 0x9d9178 ===> 0 ROOT = 0x9d913c GVGV::GV = 0x99a9f4 "main" :: "__ANON__" FILE = "-e" DEPTH = 0 FLAGS = 0x490 OUTSIDE_SEQ = 272 PADLIST = 0x99aa24 PADNAME = 0x99aae4(0x9a7d1c) PAD = 0x3f8d0c(0x9a7b44) OUTSIDE = 0x99aa74 (fudge)

As you notice, REFCNT doesn't change :)

Yeah, that is not a use case for weaken, you use weaken when you have circular references, say

my $foo = { kid => { } }; $foo->{kid}{parent} = $foo; weaken $foo->{kid}{parent};


Comment on Re: What's the weaken here for?
Select or Download Code
Re^2: What's the weaken here for?
by PerlOnTheWay (Scribe) on Jan 20, 2012 at 03:11 UTC
      The author must have done this for some reason...

      Because in the real code, unlike your simplification of it, the weakened sub reference forms a closure over itself:

      $wrapper = sub { $self->unsubscribe($name => $wrapper); $cb->(@_); };

      Ie. It contains a circular reference which would leak memory without the weakening.

      For example, with the weaken commented, this leaks like a sieve:

      #! perl -slw use strict; use Scalar::Util qw[ weaken ]; sub once { my $wrapper; $wrapper = sub { return $wrapper->(); }; # weaken $wrapper; return $wrapper; } once for 1 .. 1e9;

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

      The actual code is the sub once of Mojo::EventEmitter

      The author must have done this for some reason...

      Yeah, well Mr. Sherlock, that code is different than what you posted. The reason is always the same, circular references prevent release of memory

      { my $foo = {}; # ref count 1 $foo->{foo} = $foo; # ref count 2 } ### $foo goes out of scope here, memory should be released to perl # here, $foo is not addressable (you can't refer to it by $foo), # but its memory is not released until exit # because there is still a reference to $foo # it refers to it self, its self-referential, a circular reference # for memory held by $foo to be released after $foo goes out of scope # $foo->{foo} has to be weakened, the reference count decreased

      The actual code, which you should have copy/pasted to get a reasonable answer, since editing code you don't understand changes its meaning

      sub once { my ($self, $name, $cb) = @_; weaken $self; my $wrapper; $wrapper = sub { # a special kind of circular reference # CLOSURE, $wrapper keeps $self alive after return $wrapper (REFCNT+1) # $wrapper refers to itself, CIRCULAR REFERENCE $self->unsubscribe($name => $wrapper); $cb->(@_); }; $self->on($name => $wrapper); # potential CIRCULAR REFERENCE (on pro +bably also stores $wrapper, but it probably also uses weaken ) weaken $wrapper; return $wrapper; }

      How do I post a question effectively? says, when reducing code size, reduce to the essence, don't change the essence :)

        What about weaken $self?

        It's not done in other subs..

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://948869]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-10-25 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (143 votes), past polls