views:

695

answers:

1

I'm developing on the iPhone and I'm missing something about storing objects in an NSMutableArray - what am I doing wrong and how should I approach this?

The relevant sections of the code are

I have an object, Entity, defined which inherits directly from NSObject. In my class GLView I wish to define an array of these - so GLView.h

@interface GLView : UIView
{
    BOOL controllerSetup;

    // Entity etc - first development task it to get one up
    NSMutableArray *arrEntities;
}
@property(nonatomic, assign) NSMutableArray *arrEntities;

and in the body in the drawView function - which is called multiple times by a timer event - I allocate the array and, for initial development, just one Entity object and add it to the array

- (void)drawView
{
    if(!controllerSetup) { // initialize on first call
        [controller setupView:self];
        controllerSetup = YES;

        arrEntities = [NSMutableArray array];

        Entity* thisEntity = [[Entity alloc] initEntity]; 
        [arrEntities addObject:thisEntity];
    }
    Entity *entity = [arrEntities objectAtIndex:0];
    [entity tickEntity:0.1];
}

My understanding was that on the first call of drawView the NSMutableArray would be set up and the instance of Entity added to it, then in the body the a pointer to first copy of entity stored in the array retrieved and this manipulated (obviously I intend to add more and loop over them later).

However what happens it that on the first call of the procedure everything works fine, the objectAtIndex retrieves the entity pointer and all works. However on the second call when controllerSetup is true and the initialization clause not executed arrEntities is empty and the program crashes as objectAtIndex retrieves nil.

So what am I doing wrong? Why does my stored Entity object not persist in the NSMutableArray?

+8  A: 

Try replacing the line you create arrEntities with the following:

arrEntities = [[NSMutableArray alloc] init];

Then make sure to add it to a dealloc method:

- (void)dealloc
{
     [arrEntities release];
     [super dealloc];
}

Basically, because you are not explicity allocating your array (you are using the static +array method), it is being autoreleased. Your program crashes when the array has been autoreleased and your code tries to access it.

One more thing:

Entity* thisEntity = [[Entity alloc] initEntity];   
[arrEntities addObject:thisEntity];
[thisEntity release]; // add this

If you don't release thisEntity, it will leak. addObject retains the passed object.

Joel Levin