2

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Zigglzworth
  • 6,645
  • 9
  • 68
  • 107

4 Answers4

5

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
  • 48,104
  • 19
  • 94
  • 118
  • 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.. – Zigglzworth Aug 30 '10 at 15:33
  • Do you retain that dictionary anywhere else? – kubi Aug 30 '10 at 15:37
  • 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.. – Zigglzworth Aug 30 '10 at 16:21
  • 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. – Zigglzworth Aug 30 '10 at 18:08
0

From 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.

Community
  • 1
  • 1
Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
0

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
  • 14,911
  • 1
  • 36
  • 54
0

I had the same issue as Zigglzworth and it was the position of the [super dealloc] call. I had it at the start of my -(void)dealloc method and it was causing a crash every time. Moved [super dealloc] to the end of the method after the variable release statements and now it works just fine.

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
timv
  • 3,346
  • 4
  • 34
  • 43