【问题标题】:Stactic Analyizer Memory Leak Warning for UISwitchUISwitch 的静态分析器内存泄漏警告
【发布时间】:2011-09-30 22:15:05
【问题描述】:

我有以下代码,其中每个对象都是 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];
    }
}

【问题讨论】:

  • 你在哪一行得到警告,你的代码真的是这样吗?或者你是否为 StackOverflow 编辑了它 - 你的 updateButtonClicked 方法只是创建一个 NSMutableDictionary 然后把它扔掉。
  • myUISwitch1 和 myUISwitch2 是如何声明的,@properties,如果是这样的话,带有 whoa 属性。最好提供代码。
  • 它们是具有保留和非原子属性的属性。它们在 dealloc 中释放。代码有点笨拙,NSMutableDictionary 之后上传到服务器。
  • @RobinSummerhill 我在上面添加了完整的代码。我在上面的几乎每一行都收到错误Potential leak of object at line

标签: iphone objective-c xcode memory-management uiswitch


【解决方案1】:

经过多次挠头......

按照惯例,任何包含单词“copy”的 Objective C 方法都应该返回一个保留对象。这同样适用于方法前缀“init”和“new”。

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

解决方案是不要为您的方法命名包含“复制”一词,除非您是认真的。坚持 Objective C 方法命名约定。更改方法的名称,问题就会消失。

【讨论】:

  • Objective-C 使用名称来知道每个方法的上下文?我认为这就是发明关键字的原因......所以我们最终不会得到像functionReturnsIntOutputTakesIntInput { Output = Input; }这样的东西
  • 是的,听起来很奇怪,这正是它的作用。谷歌“Objective C 命名约定”或查看 Clang 文档。
  • 方法命名约定也用于键值编码中的getter和setter。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
相关资源
最近更新 更多