Go to the first, previous, next, last section, table of contents.


The GarbageCollecting protocol

To allow instances of a class to be garbage collectable, the class should implement the following protocol:

@protocol GarbageCollecting

- gcSetNextObject:(id)anObject;
- gcSetPreviousObject:(id)anObject;
- (id)gcNextObject;
- (id)gcPreviousObject;

- (void)gcIncrementRefCount;
- (void)gcDecrementRefCount;

- (void)gcDecrementRefCountOfContainedObjects;
- (BOOL)gcIncrementRefCountOfContainedObjects;

- (BOOL)isGarbageCollectable;

@end

The GarbageCollector class uses a double linked list to maintain the objects. The gcSetNextObject:, gcSetPreviousObject:, gcNextObject and gcPreviousObject are used by the collector to add or remove objects in its list. This could change in the future.

The gcIncrementRefCount and gcDecrementRefCount methods should increment, respectively decrement the reference count of receiver.

The gcDecrementRefCountOfContainedObjects method should decrement the reference count of all garbage collectable objects contained, by sending them the message -gcDecrementRefCount to them.

The gcIncrementRefCountOfContainedObjects method should check the flag. If this is true, the method should return NO; it this is false the method should set it. Then it should increment the reference count of garbage collectable objects contained by sending them the -gcIncrementRefCount message. After this it should send the -gcIncrementRefCountOfContainedObjects message to the same objects. Then it should return YES.

The object should respond YES at the -isGarbageCollectable message if it is garbage collectable. The NSObject class responds NO to this message.

You should note the asymmetry between the gcDecrementRefCountOfContainedObjects and gcIncrementRefCountOfContainedObjects methods. This makes the algorithm to work. So be careful if you're using copy/paste operations to write them in your editor :-)!


Go to the first, previous, next, last section, table of contents.