views:

80

answers:

4

Hello.

I'm developing an iPhone application, and I very new on iPhone development.

I've created some custom classes with instance variables (NSArray, NSString, etc.). All classes inherits from NSObject.

Should I create a dealloc method to release all instance variables?

Thank you.

+3  A: 

Yes, you definitely need a dealloc if you are keeping instance variables that are objects. You will also probably need to retain or copy those as well, depending on how your object creates/uses them.

Check out this article on memory management. I think it explains it pretty well. You must also read the Memory Management Programming Guide for Cocoa. Even if you don't fully understand everything, read the whole thing through, then read the article, then do some work, get some crashes and read it again :) eventually it should all click.

Jason Coco
Not really, no. Nothing about ' keeping instance variables that are objects' dictates you have a dealloc method.
mustISignUp
@mustISignUp: Unless they are simply assigned or all singletons, then you will. And since objects as instance variables that should be assigned (such as delegates) and references to singletons are the rare cases, then yes, you will have to have a dealloc method. My wording may have been slightly too strong, but beginners should be in the habit of writing proper tear-down methods.
Jason Coco
It is misleading. It sounds like the dealloc method is special - it isnt.
mustISignUp
@mustISignUp: It is special, it is the destructor for Cocoa and it is a fundamental part of managed memory in Cocoa. Even if you don't keep ownership of any object ivars, it's still good practice to simply assign them nil in dealloc because then you've clearly considered the memory implications. He is using Cocoa in a managed memory environment, so while he /could/ do non-stanard things, the answer to his question is definitely yes.
Jason Coco
@Jason - What i'm saying is that you seem to be misleading @VansFannelAre that dealloc is special in that this is the one method in Cocoa that it is a RULE that you must have. Like a subclass of NSObject without an overridden dealloc method should not really compile - only those Apple peeps don't know how to write a compiler properly. It might help @VansFannel's understanding to know what actually happens.
mustISignUp
A: 

In iPhone development its pretty much SOP to have a dealloc since there is no garbage collection.

Anders K.
A: 

You have to release any object your class has ownership for. That means, yes you have to overwrite the dealloc method and release the objects there.

Normally you have ownership over values (objects) in instance variables, but it also depends own how you create them.

You should definitely read the Memory Management Programming Guide, it describes pretty well when you gain ownership.

Felix Kling
You do not HAVE to release and object your class has ownership for (that doesnt even acually mean anything.). If you WANT to release an object you certainly do not HAVE TO do it by overwriting(sic) dealloc.
mustISignUp
@mustISignUp: You are right, my wording is not good. Of course you can release an object anytime you want. But I still think you have to release it if you have ownership, meaning you retained the object (hence increasing the retain count). Besides garbage collection (which does not exist on the iPhone), how would the retain count decreased otherwise?
Felix Kling
A: 

Yes, having a dealloc method is normally the best way.

If you want to reclaim memory used by your instance variables you will have to release them when you are done with them. You could add a method to do this clean up:-

- (void)cleanUp {
  [myArray release];
  [myString release];
}

Call it when you no longer need the instances.

Now, the chances are that the point in time when you want to release these variables is the point in time when their parent object is destroyed (parent object is gone, so instance variables are no longer needed). As -dealloc is automatically called for you when the parent object is going to be destroyed - it makes more sense to put the cleanup code in dealloc than in our -cleanup method that we have to call at the right time.

If you don't want to reuse the memory, eg. if you are never going to be finished with the instance variables, then you don't need to release them and might not need a -dealloc.

mustISignUp
.. And you would want to set those instance variables to `nil`, otherwise you will double-dealloc them when the real dealloc is called.
Jason
errrrrrr, it was more of an example of what not to do!
mustISignUp