1

I have this code in my .m file, which is a Cocos 2D CCLayer class. I initialize an array in the init method and then I try to use contents of this array in the nextFrame method. But when the nextFrame method gets called, the contents of the array seem empty. When I try to get the first item, I receive an error message saying:

Program received signal "EXC_BAD_ACCESS"

How can I successfully access the contents of this array in my nextFrame method?

NSMutableArray *cars;

-(id) init {
    cars = [NSMutableArray array];
    Car *car;
    car = [[Car alloc] init];
    [cars addObject:car];
    self.isTouchEnabled = YES;
}

- (void) nextFrame:(ccTime)dt {
    Car *car = [cars objectAtIndex:i]; // Program received signal "EXC_BAD_ACCESS" 
}

Car.h

#import <Foundation/Foundation.h>
#import "cocos2d.h";

@interface Car : NSObject {
    NSInteger type;
    CCSprite *sprite;
}

@property (readwrite, assign) NSInteger type;
@property (retain) CCSprite *sprite;

@end

Car.m

#import "Car.h"

@implementation Car

@synthesize type;
@synthesize sprite;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

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

@end
1
  • 1
    cars = [NSMutableArray array]; // -> That looks like autoreleased object. change it to cars = [[NSMutableArray alloc] init]; if contents of array are lost accessing objectAtIndex: would result in SIGABRT with exception NSRangeException. You are accessing released object so EXC_BAD_ACCESS. Commented Sep 4, 2011 at 23:29

1 Answer 1

3

You're assigning the result of [NSMutableArray array] to an instance variable. That is an autoreleased object, which essentially means it doesn't have any owners and thus will feel free to go away after the current runloop iteration*. You need to retain it if you want it to stick around (or just use [[NSMutableArray alloc] init], which returns an object you own).

* Basically. You should see the Cocoa memory mangement guide for more details. It's pretty short but full of essential information.

Sign up to request clarification or add additional context in comments.

3 Comments

It's a bit misleading to say it will be free to go after the current runloop iteration. Quoting Ondřej Čada from objc-language mailing list: "The guaranteed lifetime is the end of the current scope (including its return value, if it has one). No more (for outside there can be a nested pool)."
I don't think it is misleading to say so. It simply means that you can't tell how much longer it will be around. Only the current scope (including return value) is guaranteed, as you quoted.
@dreamlax: Yeah, it was a bit hand-wavy. I didn't think a discussion of local autorelease pools was particularly relevant to somebody who didn't even know what autorelease is. I'll put in a pointer to the memory management documentation for the details. Should have done so to begin with.

Your Answer

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.