How to know if object needs release

Discussion in 'iOS Programming' started by mikezang, Aug 16, 2010.

  1. macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    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?
    Code:
    NSMutableArray *controllers = [arrayWithArray:controller.viewControllers];
    // modify controllers here
    controller.viewControllers = controllers
    
     
  2. macrumors 6502a

    Joined:
    Nov 9, 2007
    #2
    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.
     
  3. macrumors regular

    Joined:
    Sep 5, 2007
    #3
    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.
     
  4. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #4
    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.
     
  5. macrumors 6502a

    Joined:
    Nov 9, 2007
    #5
    What exactly wasn't clear to you from the documentation? Ask a specific question and I will try and help.
     
  6. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #6
    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?
     
  7. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #7
    This is my specific question as I asked, do I have to release controlles?
    Code:
    NSMutableArray *controllers = [NSArray arrayWithArray:controller.viewControllers];
    // modify controllers here
    controller.viewControllers = controllers
    
     
  8. macrumors 6502a

    Joined:
    Nov 9, 2007
    #8
    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.
     
  9. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #9
    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.
     
  10. macrumors 6502a

    Joined:
    Nov 9, 2007
    #10
    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.
     
  11. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #11
    Sorry I made a mistake, you are right.
    Thanks for your answer, I am more sure before post this thread:)
     

Share This Page