2011-09-30 41 views
0

我有以下代码,其中每个对象都是UISwitch IBOutlet属性。我也不确定为什么当使用xcode分析器时,我为每一行都收到了内存泄漏警告。针对UISwitch的Stactic分析器内存泄漏警告

- (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

哪一行是你得到的警告,是你的代码看起来像什么,或者你是否将它编辑为StackOverflow - 你的updateButtonClicked方法只是创建一个NSMutableDictionary然后抛出它。 –

+0

myUISwitch1和myUISwitch2如何声明@properties,如果是这样的话,还有whoa属性。可能提供的代码将是最好的。 – zaph

+0

他们是保留和nonatomic属性proerties。它们在dealloc中发布。代码被压低了一点,之后NSMutableDictionary被上传到服务器。 – Jon

回答

2

很多挠头的后...

按照惯例,包含单词“复制”任何Objective C的方法有望复出保留的对象。这同样适用于方法前缀'init'和'new'。

静态分析器知道这个约定,并且抱怨说copyEntirePreviousNoteButtonClicked方法没有返回保留的对象。

解决方案不是为您的方法命名为“复制”,除非您确实是这个意思。坚持Objective C方法命名约定。改变你的方法的名称,问题就会消失。

+0

Objective-C使用名称来了解每种方法的上下文?我认为多数民众赞成为什么关键字被发明......所以我们不会结束像'functionReturnsIntOutputTakesIntInput {输出=输入; }' – Dani

+0

是的,听起来很奇怪,这正是它所做的。 Google针对“Objective C命名约定”或查看Clang文档。 –

+0

方法命名约定也用于键值编码中的getter和setter。 –