PDA

View Full Version : Releasing an Object After Adding it to an Array Causes App to crash




DarkNemesis618
Oct 16, 2008, 03:05 PM
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?


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


if(cell == nil)
cell = [[[MyClassViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];

((MyClassViewCell *)cell).myclass = (MyClass *)[self.list objectAtIndex:indexPath.row];



xsmasher
Oct 16, 2008, 09:07 PM
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.