Weird Memory Leak

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

  1. Lakario
    Expand Collapse
    macrumors member

    Joined:
    Oct 29, 2008
    #1
    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...
     
  2. walty
    Expand Collapse
    macrumors member

    Joined:
    Jul 15, 2008
    #2
    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
    Expand Collapse
    thread starter macrumors member

    Joined:
    Oct 29, 2008
    #3
    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
    Expand Collapse
    macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #4
    Does Instruments give you any useful data if you drill down a bit?
     

Share This Page