Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^4: Is it possible to create a sub exclusive to a sub? (nasty memory leak)

by ihb (Deacon)
on Sep 19, 2004 at 10:46 UTC ( [id://392159]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Is it possible to create a sub exclusive to a sub? (nasty memory leak)
in thread Is it possible to create a sub exclusive to a sub?

you say the anonymous sub isn't a closure

Indeed I did. I expressed myself poorly and I've rephrased my post to hopefully better express what I meant.

(But first, to avoid any confusion: a closure is a subroutine, named or anonymous, that binds its lexical context.)

A lexical anonymous recursive subroutine is by necessity a closure. What I meant was that the subroutine, if written as a named subroutine, isn't a closure. By making it lexical and anonymous you make it one though, but the only lexical surrounding it is using is its own declaration.

The memory leak comes from that inside the subroutine, you use $_fibo. That makes $_fibo reference a subroutine that references $_fibo. Thus, $_fibo is a circular reference and refcount will never drop below two and won't be garbage collected.

This below illustrates what I've just said. On my perl, and probably yours too, the following will print the same for each iteration.

sub foo { my $foo; $foo = sub { }; print $foo; } for() for 1 .. 5; __END__ CODE(0x1823d78) CODE(0x1823d78) CODE(0x1823d78) CODE(0x1823d78) CODE(0x1823d78)
This is because $foo gets garbage collected and the memory slot may be is currently is reused. (Since this just happens to be the implementation you can't trust it to always perform the same though.)

If you change the subroutine to bind $foo you get

sub foo { my $foo; $foo = sub { $foo }; print $foo; } for() for 1 .. 5; __END__ CODE(0x274f7c) CODE(0x274da8) CODE(0x1823e48) CODE(0x1823e90) CODE(0x1823ed8)
Here we see that the memory slot isn't being reused.

It can also be interesting to look at the output of the &Dump subroutine in the Devel::Peek package. (For extra effect, dump the sub after doing differently many recursive calls.)

I hope I managed to express myself better this time.

ihb

Read argumentation in its context!

Replies are listed 'Best First'.
Re^5: Is it possible to create a sub exclusive to a sub? (nasty memory leak)
by dragonchild (Archbishop) on Sep 19, 2004 at 13:56 UTC
    What I meant was that the subroutine, if written as a named subroutine, isn't a closure. By making it lexical and anonymous you make it one though, ...

    That makes no sense to me, but I have a fuzzy definition of Perl's closures, so that may not be surprising.

    The confusing part is this.

    { my $foo = 1; sub get_and_inc_foo { $foo++ } }

    I was always told that get_and_inc_foo() is a closure over $foo. But, it's neither anonymous nor lexical, so how does that work?

    If my example is a closure, then aren't all subroutines closures over the file-scoped lexicals in the file they're defined in?

    (Now that I think of it, isn't this the reason why you cannot have globals when using ModRegistry?)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      &get_and_inc_foo is a closure, because it binds a lexical variable defined outside of it.

      If my example is a closure, then aren't all subroutines closures over the file-scoped lexicals in the file they're defined in?

      If they don't use any lexicals defined outside them, they're not closures. sub foo { 1 } isn't a closure no matter where it's defined.

      my $foo; sub closure { $foo } sub not_closure { 1 }
      What I mean with making the subroutine a lexical anonymous subroutine is the following.
      my $foo; # lexical $foo = sub { $foo->() }; # closure local $bar; # dynamical $bar = sub { $bar->() }; # not a closure

      ihb

      Read argumentation in its context!

        So, if I had something like:
        package main; my $foo = 1; sub foobar { $foo++; }

        Does that make foobar() a closure?

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        I shouldn't have to say this, but any code, unless otherwise stated, is untested

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-23 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found