I have a class that has a property:
@property (nonatomic, assign) id customDatePicker
Based upon user choice I will need a UIDatePicker or a UIPicker so I typecast the ivar and retain it and subsequently dealloc it. Is this the correct way to manage the property’s memory?
// init snippet
if (useUIDatePicker) {
customDatePicker = (UIDatePicker *)[[[UIDatePicker alloc] initWithFrame:frame] retain];
} else {
customDatePicker = (UIPickerView *)[[[UIPickerView alloc] initWithFrame:frame] retain];
}
- (void)dealloc {
[customDatePicker release];
[super dealloc];
}
No.
When you declare the property as
assign, you should not be retaining the object. Theassignoption is used for non-object variables and for situations where having aretained property would create a cycle, with both objects retaining each other. Declaring a property asassignmeans you will not be managing the memory of the object; you should neither callretainnorreleaseon it.You are also over-retaining the picker object. Retaining an object creates a claim on the object; you don’t want it to disappear until you say you are done with it. You relinquish a claim, allowing an object to be deleted, by calling
release. When you callalloc, that creates the same kind of claim as callingretain. So this line:creates two claims, one for
allocand one forretain. Later, you only callreleaseonce, which means that you will always still have one claim on this object, and it will have turned into a memory leak.What you should do is:
Now you have only one claim on the picker because you used
alloc.(You don’t need to cast the assignment; when you are assign to a generic pointer you can use any kind of object.)
Also take a look at the Apple Memory Management docs.