develooper Front page | perl.perl5.porters | Postings from August 2003

Re: [perl #10030] DESTROY not called on code reference objects

Thread Next
From:
Dave Mitchell
Date:
August 17, 2003 15:07
Subject:
Re: [perl #10030] DESTROY not called on code reference objects
Message ID:
20030817220715.GB22252@fdgroup.com
About a year ago, Simon Cozens reported a bug to the effect that

    { my $x = bless sub { }, 'X'; }
    sub X::DESTROY { print "DESTROYED\n" }

Doesn't call the destructor.

Having looked at it, the reason is that the anon sub isn't a closure
(ie doesn't refer to any outer lexicals. Thus, the CV is shared rather
than cloned, so its refcnt is >1 when $x is freed.

Turning it into a closure makes it work:

    my $y;
    { my $x = bless sub { $y }, 'X'; }
    sub X::DESTROY { print "DESTROYED\n" }

Simon mentions that "either the documentation or the code are wrong".
Since it would be inefficient to fix, and since closureless anon subs
are more common that blessed coderefs (I would speculate), I think it
should be documented as a misfeature.

Dave.

-- 
In England there is a special word which means the last sunshine
of the summer. That word is "spring".

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About