Programming Tutorials

Garbage Collector in Cocoa Programming in Mac

By: Aaron Hillegass in Cocoa Tutorials on 2010-09-24  

As long as you use only Objective-C objects, the garbage collector will do exactly what you want, without your needing to think about it. However, as soon as you start to malloc C data types and Core Foundation structures, you will need to be a bit more circumspect.

When the garbage collector runs, it is looking for unreachable objects. You can think of the objects in your applications as a directed graph: This object knows that object; it knows those objects; and they know those other objects. So, the garbage collector starts with the pointers on the stack and the global variables and wanders this directed graph until it has recorded every "reachable" object. The unreachable objects are deallocated. See Figure below.

Figure: Reachable Objects

Before an object is deallocated by the garbage collector, it is sent the finalize message. Overall, you shouldn't need to implement finalize very often, but if you do, it looks like this:

- (void)finalize
    ...Do some last minute stuff here...
    [super finalize];

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in Cocoa )

Latest Articles (in Cocoa)