开发者

Stactic Analyizer Memory Leak Warning for UISwitch

开发者 https://www.devze.com 2023-04-09 11:54 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号