Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Lakario

macrumors member
Original poster
Oct 29, 2008
30
0
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:

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:

Code:
#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:
Code:
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

macrumors member
Jul 15, 2008
33
0
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:

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:

Code:
#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:
Code:
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

macrumors member
Original poster
Oct 29, 2008
30
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.