Weird Memory Leak

Discussion in 'iOS Programming' started by Lakario, Nov 4, 2008.

  1. Lakario macrumors member

    Oct 29, 2008
    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;
    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...
  2. walty macrumors member

    Jul 15, 2008
    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.
  3. Lakario thread starter macrumors member

    Oct 29, 2008
    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.
  4. Luke Redpath macrumors 6502a

    Nov 9, 2007
    Colchester, UK
    Does Instruments give you any useful data if you drill down a bit?

Share This Page