tags:

views:

65

answers:

3

I have a leak in the following code:

- (void)viewDidAppear:(BOOL)animated {
//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

+1  A: 
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];
willcodejavaforfood
Thank you! this is working...
xnz
+1  A: 

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];
  }
}
Olly
Your sample is missing a `release`.
Georg Fritzsche
@George Fritszsch, thanks. I've updated the code sample.
Olly
A: 

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

ULysses