Re: recursive closures?

From: Uri Guttman (uri_at_stemsystems.com)
Date: 12/17/03


Date: Wed, 17 Dec 2003 14:42:03 GMT


>>>>> "BM" == Brian McCauley <nobull@mail.com> writes:

>> ------------------------
>>
>> They're not correct.

> Who is "they" here? Damian or "someone".

they being those (you?) who claim that recursive closures will leak.

>> At least, not under 5.8.0 or later (and probably earlier too). And
>> you can verify that by blessing the closure and seeing it die:

> The code you cite demonstrates that destruction will take place if you
> _do_ explicitly break the cirular ref.

that is no difference from $fib going out of scope.

i just changed the code to do just that:

{
     my $fib;
     $fib = bless sub {
                  my ($n) = @_;
                  return 1 if $n < 2;
                  return $fib->($n-1) + $fib->($n-2);
                }, 'Bang';

     # Test...

     print $fib->(7), "\n";
}

     package Bang;

     sub DESTROY { print "Bang!\n" }

it prints:

21
Bang!

so it sure looks like it got destroyed upon exiting scope. what more do
you want? i see no leak there

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


Relevant Pages

  • Re: recursive closures?
    ... it isn't real code anyhow. ... i don't recall if damian's leaked or he ... it would only leak if you let it fall out of scope without destroying it ...
    (comp.lang.perl.misc)
  • Re: recursive closures?
    ... [Of memory leaks in recursive closures] ... > implicitly at the end of a scope scope then Perl has just such a thing ... The SV pointed to be *subthat holds the CODE ref will _not_ ... What happens is that the scalar reference held ...
    (comp.lang.perl.misc)