Skip to content

Memory Management in iPhone – 4

July 13, 2010

Hi Friends,

….continuation on memory management is following:

Power of nil:

You can send messages to an object that has been set nil value. And it will not throw an exception and will not crash. By this you don’t have to check an object for nil before invoking a method on the object. If yo invoke an method on nil which returns an object, you will receive nil as the return value.

If you have setters implemented for an object, you can use the following in your dealloc method.

consider you have object sampleObject.

– (void ) dealloc{

self.sampleObject = nil;

[super dealloc];

}

Here what you do is  passing your setter method a nil value and your setter releases the old value and sets the current value to nil. It is equivalent to the following two lines of code. And Apple suggests us not to manage them in dealloc method.

[sampleObject release];

sampleObject  = nil;

But beware not to try the following, because it would leak memory.

sampleObject  = nil;

Resource Management:

Design your classes such that the you manage scarce resources carefully by identifying cases when you no longer need the resources  and  release them. Never assume that your dealloc method would be invoked when you need them to be invoked, because their invocation may be delayed and this would have adversely affect the functions of the App that depend upon the scarce resources like network connections, file descriptors.

Collections

The collection objects (instance of NSArray, NSDictionary et. al)that you use will retain the object that you add to them. So ensure you either add objects that would be autoreleased or send the objects you created a release message. And keep in mind once the collection object is released, the objects stored in them would also be released.

Other Guidelines:

1. If you no longer wish to use an object and sent a release message to it, assign nil to it. By this, you are making sure that you don’t accidentally attempt to send a message to an object which does not exist.

2. When passing  local objects created with in the scope of a function as a return value of it, ensure that you return a autoreleased object by either

i) getting the object from a convenience constructor

ii) sending the object you created an autorelease message

3. Normally an autoreleased object would remain valid within the current scope i.e current method, so it is good to send an autoreleased object a retain message, if you wish to use it.

4. Applications that uses a lot of autorelease messages might be slower, because release message releases an object faster than autorelease. So use it judiciously.

5. When you receive an object as a result of method call, it will remain valid with in the scope of this function. If you want to store it in an instance variable, then either retain the object or copy the object.

6. Whenever you implement the -dealloc method, ensure you invoke your super class’s -dealloc method.

Will meet in the next article..

Advertisements

From → iPhone

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: