Quote:
Originally Posted by aceallways
Thanks you so much!
I just release it on dealloc as it is used throughout the life of the view, is this acceptable?
Thanks again!
|
If the code you posted is run once and only once in the life of an object, you're ok. However, if that code is ever run more than once, you will have a memory leak.
Say you put the code you posted in a view controller's viewDidLoad method.
Code:
lastName=[[formatter stringFromDate: [datePicker date]] copy];
The first time through, lastName is nil when that statement executes. Then, later in the life of the app, the system generates a low memory warning and the view controller unloads it's view. The next time you try to display the view controller's view, it calls viewDidLoad a second time.
This time, there is already an object in the lastName instance variable. You execute the assignment above, and the old value in lastName is replaced with a new date string, but you never release the old object, thus leaking it.
You would be better off setting up lastName as a retained (or copied) property). Then, use the syntax:
Code:
self.lastName=[formatter stringFromDate: [datePicker date]];
The setter for the lastName property takes care of retaining (or copying) the string, but first it releases any old value stored there.
Then, in your dealloc method, just add:
Code:
self.lastName = nil;
Properties take care of a lot of the details of memory management for you, meaning you don't have to write a bunch of error-prone memory management code.