develooper Front page | perl.perl5.porters | Postings from March 2001


Thread Previous | Thread Next
Gurusamy Sarathy
March 5, 2001 07:51
Message ID:
On Mon, 05 Mar 2001 11:02:34 GMT, Alan Burlison wrote:
>The question I have is does the SvREFCNT_inc(outsidecv) have to happen
>anyway in the case of an anon sub?   It will disappear if its outer
>scope disappears, so why does it need to increment the outer scopes
>refcount at all?  I'm thinking of the changes that were made to break
>the stash refcount loop, where the element of the stash had a refcounted
>handle back to its parent - the fix there was just to not increment the
>refcount, on the basis that the child couldn't exist without the parent
>anyway.  I can see that named subs *would* have to increment the
>refcount of the outer scope, because they are entries in a stash and can
>therefore exist after the scope that created them disappeared.

Consider this piece of code:

      sub X {
          my $n = 10;
	  sub { print $n }
      my $x = X();
      undef &X;      # or a redefinition of X() via eval"" or *X = ...

There are three CVs there: X(), the prototype anonymous sub within
X(), and the clone of the anonymous sub (generated via pp_anoncode())
that gets put in $x.  Note how the lifetime of $x can extend beyond
that of X().  The lifetime of the prototype sub is tied to the lifetime
of X(), so it doesn't need to own a refcount on X(), but $x does need
to keep a refcount to X() if any evals within it are expected to "see"
lexicals within X().

Incidentally, that example will segfault without the patch I sent.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About