2

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?

asked Nov 20, 2010 at 19:49
5
  • 1
    NSMutableArray can't hold a Null pointer. Somehow you are interpreting the debugger incorrectly. You might want to be more specific about what is making you think they are Null. There is nothing wrong with the code. Commented Nov 20, 2010 at 19:56
  • 2
    Don't forget to release those columns. You allocked them, so you need to release them. Remember that a collection owns the objects that are in it, so they won't die off as long as they're in the array. developer.apple.com/mac/library/documentation/Cocoa/Conceptual/… Commented Nov 20, 2010 at 19:58
  • Mh the debugger shows as contents of the array "NSObject* 0x0 Out of scope". That sounds pretty NULLy to me. Also when operating the objects i the array like "[[mColumns objectAtIndex:x] addObject:g];" the app crashes. Commented Nov 20, 2010 at 20:02
  • 2
    ok, at what point does the debugger say "NSObject* 0x0"? Immediately or some point later? You are retaining the array, if you need it to stick around, right? Commented Nov 20, 2010 at 20:10
  • +1 for the likelihood you're not retaining the array and it's being autoreleased from you. Commented Nov 21, 2010 at 1:08

3 Answers 3

2

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?

answered Nov 20, 2010 at 20:28
Sign up to request clarification or add additional context in comments.

Comments

0

I know you say that the objects are allocated correctly but I'd be inclined to check it. Here's the same code with more debugging:

NSMutableArray *mColumns = [NSMutableArray array];
for( int i=0; i<5; i++ ) {
 MyColumn *col = [[MyColumn alloc] init];
 NSLog(@"%d: %@", i, col);
 [mColumns addObject:col];
 [col release];
}
NSLog(@"Array has %d elements, first is %@", [mColumns count], [mColumns objectAtIndex:0]);

The problem is likely to be either (a) the object is not being created correctly, or (b) the array does have the elements and you have a different problem.

answered Nov 20, 2010 at 19:59

1 Comment

If init were returning nil, the questioner would get an exception upon trying to add that to the array, because an array cannot contain nil. Since the questioner is able to add all five objects, init is returning an object each time; even if the object were somehow wrongly or incompletely initialized, its pointer would not be nil; it would not be logged as 0x0.
0

Hmmm. I can see one problem in your code, but not one that would cause this issue: you are adding retained objects to the array; you'll want to autorelease those first so you don't leak.

As for your actual problem, I don't know. It's impossible to fill an array with null pointers; only objects of type id (not arbitrary pointers) can be put inside of an NSArray/NSMutableArray.

I know you said that you have checked -[MyColumn init], but it would be a good idea to check that it is producing the proper objects in this very spot.

columns = [NSMutableArray array];
for (int i = 0; i < 5; i++) {
 id c = [[[MyColumn alloc] init] autorelease];
 /* set a breakpoint here, and type `po c`
 into the debugger to see what was created
 */
 [columns addObject:c];
}

Unless -[MyColumn init] is making some really funky objects, I can't figure out what the problem would be. I wonder whether something weird is happening to mColumns; is it retained by anything, for instance?

answered Nov 20, 2010 at 19:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.