[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Weak references & GC

From: James Knight
Subject: Re: Weak references & GC
Date: Wed, 19 Feb 2003 19:32:48 -0500

Frederic De Jaeger wrote:
I don't fully agree with the fact that the system should remove dead
objects from the list of observers.  In my opinion, any program that
relies on this fact is not well designed.  The reason is that an
object that is not in the  graph of living objects should not
receive any notification, wether it has been collected or not.

Well, it depends on whether the program cares about that or not! In my case, the notification only changes the object's state, so if the object is no longer referenced, and will be collected at some future point, any notifications it gets will not change anything.

Also, don't forget the other half of the problem, observations for a notification *from* objects that no longer exist. While I think this won't cause a crash, it can fill up the observation table needlessly. For this case, these should definitely be tracked and removed.

Collection occurs non deterministically.  It looks strange to me that
some notifications would be sent or not, depending on what the GC is
doing.  And I cannot imagine what is the meaning of a zombie object
(someone that is waiting to be collected) receiving a notification.

It sounds like you think the notification system should use a strong reference to the observer. That is a possibility to consider. I currently believe that it should be using weak refs as it is now, but I'm willing to listen to the other possibility..

However, as I said in my email to R F-M, it is a matter of correctness that the system not be caused to crash, so either the reference has to be strong, or we have to take care to remove it when the object goes away.

IMHO, being an observer is a resource that needs to be free
explicitely, when it is not needed anymore.  And this applies to all
kind of resources (except memory, that is handled by the GC) like
files, sockets, windows...

Please note that in these other cases, if the programmer does not take care of it themselves, the GNUstep library is set up so that the gcFinalize does it for them. Yes, there is a matter of correctness when not e.g. closing a file yourself and waiting for the GC to do so (you may fill up the file table before the GC gets around to collecting), but at least there is the failsafe that occurs if you mess up.

In such situation where is cannot be avoided, it is always possible to
"removeObserver:"  in a "gcFinalize" method (but I'm sure you already
know this)

There should be no reason for users to have to do this themselves. If the user doesn't care when the observations go away (as would be indicated by putting removeObserver in gcFinalize), the library should just do it for them.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]