develooper Front page | perl.perl6.language.objects | Postings from December 2000

Re: RFC from a newbie: Method References

Thread Previous | Thread Next
From:
Michael G Schwern
Date:
December 16, 2000 18:18
Subject:
Re: RFC from a newbie: Method References
Message ID:
20001216195452.A14451@chaos.wustl.edu
On Sun, Dec 17, 2000 at 12:11:01PM +1100, Jeremy Howard wrote:
> Something to be careful of--it's easy to create a circular reference when
> using method pointers. As a result, neither the referrer nor referee objects
> are ever destroyed.
> 
> When using method references it's important that your class keeps track of
> its references, and explicitly removes them when done to allow object
> destruction to occur. Perhaps this could be incorporated into
> Class::MethRef?

Circular references make my brane hurt, but I don't think there's any
circularlity here.  Once the method reference falls out of scope, so
will its reference to the object.  Then the object will be garbage
collected when it to falls out of scope.  The other way around, if the
object falls out of scope first, the method reference will still work
and the object will finally be destroyed when the meth ref goes away.
The only snag is if you put the method reference on the object its
refering to, that'll be circular.  I'll remember to document that caveat.

Now, you could have the method reference hold a weak reference to the
object (ie. a reference which does not increment its count).  That
way, when the object goes out of scope, it is destroyed regardless of
the state of the meth ref.  This, IMHO, is a Bad Thing, since it can
leave you with a method reference that suddenly doesn't work.  Its
Action At A Distance at its finest.  You hand off a method reference
(which looks just like any other function reference) to some unknowing
library which hangs onto it for later use.  The refering object might
then fall out of scope and the library is left with a function
reference that mysteriously doesn't work.

I don't know if those are the conditions under which you're worried
about circularlity occuring, but I can't think of anything else.


-- 
Michael G. Schwern   <schwern@pobox.com>    http://www.pobox.com/~schwern/

Thread Previous | 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