On Sun, Dec 17, 2000 at 11:28:49PM +1100, Jeremy Howard wrote:
> There's not necessarily any circular reference. The problem is that method
> references are frequently used to implement event callbacks. This would
> generally look something like this:
Hmmm... an object which contains a method reference which contains a
referent to itself.
> Something like this would be nice in a class that creates method
> references--it would simply need to keep a list of referred objects, and
> have an explicit destructor that iterates through the references and undefs
> them. Of course, calling the destructor would be optional where no circular
> reference exists.
Yes, you could keep a list/hash of what you created (as weak
references) and explicitly destroy them but I don't think that
would help. Consider the following...
%methref_cache = ();
{
my $a = A->new;
my $b = B->new;
my $methref = sub { $a->did_the_nasty_with($b) };
$methref_cache{someid} = $methref; # I forget how to do weak refs.
$b->{callback} = $methref;
}
# This has no effect. $b still contains a referent to $methref which
# contains a referent to $b, etc...
delete $methref_cache{someid};
You could make $methref a double reference (scalar reference to a code
ref) and then say C but that makes the
calling syntax nasty, something like &{${$methref}} (someone can
probably write that better, but it'll still be yicky). Besides, the
idea of having a method which suddenly makes all the meth refs
program-wide not work sounds Bad.
Which doesn't solve the problem... and I don't have any better ideas.
--
Michael G. Schwern <[EMAIL PROTECTED]>http://www.pobox.com/~schwern/