开发者

Releasing custom made NSObject class in iOS App

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

开发者_StackOverflowDo I need to create a custom release method which releases all of these?

I ask because of memory management issues.

Thank you.


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.


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.


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]


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜