PDA

View Full Version : Weird Memory Leak




Lakario
Nov 4, 2008, 01:50 PM
I am having some very unfortunate behavior with my program working for a few actions and then suddenly it crashes without a decent bit of useful information. I've been looking into memory leaks and from the looks of it, I only have a couple of small ones. I tracked one of them down to this piece of code:


if(self.itemListController == nil) {
ItemListController *controller = [[ItemListController alloc] init];
self.itemListController = controller;
[controller release];
}

NSMutableArray *itemArray = [[NSMutableArray alloc] initWithObjects:sword, shield, subligar, nil];
self.itemListController.items = itemArray;

[sword release];
[shield release];
[subligar release];
[itemArray release];


ItemListController is defined as such:


#import <UIKit/UIKit.h>
#import "Item.h"


@interface ItemListController : NSObject {
NSMutableArray *items;
Item *defaultItem;
}

@property (nonatomic, retain) NSMutableArray *items;

-(void)setDefaultItem:(Item *)item;

@end


The exact line of the leak is:

self.itemListController.items = itemArray;

If I comment/remove that line the leak goes away.

Can anyone think of a reason why that would cause a leak? It doesn't seem like I'm doing anything out of the ordinary...



walty
Nov 4, 2008, 11:17 PM
I am having some very unfortunate behavior with my program working for a few actions and then suddenly it crashes without a decent bit of useful information. I've been looking into memory leaks and from the looks of it, I only have a couple of small ones. I tracked one of them down to this piece of code:


if(self.itemListController == nil) {
ItemListController *controller = [[ItemListController alloc] init];
self.itemListController = controller;
[controller release];
}

NSMutableArray *itemArray = [[NSMutableArray alloc] initWithObjects:sword, shield, subligar, nil];
self.itemListController.items = itemArray;

[sword release];
[shield release];
[subligar release];
[itemArray release];


ItemListController is defined as such:


#import <UIKit/UIKit.h>
#import "Item.h"


@interface ItemListController : NSObject {
NSMutableArray *items;
Item *defaultItem;
}

@property (nonatomic, retain) NSMutableArray *items;

-(void)setDefaultItem:(Item *)item;

@end


The exact line of the leak is:

self.itemListController.items = itemArray;

If I comment/remove that line the leak goes away.

Can anyone think of a reason why that would cause a leak? It doesn't seem like I'm doing anything out of the ordinary...

Since u used retain property for items, the reference count would be incremented when u assign to it, I think u should add a release of items in the "dealloc". The release of "items" should trigger the release of everything in the "items" array as well.

Lakario
Nov 5, 2008, 02:32 AM
Since u used retain property for items, the reference count would be incremented when u assign to it, I think u should add a release of items in the "dealloc". The release of "items" should trigger the release of everything in the "items" array as well.

I made sure items and defaultButton were released in the ItemListController, but I don't believe that to be the source of the problem as the leak happens as soon as the program loads.

Luke Redpath
Nov 6, 2008, 11:39 AM
Does Instruments give you any useful data if you drill down a bit?