Release object.

Discussion in 'Mac Programming' started by larswik, Jul 6, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    In my little calculator app I have 2 NSMutableStrings that I instantiated. All the code was written in the app AppController. Should I release these objects from Main.m or within the AppController.m , or does it matter?

    -Lars
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    It's best to think about this in terms of which objects own which other objects. Any object that owns something needs to retain it (either by allocating it in the first place, copying it, or retaining it). Any object that no longer needs to own an object needs to release it (which includes when the owning object deallocates). An object can be owned by more than one thing at once.

    If it's not clear in your code what owns what, you should probably fix it so it is clear.
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    I will post my Calculator code when I finish. In the -(void)wakeFromNib I alloc and init an NSMutableString

    valueAppender = [[NSMutableString alloc] initWithString:mad:""];

    ValueAppendar is used a lot in the program. So I guess it just need to be released when I quit the application.

    Thanks for the input.

    -Lars
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    Make it a @property (readwrite, retain). In dealloc, assign nil (to the property, _not_ to the member variable) . Don't think in special cases (like "when I quite the application"). Think about ownership, and code patterns. The combination @property (readwrite, retain) and assigning nil to the property in dealloc is a simple pattern that works absolutely fine.
     

Share This Page