I know this is a common problem, so I need an explanation so I don’t keep having this problem. In my header file, I have defined a UIAlertView and retained it as shown:
@interface myController {
UIAlertView *alert;
}
@property (nonatomic, retain) UIAlertView *alert;
In my implementation, I use and reuse this alert as follows:
@synthesize alert;
...
if (self.alert != nil) {
[self.alert release];
}
self.alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: @"Ok To Send", nil];
[self.alert show];
I also release this in my dealloc.
So, I’ve heard the golden rule on memory management, but I apparently don’t understand it. The golden rule says that you must never release an object that you have not either retained or obtained via alloc. You must always eventually release an object that you have either retained or obtained via alloc.
I retained it in the header file, so I eventually must release it in the dealloc. In my implementation, I perform an alloc of the alert object more than once, so each time I get ready to re-alloc it, I release the old one.
Please help me understand what I am misunderstanding.
The
@propertywithretainspecified is implemented something like this…So by assigning a new value to the property, the property will handle the
releaseof the previous value… so when you manuallyreleaseit, you’re over-releasing.Also, since you have the
@propertyset toretain, you shouldautoreleasebefore assigning to the property: