开发者

leak problem on NSUserDefaults and NSMutableArray

开发者 https://www.devze.com 2023-01-08 10:17 出处:网络
I have a leak in the following code: - (void)viewDidAppear:(BOOL)anim开发者_如何学JAVAated { //If Home-Theme

I have a leak in the following code:

- (void)viewDidAppear:(BOOL)anim开发者_如何学JAVAated {
//If Home-Theme
    if (themeIndex == 0) {
        NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
        NSMutableArray *thisArray = [[NSMutableArray alloc] init];
        thisArray = [[pref objectForKey:@"Themes"] mutableCopy];
        [thisArray release];
    }
}

the leak is at NSMutableArray. I have try'd some different workarounds but nothing is help. Maybe there is someting wrong with the NSUserDefaults? any ideas?

thank you xnz


NSMutableArray *thisArray = [[NSMutableArray alloc] init];

That is leaking since you never release that instance, just assign a new one in the next line. Replace it with:

NSMutableArray *thisArray = [[pref objectForKey:@"Themes"] mutableCopy];


You are allocating a NSMutableArray and the changing the reference to another array.

You probably want something like this:

- (void)viewDidAppear:(BOOL)animated {
  //If Home-Theme
  if (themeIndex == 0) {
    NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
    NSMutableArray *thisArray = [[pref objectForKey:@"Themes"] mutableCopy]];
    // do something with thisArray
    [thisArray release];
  }
}


you alloc thisArray and then overwrite the reference to it with a mutable copy from pref. Either do autorelease or just remove the unnecessary NSMutableArray allocation

0

精彩评论

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