To keep memory consumption as low as possible in an application, it is necessary to get rid of objects that are not being used, but you need to make sure that you don’t get rid of an object that is being used. You therefore need a mechanism that allows you to mark an object as still being useful. In many respects, memory management is thus best understood in terms of “object ownership.”
- An object may have one or more owners.
By way of an analogy, consider a timeshare apartment. - When an object has no owners, it is destroyed.
To stretch the analogy, consider a timeshare complex that is not loved by the local population. If there are no owners, the complex will be torn down. - To make sure an object you’re interested in is not destroyed, you must become an owner.
You can either build a new apartment, or take a stake in an existing one. - To allow an object in which you’re no longer interested to be destroyed, you relinquish ownership.
You can sell your timeshare apartment.
1
. When the retain count drops to 0
, an object is deallocated (destroyed). You manipulate the retain count (take and relinquish ownership) using a variety of methods:- Allocates memory for an object, and returns it with retain count of
1
. You own objects you create using any method that starts with the wordalloc
or with the wordnew
. - Makes a copy of an object, and returns it with retain count of
1
. If you copy an object, you own the copy. This applies to any method that contains the wordcopy
where “copy” refers to the object being returned. - Increases the retain count of an object by
1
. Takes ownership of an object. - Decreases the retain count of an object by
1
. Relinquishes ownership of an object. - Decreases the reference count of an object by
1
at some stage in the future. Relinquishes ownership of an object at some stage in the future.
alloc
copy
retain
release
autorelease
- You only own objects you created using a method whose name begins with “alloc” or “new” or contains “copy” (for example,
alloc
,newObject
, ormutableCopy
), or if you send it aretain
message.
Many classes provide methods of the form+className...
that you can use to obtain a new instance of the class. Often referred to as “convenience constructors”, these methods create a new instance of the class, initialize it, and return it for you to use. You do not own objects returned from convenience constructors, or from other accessor methods. - Once you have finished with an object that you own, you should relinquish ownership using
release
orautorelease
.
Typically you should userelease
rather thanautorelease
. You useautorelease
only when immediate deallocation of the object would be inappropriate, for example you are returning an object from a method. (Note: this does not imply thatrelease
necessarily results in deallocation of an object—it will only do so if the retain count drops to0
as a result - Implement a
dealloc
method to release the instance variables you own. - You should never invoke
dealloc
directly (other than when you invoke super’s implementation in a customdealloc
method).
IT IS IMPORTANT TO GO THROUGH THE BASICS
ARC is a good replacement.....
ReplyDelete