Gisle Aas <[EMAIL PROTECTED]> writes:

> The current Tcl.pm code works because it does '$srefs{"$s"} = $s'
> instead of the '$srefs{"$s"}++' you do above.  This keeps the code
> alive even after $s and $t are cleared.

Looking closer this isn't really true.  The $srefs reference is not
really needed as the client data saved in XS(Tcl_CreateCommand) also
contains a reference to the CV.  This reference is cleared in
Tcl_PerlCallDeleteProc if the Tcl command is deleted.

Consider this test program:

   use Tcl;
   use Devel::Peek;
   my $i = Tcl->new;
   my $cv = sub { $i };
   Dump($cv);
   $i->call("puts", $cv);
   Dump($cv);

You will see that the REFCNT on the $cv is 1 before the $i->call and 3
after.  The reason it is 3 is that %anon_refs stores one reference and
the client data of the Tcl command created stores another.

The only reason for the %anon_refs reference seems to be to make
$i->return_ref($name) able to return it.  It's not needed to keep the
CV alive until Tcl calls it.

--Gisle

Reply via email to