Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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
 
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

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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.