objectAtIndex memory leak

Discussion in 'iOS Programming' started by littlealmo, Sep 13, 2010.

  1. littlealmo macrumors newbie

    Sep 13, 2010
    Hi all,

    I am running the following code which reads a simple csv file and then creates an object for each line and sets one value. When I run the code in instruments using leaks it creates a leak (NSCFString) for each line in the csv. I cannot understand how to stop this leak?

    - (void)viewDidLoad {
    	 [super viewDidLoad];
    	 buildingsArray  = [[NSMutableArray alloc] init];
    	NSBundle *bundle = [NSBundle mainBundle];
    	NSString *fileContents = [NSString stringWithContentsOfFile:
    					[bundle pathForResource:@"buildings" ofType:@"csv"]];
    	NSMutableArray *quizArray = [[NSMutableArray alloc] 
    					initWithArray:[fileContents componentsSeparatedByString:@"\n"]];	
    	for (int i = 0; i < [quizArray count]; i++) {				   
    		NSMutableArray *chunks = [[NSMutableArray alloc] 
    					initWithArray:[[quizArray objectAtIndex:i]  componentsSeparatedByString:@","]];
    		Building *a = [[Building alloc]init];
    		//the problem shows up here
    		a.name = [chunks objectAtIndex: 0];
    		[buildingsArray addObject:a];
    		[a release];
    		[chunks release];
    	[quizArray release];
    The leak shows up when I do
    a.name = [chunks objectAtIndex: 0];
    if I replace this line with
    a.name = @"test";
    then there is no leak?
  2. jnic macrumors 6502a

    Oct 24, 2008
    viewDidLoad can be called more than once, but buildingsArray never gets released and is not using a setter property, so each time viewDidLoad gets called you leak memory.
  3. littlealmo thread starter macrumors newbie

    Sep 13, 2010
    hi jnic,

    Thanks for the reply I am using viewDidLoad just to highlight the issue I have put the code into a UITableView within a navigation view, I call the table view once and then go back one level in the navigation. I have set this up just to test this problem in leaks. The buildingsArray is released in dealloc. So even when i call viewDidLoad only once then leave I see the memory leak. In the building object I have set the "name" as
    @property(nonatomic, copy) NSString *name;
    . So I am not sure why I get the leaking of strings?
  4. littlealmo thread starter macrumors newbie

    Sep 13, 2010
    here is the leaks screenshot generated for each loop through
    a.name = [chunks objectAtIndex: 0];

    Attached Files:

  5. bredell macrumors regular

    Mar 30, 2008
    Uppsala, Sweden
    You're allocating a Building object, setting the name property to a string, and then finally releasing the Building object. Have you made sure that you're releasing the name property in the dealloc method of the Building class?
  6. littlealmo thread starter macrumors newbie

    Sep 13, 2010

    Hi bredell,

    You where exactly right I was not releasing the name property in the Building object. I added the code and the leak has gone away. Thanks for your help with this it was driving me mad. I did not realise that object dealloc method has to release there properties. Anyway thanks again for taking the time to look at this.

Share This Page