Some background: I'm writing a DBI driver (pure perl) for a proprietary system. The transport layer to the "database" is over http, and there is a whole perl object framework for interacting with the database. I've got it working pretty nicely, but I need to implement an "interrupt" functionality that communicates back to the server in the case where the client is terminated either by a SIGTERM, SIGINT or by an alarm/timeout.

The framework supports this interrupt functionality, but it must know what request it needs to interrupt, meaning that the $SIG{INT}/$SIG{TERM} subroutine needs to know which DBI statement handle is active at the time, and act accordingly.

My initial thought was to do something like this:

# This is $sth->execute sub execute { my $sth = shift; .... my $req = BigHairyDatabaseInteraction->new(...); local $SIG{INT} = sub { $req->handleInterrupt }; $req->execute; # When $req->excute comes back the entire query # has been processed, and all the rows are in $req. ... }
Initial testing appear to show that this works, but I'm worried that by using a closure like this $req will never get destroyed due to dangling references. I haven't really used closures before, so my experience in that area is rather limited.

Does the above technique seem reasonable, or would you suggest other techniques to achieve the same result?

Thanks!

Michael


In reply to Closures, object destruction and memory leaks by mpeppler

Title:
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, details, 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, summary, 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.