Yazzmi
Yazzmi

Reputation: 1571

memory management on the iPhone with viewDidUnload

If you set a property to nil in viewDidUnload do you need to release it again in dealloc?

Upvotes: 3

Views: 361

Answers (1)

Seamus Campbell
Seamus Campbell

Reputation: 17906

No, but:

  • You don't need to check for that case. [nil release] is fine.
  • You can't count on viewDidUnload being called.

So just release as normal in -dealloc.

Of course, you must make sure that you actually released the previous object. You implicitly do this if you used the synthesized setter:

self.myProperty = nil;  // good
// or
[self setMyProperty:nil]; // also good

But setting the ivar to nil will leak:

self->myProperty = nil; // leaky as a sieve
// or 
myProperty = nil; // as useful as a screen door on a submarine

These are common errors.

Also note that setting properties to nil in -dealloc is a bad idea. As Kendall points out in the comments, you may unexpectedly invoke KVO behavior. There's a fuller discussion at Properties in dealloc: release then set to nil? or simply release.

Upvotes: 4

Related Questions