我有以下代码,其中每个对象都是UISwitch IBOutlet属性。我不确定为什么在使用xcode分析器时我为每条线路收到内存泄漏警告。
- (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"];
}
}
使用完整代码进行更新:
@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)updateButtonClicked:(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];
}
}
答案 0 :(得分:2)
经过大量的搔痒......
按照惯例,任何包含“copy”一词的Objective C方法都应该返回一个保留的对象。这同样适用于方法前缀'init'和'new'。
静态分析器知道这个约定并抱怨你的copyEntirePreviousNoteButtonClicked方法没有返回一个保留的对象。
解决方案不是为包含“copy”一词的方法命名,除非你真的是这个意思。坚持Objective C方法命名约定。更改方法的名称,问题就会消失。