views:

38

answers:

3

Hi Friends,

Consider I have a property named sampleObject.

In dealloc method, which should be the best way to manage memory?

Option 1:

self.sampleObject = nil; //This will release the sampleObject and set it to nil

This is equivalent to

[sampleObject release];

sampleObject = nil;

Option 2:

Explicitly releasing an object and setting it to nil

[sampleObject release];

sampleObject = nil;

In my opinion, both would achieve the same results? Please Share your views.

Regards, Krishnan

+1  A: 

in a dealloc method, the class is never used again so setting retained resources/properties to nil is just not required. Sending release is the best option and avoids unnecessary code.

Thanks friend. As you said, the class would be never used again so setting the properties to nil would be unnecessary..
Krishnan
+1  A: 

Going through the property setter does have a small overhead over directly sending the release message. Thus, for synthesized properties, it's better to send release.

Of course, there are cases where you have to call the property setter, if the setter logic is more complex (for example, the property is backed by multiple variables and the setter decomposes the value and cleans up the old one). This is not as common scenario, though.

And since you are deallocating your object, there's no need to set the backing variable explicitly to nil after you release it.

Franci Penov
For instance variables,there should be no need to go through the setter during deallocation. If there is, it is indicative of a design bug.
bbum
some properties can be backed by more than one variable. properly releasing the property requires cleanup of all variables. this logic is already contained in the setter, so calling it is simpler than replicating it in the dealloc.
Franci Penov
Nope -- still a bad idea -- what happens if someone subclasses the setter and adds additional logic? Your `dealloc` method should be releasing the ivars and only the ivars. Since it is *inside the class itself* there is no breaking of encapsulation in doing so. You **should not have** any complex logic taking place in `dealloc`.
bbum
Interesting. So you concern is that someone might subclass my code and override the setter so that it breaks my dealloc without breaking my public interface and any external code that relies on it?
Franci Penov
Thakns Franci and bbum. I guess from your views on using setter in dealloc, it is better to avoid it. And once an object's -dealloc is called , its instance variables need not be set to nil(For me ultimate purpose of using the setter is to release and set the instance variable to nil). But I guess we can use this setter in ViewDidUnload method to clear up memory allocated in ViewDidLoad.
Krishnan
+1  A: 

In the 'dealloc' method you should 'release' the ivar directly and set it to nil.

You do so directly because that avoids executing any custom setter code that may exist in a subclass.

The setting to nil isn't strictly necessary, but it costs virtually nothing to do and you'll be happy you did so during debugging in that it eliminates a dangling pointer.

bbum