Objective C: Why does not my NSMutableArray work?


I'm quite new to ObjC and its mutable arrays. This is driving me crazy ;-)

I do the following:

mColumns = [NSMutableArray array];
for( int i=0; i<5; i++ ) [mColumns addObject:[[MyColumn alloc] init]];

After that code my array "mColumns" contains 5 elements. But each of them is a NULL-Pointer! (Or at least that's what the debugger tells me). I already checked that the code

[[MyColumn alloc] init]

Gives me some valid objects, so I have no idea why my array gets filled with 0x0s. Can you give me a hint?

Retain your mutableArray if you want it to stick around. At the end of the current event-loop it will be automatically deallocated, as it is in the autoreleasePool.

At that point all bets are off. Your mColumns variable just points to a junk piece of memory, maybe another object, maybe half an object, maybe even you can still get the correct count or even a contained object, but at some point your app will crash.

Just a quick point, if [[mColumns objectAtIndex:x] addObject:g]; crashes your app is it [mColumns objectAtIndex:x] that is causing the crash or is it addObject:g ?

Why not put them on separate lines and find out?