views:

94

answers:

3

Hi,

I have a Class which I have created as an NSObject. This class has a number of properties of different types and methods etc.

When I instantiate this class in my App (say in the main View Controller) I immediately send it a release call when I am finished using it. ie:

MyObject *myObject = [[MyObject alloc] initWithParameters:parms];
[myObject doSomeMethodCall];
[myObject release];

So my question is: When I release myObject, does it automatically release all of the declared objects, variables, etc. that I declared in the MyObject .h file?

OR

Do I need to create a custom release method which releases all of these?

I ask because of memory management issues.

Thank you.

A: 

From http://stackoverflow.com/questions/577635/iphone-when-is-dealloc-for-a-viewcontroller-called:

Dealloc will run when the last reference to an object has been released.

so when you release your object it will run dealloc. So put all your releases and such into your object's dealloc method.

Thomas Clayson
A: 

Eventually, it will call the dealloc method on the myObject. In the dealloc method of myObject, you should release all the instance variables that myObject has. Also, don't forget the [super dealloc]

Rengers
+3  A: 

You need to implement a dealloc method in your object and use that method to release any resources that you own.

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-SW4

- (void)dealloc {
    [mainSprocket release];
    [auxiliarySprocket release];
    [super dealloc];
}

Important note: you never call a dealloc method on an object, it's invoked automatically by the runtime when it's time to clean up.

kubi
This is good but I have a NSDictionary object that when I release it in the -(void)dealloc call, it crashes my app... I can't figure out why..
Do you retain that dictionary anywhere else?
kubi
No but I assign values from the dictionary to other objects such as strings. These other objects are released anyway before I try to release the NSDictionary but it still crashes the app. Also, when I put the [super dealloc] in my -(void)dealloc method it crashes my app. The funny thing is that it only crashes my app if I am not running it with the Debugger. If I run it with debugger and have the [super dealloc] calls it does not crash. strange..
I figured out the problem regarding the [super dealloc] and NSDictionary. Its a bit complicated to post and really has more to do with some errors I made. anyway this all works good in my app now.