UISwitch 的静态分析器内存泄漏警告

发布于 2024-12-07 09:06:42 字数 1988 浏览 1 评论 0原文

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

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)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];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆一缕阳光ご 2024-12-14 09:06:43

经过一番绞尽脑汁之后...

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

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

解决方案是不要在方法命名中包含“复制”一词,除非您确实这么想。坚持 Objective C 方法命名约定。更改方法的名称,问题就会消失。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文