the_critic
the_critic

Reputation: 12820

initWithContentsOfFile is leaking like crazy

My application is completely leak-free (I think), but there is this one thing that supposedly leaks a whole lot. So here it is :

    mainPath = [[NSBundle mainBundle] bundlePath];
    levelConfigPlistLocation = [mainPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",CATEGORY]];
    levelConfig = [[NSDictionary alloc] initWithContentsOfFile:levelConfigPlistLocation];

And here is how I use it :

NSString *question = [[[levelConfig objectForKey:LEVELSETSTRING]objectForKey:LEVELSTRING]objectForKey:@"question"];

    questionLabel = [CCLabelTTF labelWithString:question dimensions:CGSizeMake(820.0f,360.0f) alignment:UITextAlignmentCenter fontName:@"helvetica" fontSize:32.0f];
    questionLabel.position = ccp(screenSize.width/2-30 ,screenSize.height);
    questionLabel.color = ccc3(255,255,255);
    [self addChild:questionLabel z:5];

and:

NSString *answer = [[[levelConfig objectForKey:LEVELSETSTRING]objectForKey:LEVELSTRING]objectForKey:@"answer"];

    for (int i=0; i < [answer length]; i++) 
    { 
        NSRange r = NSMakeRange(i, 1);
        NSString *ichar = [answer substringWithRange:r]; 
        [characters addObject:ichar]; 
    }

And, in -dealloc I release it:

[levelConfig release];

Instruments shows me the following leaks:

Screenshot Instruments

Which, when I double-click, always shows the same line, namely:

Screenshot Instruments 2

which is obviously a leakage of levelconfig... but what can I do ?

Upvotes: 0

Views: 159

Answers (1)

Rob Napier
Rob Napier

Reputation: 299275

You're directly accessing your ivars, which you should not do. If you replaced this with self.levelConfig =..., then very likely your leaks would go away.

Most likely the particular block of code you've provided here is somewhere like viewDidLoad or some other place that can be called multiple times. Every time you do that, you're leaking the old contents of levelConfig. Using self. will fix that. Avoid direct access to your ivars except in init and dealloc.

Upvotes: 1

Related Questions