Stactic Analyizer Memory Leak Warning for UISwitch
I have the following code where each object is a UISwitch IBOutlet property. I'm nor sure why I am getting a memory leak warning for each line when using xcode analyzer.
- (IBAction)copyEntirePreviousNoteButtonClicked:(id)sender
{
self.myUISwitch1.on = TRUE;
self.myUISwitch2.on = TRUE;
}
- (IBAction)updateButtonClicked:(id)sender
{
NSMutableDictionary *copyOptions = [[[NSMutableDictionary alloc] init] autorelease];
if (self.myUISwitch1.on) {
[copyOptions setValue:@"ON" forKey:@"myUISwitch1"];
}
if (self.myUISwitch2.on) {
[copyOptions setValue:@"ON" forKey:@"myUISwitch2"];
}
}
Update with full code:
@property (nonatomic, retain) IBOutlet UISwitch *copy_hp_cchpi;
@property (nonatomic, retain) IBOutlet UISwitch *copy_hp_history;
- (IBAction)copyEntirePreviousNoteButtonClicked:(id)sender
{
self.copy_hp_cchpi.on = YES;
self.copy_hp_history.on = TRUE;
}
- (IBAction)updateButtonClic开发者_如何学运维ked:(id)sender
{
NSMutableDictionary *copyOptions = [[[NSMutableDictionary alloc] init] autorelease];
if (self.copy_hp_cchpi.on) {
[copyOptions setValue:@"ON" forKey:@"copy_hp_cc_history_present_illness"];
}
if (self.copy_hp_history.on) {
[copyOptions setValue:@"ON" forKey:@"copy_hp_med_fam_social_history"];
}
int rcode = [MyAPIDataSource copyPreviewAppointmentClinicalInfo:[MyAPIDataSource getCurrentAppointmentId] copyOptions:copyOptions];
if (rcode)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to copy last appointment information. Please try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
//Send Notifications to other screens that clinical info was copied from last appointment to current one.
[[NSNotificationCenter defaultCenter] postNotificationName:@"LastAppointmentLoadedHandler" object:self];
[self dismissModalViewControllerAnimated:YES];
}
}
After a lot of head scratching...
By convention, any Objective C method that contains the word 'copy' is expected to return a retained object. The same applies for the method prefixes 'init' and 'new'.
The static analyzer knows about this convention and is complaining that your copyEntirePreviousNoteButtonClicked method does not return a retained object.
The solution is not to name your methods containing the word 'copy' unless you really mean it. Stick to the Objective C method naming conventions. Change the name of your method and the problem will go away.
精彩评论