Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi.

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.

So...is 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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found