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

Re: Disposing object instances

> I noticed I have to remove all references of all objects that I don't need,
> otherwise memory runs out. Is there other way to inform the gc directly 
> that somehing is not needed anymore?


It sounds like you are refering to weak pointers.  These are
references that GC will ignore when it looks to see if a object
is still alive.  Coupled to that is the concept of finalization -
a way for a object to be informed right before it is GCed.

The problem with you "explicitly" telling the GC to release an object

 What happens when one of the those "unimportant" references 
 tries to access a previously disposed of object?

This is the problem that people run into in non-GC enviroments.  It
usually causes machines to crash.  Weak pointers are usually used
when you need to keep tabs on an object (such as an external resource)
that you don't really have control of anyway.  The best way to kill your
object, is to have the object contain a list of those objects that refer
to it.  When you want to GC it, simply ask your object to go through
it's list and tell everyone it is about to disappear.  The other objects
then delete their references to it, and you let GC take its natural course.

See below for an example.

This is the safest way, because if you mess up and don't explicitly 
delete a reference, your object is still around (and your program does
not crash).


Jeffrey Kane, MD
Kane Biomedical Systems
Boston, MA

Internet    jbk@world.std.com
Compuserve  74206,640
AppleLink   D0738

[Don't take life too seriously... it's not like anyone gets out of it alive.]

(defclass special-object ()
		:accessor list-of-objects-that-know-about-me
		:initform nil)))

(defmethod please-gc-me ((me special-object))
	(dolist (who list-of-objects-that-know-about-me)
		(delete-ref-to-me who me)))
(defmethod make-weak-ptr-to-me ((me special-object) who)
	(push who (who list-of-objects-that-know-about-me))
(defmethod delete-ref-to-me (who (me special-object))
	; customize for each object that can handle a weak pointer)

Sent: 2/6/1995, 18:4