PDA

View Full Version : How to know if object needs release




mikezang
Aug 16, 2010, 07:30 PM
I know that I have to release object if I init a object. But I am not sure if some other cases. For example, do I have to release controllers? Is there a simple rule to decide when have to release or not?

NSMutableArray *controllers = [arrayWithArray:controller.viewControllers];
// modify controllers here
controller.viewControllers = controllers



Luke Redpath
Aug 16, 2010, 07:37 PM
Have you read the Apple guide to memory management? It covers this in great detail. If not, I suggest you do then come back if you then have any questions or need anything clarifying.

I could summarise the memory management rules for you (they aren't that complicated) but you'd learn more from the Apple docs.

zerocustom1989
Aug 16, 2010, 07:37 PM
Im not sure what that code you posted says or does, but it's not "Init" that means you need to release it's "alloc".

Yes sometimes alloc is followed by init, but alloc is the main concern.

Alloc means you're allocating memory for the object. Release means you give it back. Init just initializes an already allocated object.

For example:

NSNumber *number = [[NSNumber alloc] initWithInt:4]; That needs to be released.

This:

number = [NSNumber numberWithInt:4]; this does not need to be released.

mikezang
Aug 16, 2010, 07:45 PM
Have you read the Apple guide to memory management? It covers this in great detail. If not, I suggest you do then come back if you then have any questions or need anything clarifying.
Off course, I read it.

But I am just not sure in practice for detail, I also found some books they didn't release, those confused me.

Luke Redpath
Aug 16, 2010, 07:47 PM
Off course, I read it.

But I am just not sure in practice for detail, I also found some books they didn't release, those confused me.

What exactly wasn't clear to you from the documentation? Ask a specific question and I will try and help.

mikezang
Aug 16, 2010, 07:47 PM
Im not sure what that code you posted says or does, but it's not "Init" that means you need to release it's "alloc".

Yes sometimes alloc is followed by init, but alloc is the main concern.

Alloc means you're allocating memory for the object. Release means you give it back. Init just initializes an already allocated object.

For example:

NSNumber *number = [[NSNumber alloc] initWithInt:4]; That needs to be released.

This:

number = [NSNumber numberWithInt:4]; this does not need to be released.
So if I don't use alloc, that mean I also don't need release it, for my example, no release needs, may I make sense?

mikezang
Aug 16, 2010, 07:49 PM
What exactly wasn't clear to you from the documentation? Ask a specific question and I will try and help.
This is my specific question as I asked, do I have to release controlles?
NSMutableArray *controllers = [NSArray arrayWithArray:controller.viewControllers];
// modify controllers here
controller.viewControllers = controllers

Luke Redpath
Aug 16, 2010, 07:53 PM
Well that depends entirely on the context. Did you alloc the controller instance, or retain it somewhere? If so, then yes. A small snippet of code out of context isn't really much to go on.

mikezang
Aug 16, 2010, 07:59 PM
It is not my attention about controller, because I know what I should do for it.

I want to know if I have to release controllers, all code there about controllers.

Luke Redpath
Aug 16, 2010, 08:05 PM
It is not my attention about controller, because I know what I should do for it.

I want to know if I have to release controllers, all code there about controllers.

I'm assuming the code isn't taken directly from your app as the first line isn't syntatically correct. Assuming it should be [NSArray arrayWithArray:] then the answer is no, as this method neither does not start with alloc, new or copy. In this case you can assume the method returns an autoreleased object so unless you explicitly retain it, then you do not need to release it.

mikezang
Aug 16, 2010, 08:09 PM
I'm assuming the code isn't taken directly from your app as the first line isn't syntatically correct. Assuming it should be [NSArray arrayWithArray:] then the answer is no, as this method neither does not start with alloc, new or copy. In this case you can assume the method returns an autoreleased object so unless you explicitly retain it, then you do not need to release it.
Sorry I made a mistake, you are right.
Thanks for your answer, I am more sure before post this thread:)