views:

61

answers:

1

In a branch of my code, I previously used this

NSMutableArray *array1 = [[NSMutableArray alloc] init];

The above array is used populate a UITableVew.

Just cause, I switched to the following:

NSMutableArray *array1 = [NSMutableArray arrayWithCapacity:0]

I made no other changes to my code) and my app crashes whenever I try to scroll down the list in the UITableView.

It looks like my array is not initialized correctly. Can someone explain why this would happen? Are the two methods not identical wrt how the underlying memory space is allocated?

+1  A: 

Your second line of code is not retaining the NSArray, which is causing a crash. You'll need to call [array1 retain] after you call arrayWithCapacity:.

There's quite a bit of useful information in this post: Understanding reference counting with Cocoa / Objective C

In general, if you're calling a class method that doesn't start with "new" or "init" (e.g. arrayWithCapacity), you can usually assume that the returned object will be autoreleased.

pix0r