views:

68

answers:

3

Any idea why this code gives me a memory leak? As you can see, I'm running out of ideas as to what I can do to stop it.

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    NSArray *allSketches = [project.sketches allObjects];
    NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:allSketches];

    if(sketchesArray != nil) [sketchesArray release];

    [self setSketchesArray:temp];

    [allSketches release];
    allSketches = nil;
    [temp release];
    temp = nil;

}

I also release sketchesArray inside viewDidDissapear. I'm not using viewDidLoad and dealloc to init/release these objects as what I am doing requires me to use viewWillAppear and viewDidDissapear.

Thanks

A: 

Is this being released somewhere else when you are done with it?

[self setSketchesArray:temp];

Specifically, you are releasing sketchesArray in this function, but do you do that elsewhere when you are done with the view?

blu
yes, it's released in dealloc. it works now, by the way. See above :)
Marian Busoi
+1  A: 

Fixed it by using this instead:

NSArray *allSketches = [project.sketches allObjects];
NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:allSketches];

[self setSketchesArray:temp];

[temp release];

Though I remember doing that and it didn't work before... Strange... There appear to still be some memory leaks coming from CoreGraphics though. Is that normal?

Marian Busoi
A: 

I can't see the leak, but you've got a couple of probable over-releases.

The release of a non-nil sketchesArray should be managed inside setSketchesArray. And it doesn't look like you have local ownership of allSketches either...

walkytalky
you are right, thanks. I'm very new to this and still not used to the memory management system.
Marian Busoi