Releasing an Object After Adding it to an Array Causes App to crash

Discussion in 'iOS Programming' started by DarkNemesis618, Oct 16, 2008.

  1. DarkNemesis618 macrumors newbie

    Joined:
    Jun 6, 2008
    #1
    While setting up a table view for my app, we go through an array of nodes adding them to a class and then adding several of these objects into an array that drives the creation of the cells. The problem is is that these class objects are leaking memory. I try releasing them at the end of the for loop, but when I do that and try to load the table view, the app crashes...what am I doing wrong?

    Code:
    NSMutableArray *myArray = [NSMutableArray array]; 	
    if ([nodes count] > 0 ) 
    {
    	for(NSInteger iCounter=0;iCounter<[nodes count];iCounter++)
    	{
    		MyClass classObject = [[MyClass alloc] init];
    		classObject.value1 = someValue1;
    		classObject.value2 = someValue2; 
    		classObject.value3 = someValue3;
    
    		[myArray addObject:classObject];
    		
    		[classObject release];
    	}
    }
    
    self.list = myArray ;
    
    list is a global variable for the tableviewcontroller

    This is where it fails at, in the cellForRowAtIndexPath function

    Code:
    if(cell == nil)
    	cell = [[[MyClassViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    
    ((MyClassViewCell *)cell).myclass =  (MyClass *)[self.list objectAtIndex:indexPath.row];
    
     
  2. xsmasher macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #2
    I don't see the problem, unfort.
    I assume your properties are declared as "retain?" ( all 4 of them?)

    Either way, try to NSlog the length of your array right before using it in the 2nd snippet. That'll narrow the problem - is it the array, or a problem with it's contents.
     

Share This Page