tags:

views:

49

answers:

2

Iam having a very basic doubt in memory management. If suppose iam allocating memory for an object in viewWillAppear method. Should i release the object in viewWillDisappear method or in the release all the objects in the de

+1  A: 

It's dependent when you want to release the object. You don't have to release on viewWillDisappear. But, you can, just think about when you need it and when you don't. Technically, either one is fine. Depending on the situation though I would think: if you need the object for multiple views don't dealloc in viewWillDisappear, if you need it only for that view and you don't need it again, dealloc in viewWillDisappear.

thyrgle
If you choose to create objects in viewWillAppear, always make sure they don't exist yet by comparing the pointers to nil. Whenever you release them, set the pointers to nil. You have to be prepared for viewWillAppear being sent multiple times.
Costique
A: 

Here's a very easy to learn tutorial on objective-c memory management.

http://cocoadevcentral.com/d/learn_objectivec/

You'll learn a lot about retaining and releasing variables. In general variables are defined according to scope (i.e where they will be used) For example, you may want to initialize a variable that's used throughout a class in viewDidLoad and release it in dealloc. It all depends on where you need to store data and for how long.

Jordan
Thanks a lot for the tutorial link. It has cleared many of my confusion i had regarding memory management.
Swapna