Skip to Content
Objective-C Pocket Reference
book

Objective-C Pocket Reference

by Andrew Duncan
December 2002
Beginner content levelBeginner
128 pages
3h 10m
English
O'Reilly Media, Inc.
Content preview from Objective-C Pocket Reference

Memory Management

The liveness of an object—whether or not it is referenced by other objects currently in use—is inherently a global property. This means that keeping track of object usage tempts you to breach the encapsulation and modularity of your program. There are several approaches you can use with Objective-C to help manage object lifecycles:

  • Manual memory management, using the free functions provided by the Objective-C runtime

  • Semi-automatic memory management, using root class reference-counting methods

  • Automatic memory management, using third-party garbage collector tools

The programming tools and libraries you use may constrain you toward or away from some strategies. For example, the Cocoa class library uses reference counting; if your code uses Cocoa objects it will have to be written to manage those objects with reference counting, even if your own objects use a different mechanism.

Manual Memory Management

You can explicitly release the memory held by an object by calling its -free method (if it inherits from Object) or its -dealloc method (if it inherits from NSObject). It will be your responsibility to ensure that your program no longer needs the object.

In classes you write, the deallocation method should release any objects managed by the receiver, as well as any other kinds of resources (such as network sockets) the receiver holds. As with deciding to deallocate the receiver itself, it is your responsibility to ensure that your program no longer needs the objects the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Pro Objective-C

Pro Objective-C

Keith Lee
Learning Cocoa with Objective-C, 4th Edition

Learning Cocoa with Objective-C, 4th Edition

Paris Buttfield-Addison, Jonathon Manning, Tim Nugent

Publisher Resources

ISBN: 0596004230Errata Page