Introduction

First I would like to thanks my IPhone team in helping me in compiling stuff on IPhone memory leakages that they faced during our IPhone project development.

In any program we write, we must ensure that we manage resources effectively and efficiently. One such resource is our program’s memory. In an Objective-C program, we must make sure that objects we create are disposed of when we no longer need them. We need to grab two below concepts for that:

Object Ownership

It’s important to understand the concept of object ownership. In Objective C, an object owner is someone (or piece of code) that has explicitly said “Right, I need this object, don’t delete it”. This could be the person (or code) that created the object (e.g. Class A in the example above). Or it could be another person (or code) that received the object and needs it (e.g. Class B in the example above). Thus an object can have more than one owner. The number of owners an object has, is also the reference count or retain count. And you can get it by

NSLog(@”My Obj retain count is %d”,[obj retainCount])

Object owners have the responsibility of telling the object when they are done with it. If you own that object then you have to call its release method.

[obj release];

When they/you release an object, they/you are no longer an owner and the object’s reference count is decreased by one. When an object has no owners left (i.e. no one needs it, reference count is zero), it is deleted, and its dealloc is called.

You can still use an object without being its owner (using it temporarily), but bear in mind that the object might (and probably will) get deleted in the near future. So if you need an object long-term, you should take ownership. If you only need the object there and then (e.g. within the function that you received it, or within that event loop ), then you don’t need to take ownership.

Now you should have one question in your mind.

How can i own an object?

It’s simple when you init or alloc an object or you copy or mutable copy it from another object then you are owner of this object, and now it’s your responsibility to release it as soon as you are done with it, otherwise it may leak and may cause in rejection from AppStore.

alloc

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 word alloc or with the word new.

copy

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 word copy where “copy” refers to the object being returned.

retain

Increases the retain count of an object by 1.
You call this when you are passed an existing object (memory has already been allocated for it elsewhere), and you want to tell the object that you need it as well. This is like saying “Ok, I need this object, don’t delete it till I’m done.”. The reference count is increased by one, and you are the new owner of this object (along with any other previous owners). You must release the object when you are done with it.

release

Decreases the retain count of an object by 1.
You call this when you are done using an object. You are no longer the owner of the object so the reference count is decreased by one. If reference count is now zero (i.e. no owners left), then the object is automatically deleted and the memory is freed, otherwise the object stays in memory for other owners to use. It is like saying “Right, I’m done with this object, you can delete it if no one else is using it”. If you are not the owner of an object, you should not call this. If you are the owner of an object, you must call this when you are done.

autorelease

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.

Autorelease Pools

An autorelease pool is an instance of NSAutoreleasePool and defines a scope for temporary objects (objects which are to be autoreleased). Any objects which are to be autoreleased (e.g. objects you send the autorelease message to or created with convenience methods) are added to the current autorelease pool. When the autorelease pool is popped (released) all objects that were added to it are also automatically released. This is a simple way of managing automatic release for objects which are needed temporarily.

E.g. You want to create a bunch of objects for temporary calculations, and instead of keeping track of all the local variables you define and then calling release for all of them at the end of your function, you can create them all with autorelease (or convenience methods) safe in the knowledge that they are going to be released next time the autorelease pool is popped.

Autorelease pools can be nested, in which case autorelease objects are added to the latest autorelease pool to be created (the pools are stacked in a Last In First Out type stack).

Part 2: Memory Management Special Cases

Part 3: Memory Leakage detection using Xcode Intrument

References

About these ads