libFoundation contains some classes that allow you to write code without explicitly managing cyclic references between objects. Usually you have to manage explicitly the reference counts when you have objects that form cyclic graphs.
The garbage collector neither maintains explicitly all the pointers in your program nor it collects all the memory allocated by you in the program. Instead it collects and maintains only some special kind of objects, that are garbage collectable.
The algorithm was inspired from a similar one found in the OATH C++ library written by Brian M. Kennedy's bmk@csc.ti.com. It works in three passes. Suppose that all the objects that are garbage collectable are known. Also each object has an additional flag, used for determining if the object was already visited.
During the first pass, all objects that are garbage collectable clears
the associated flag and receive the message
-gcDecrementRefCountOfContainedObjects. In this method the object
decrements the reference count of every garbage collectable object it
contains. After this pass all the objects that have the reference count
0 are part of cyclic graphs. Such objects have their reference count due
only to another objects from graph.
In the second pass we have to restore the original reference counts and
to isolate the nodes from cyclic graphs. In this pass all the objects
that have the reference count greater than 0 receive the message
-gcIncrementRefCountOfContainedObjects. In this method the object
check the flag if it's set. This flag tells if the object was visited
previously. If the flag is set, the method returns, else the flag is
set. Then the reference count of every garbage collectable object
contained is incremented and the message
-gcIncrementRefCountOfContainedObjects is sent to those objects.
After this pass all the objects that are reachable from `outside' have their reference count greater than 0. All the objects that still have their reference count equal with 0 are part of cyclic graphs and there is no reference from `outside' to an object from these graphs.
In the third pass all the objects that have their reference count equal
with 0 receive the -dealloc message. In this method the objects
should send the -release message to all contained objects that
are not garbage collectable. This because the order of deallocation is
not known and you can send -release to an already deallocated
object. So if a class contains both normal objects and garbage
collectable objects, it should maintain their collectable status when
the objects are retained.
Go to the first, previous, next, last section, table of contents.