Re: recursive closures?

From: Brian McCauley (nobull_at_mail.com)
Date: 12/16/03


Date: 16 Dec 2003 17:25:58 +0000

Uri Guttman <uri@stemsystems.com> writes:

> >>>>> "BM" == Brian McCauley <nobull@mail.com> writes:
>
> > Uri Guttman <uri@stemsystems.com> writes:
> >> my $sub ;
> >> $sub = sub{ blah; $sub->() }
>
> > That leaks.
>
> it isn't real code anyhow. i don't recall if damian's leaked or he
> weakened it.
>
> > use Scalar::Util qw( weaken );
> > my $weak_sub;
> > my $sub = sub{ blah; $weak_sub->() }
> > weaken($weak_sub = $sub);
>
> it would only leak if you let it fall out of scope without destroying it
> (but how?).

"You only need weak references if you don't want use some other way to
take care of tracking and breaking the circular dependancy on every
possible exit path" is true of anything that uses weak references.

Of course if you want $sub to be a variable that is forcably undefined
implicitly at the end of a scope scope then Perl has just such a thing
- the local() variable!

Now, of course, it is an annoying miss-feature that local() can't be
applied to lexical variables so if you want to use local() you are for
forced to use package variables. What one really wants to do is...

    # Won't work
    my $sub;
    local $sub = sub{ blah; $sub->() }

...but one is forced to do...

    our $sub;
    local $sub = sub{ blah; $sub->() }

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


Relevant Pages

  • Re: BSTR, _bstr_t, std::wstring
    ... >> This leaks. ... when the temporary _bstr_t goes out of scope it will ... Igor Tandetnik ... With sufficient thrust, pigs fly just fine. ...
    (microsoft.public.vc.atl)
  • Re: gas leak?
    ... it leaks somewhere until it gets close to 1/2 empty. ... The gas filler neck overflow tube is rusted. ... and if that is the case, it's probably a factory recall item, and your local dealer should fix it for free. ...
    (rec.autos.makers.honda)
  • Re: [ANN] dike-0.0.1 - a memory leak detector
    ... goes out scope. ... consistent (no leaks), but wow is it easy to quickly create a lot of ... garbage to collect. ...
    (comp.lang.ruby)
  • Re: G5 coolant leaks
    ... I don't recall ever having seen it mentioned before. ... Liquid coolent leaks, ... When lots leaks it goes into the PSU and blows it, often taking down the mobo or CPU module. ...
    (uk.comp.sys.mac)