ATG said:
Nope. It still crashes. I've tried both of your's, not that I understand your last statement about autoreleasing and class methods. Any other ideas?
While this may not be the cause of the crash, it is very important to understand the point he was making.
The difference between the two lines he gave was:
Code:
NSMutableArray * pfacArray = [[NSMutableArray alloc] initWithCapacity:100];
In this case, because you're calling the alloc method to allocate memory for the pfacArray, it's your responsibility to call its release method later to free it - otherwise it'll leak.
Code:
NSMutableArray *pfacArray = [NSMutableArray arrayWithCapacity:100];
In this case, you're not calling alloc (or copy), so it's not your responsibility to call the release method - if you do so, your app may well crash.
That's because the arrayWithCapacity method is autoreleasing the array it returns to you (i.e. instead of the last line in arrayWithCapacity: being something like "return newArray;" it's probably "return [newArray autorelease];").
However, if you want to hold on to the pfacArray returned (say, if you need to use it in later methods), you'll need to call it's retain method to ensure it doesn't get freed while you still need it, then call release once you're done with it.
Code:
// Create a new array via alloc..init
- (void) createNewArray1
{
NSMutableArray* myArray = [[NSMutableArray alloc] initWithCapacity:10] ;
// Do stuff with myArray
[myArray release] ;
}
/* Now, what if you want to return that array from that function? You can't release the array before you return it (or you'll be returning nothing!), so how do you make sure it doesn't get leaked? Use autorelease */
- (NSMutableArray*) createNewArray2
NSMutableArray* myArray = [[NSMutableArray alloc] initWithCapacity:10] ;
// Do stuff with myArray
return [myArray autorelease] ;
}
/* Or, you could just: */
- (NSMutableArray*) createNewArray3
NSMutableArray* myArray = [NSMutableArray arrayWithCapacity:10] ;
// Do stuff with myArray
return myArray;
}
/* Note, in createNewArray3, you didn't release or autorelease the array. That's because, you didn't create it (via alloc or copy), so you're not responsible for releasing it. */
Hope I've helped and not confused!