Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??


I've recently had a bunch of "fun" chasing down leaks in code (mine and others) caused by accidental capture of variables in callbacks.

e.g. something like:

sub foo { my $self = shift; $self->do_something(on_success => sub { do_stuff(); $self->log("done stuff"); do_more_stuff(); $self->log("done more stuff"); });

Assuming that do_something hides the callback within $self, we've just created a cycle which if it doesn't get cleared (e.g. on an error path) will cause a leak (could leak mem, could leak other resources such as network connections. badness.)

Note that the logging could have been added casually, and doesn't look evil on a review diff.

One idea being bounced around here would be a pragma which declared "I don't want any vars to be captured implicitly" and a way of marking which vars are ok to capture.

The thinking is that this would make it clear and explicit what vars are being captured, so it is easy to see them and reason about them. Something like:

use NoClose; sub foo { # Still a cycle, but now a more obvious one! $self->do_something(on_success => sub : capture('$self') { do_stuff(); $self->log("done stuff"); do_more_stuff(); $self->log("done more stuff"); });

With the idea being that any vars which a closure would capture would be compile-time errors unless they are referenced in the 'capture' attribute. this madness? And has it already been implemented? Or do people resolve this pain in a different way?

(I know about weak refs, the issue is more about making it easy to know what to weaken.)

[Edit, added comment to code to emphasise that this doesn't prevent cycles, just makes them more visible.

In reply to Managing capture by jbert

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (3)
    As of 2018-05-21 05:31 GMT
    Find Nodes?
      Voting Booth?