I have upgraded my XCode to versio 3.2.3 to support iOS4 on my iphone project. using the static analyser I checked for memory management problems.
In one of my routines I get the following problem:
I generate a user alert after adding an event to the calendar to give him a status.
This runs fine, but the memory analyser doesn’t like how I defined the alert.
I can’t see the coding problem, do you? (I indicated the memory analyser hints with “<<<<“)
- (IBAction) addToCalendar {
...
UIAlertView *tmpAlert = [UIAlertView alloc]; <<<<Method returns an Objective-C object with a+1 retain count (owning reference)
calData.startDate = iVar.zeitVon;
calData.endDate = iEvent.zeitBis;
calData.title = iVar.title;
calData.calendar = myEventStore.defaultCalendarForNewEvents;
if ([tmpEventStore saveEvent:tmpEvent span:EKSpanThisEvent error:&tmpSaveError]) {
// Show a save success dialog
[tmpAlert initWithTitle:@"Success" <<<<Object released
message:@"entry saved" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
} else {
// Show a save error dialog
[tmpAlert initWithTitle:@"Error"
message:@"entry not saved" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] ;
}
[tmpAlert show]; <<<<Reference counted object is used after its released
[tmpAlert release];
}
thanks
You should never decouple
allocandinit.initoften changes the object behind the scenes! TryYou’ll see something like
This shows that
+[NSString alloc]doesn’t really allocate anything; rather, what does the job isinitWithStringitself. I don’t thinkUIAlertViewdoes this, but you never know.To recap: never decouple
allocandinit. I think the static analyzer just assumes that everyone use[[... alloc] init], so that it got confused by your code. The analyzer should have warned you not to decoupleallocandinit.