Deallocating Objects
When an
object
is ready to be destroyed (as determined by the reference counting
mechanism), the system will give the object an opportunity to clean
up after itself by calling the
dealloc
method defined by
NSObject
. If the object has created or retained
any other objects’ reference by its instance
variables, it must implement this method and perform the appropriate
tasks to maintain integrity of the reference counting system.
In Example 1-8, the Song
class
retains the title
instance variable in the
setTitle
: method. To properly implement memory
management, you need to balance this retain with a release. Example 1-10 shows the release performed in the
Song
class’s
dealloc
method.
This provides proper balance in the reference counting mechanism.
Get Cocoa in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.