Okay guys, I’ve got one thats got me baffled. I’ve done some searching for an answer but nothing I’ve found has cleared anything up. If I have missed an answer that is relevant please accept my apologies and kindly point me in the correct direction. That said…
My problem is an exc_bad_access error which I know is usually caused by trying to do something with an object that has had its memory freed. However, my confusion comes from the fact that when I go to create an instance of a layer (via the init method) as soon as i try to set any of the classes values the error in question gets thrown. The sprite is a member of a super class…the super class inherits from CCLayer. Why is it saying I’m trying to access memory thats been freed when my layer was clearly just created and had memory allocated for it? Is there a solution to get rid of this error message? Perhaps there is some quirk about inheritance in objective c that I don’t know about?
Here are the relevant methods from the layer I’m trying to create:
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
//note that i allocate and initialize the object just like any other object
//I've also tried retain here
MainGameLayer *layer = [[MainGameLayer alloc] init];
layer = [MainGameLayer node];
HUD *hudLayer = [[HUD alloc] initWithLayer:layer];
[scene addChild:hudLayer z:1];
[scene addChild:layer];
return scene;
}
-(id) init {
//will call the init method shown below
if (self=[super init]) {
//program crashes right here on this line with exc_bad_access
centerBackground = [CCSprite spriteWithFile:@"background.png"];
centerBackground.position = ccp(240, 160);
[self addChild:centerBackground];
eastBackground = [CCSprite spriteWithFile:@"background.png"];
eastBackground.position = ccp(752, 160);
[self addChild:eastBackground];
westBackground = [CCSprite spriteWithFile:@"background.png"];
westBackground.position = ccp(-272, 160);
[self addChild:westBackground];
northBackground = [CCSprite spriteWithFile:@"background.png"];
northBackground.position = ccp(240, 672);
[self addChild:northBackground];
southBackground = [CCSprite spriteWithFile:@"background.png"];
southBackground.position = ccp(240, -352);
[self addChild:southBackground];
}
return self
}
Here is the interface for the superclass from which my layer inherits:
@interface GamePlayLayer : CCLayer <UIAccelerometerDelegate> {
UIAccelerometer *accelerometer;
float xAccelerationFactor;
float yAccelerationFactor;
float zAccelerationFactor;
CCSprite *centerBackground;
CCSprite *eastBackground;
CCSprite *westBackground;
CCSprite *southBackground;
CCSprite *northBackground;
CCSprite *southWestBackground;
CCSprite *southEastBackground;
CCSprite *northWestBackground;
CCSprite *northEastBackground;
}
@property (nonatomic, retain) UIAccelerometer *accelerometer;
@property float xAccelerationFactor;
@property float yAccelerationFactor;
@property float zAccelerationFactor;
@end
…And the superclass’s init method
- (id) init {
//note that this init finishes with no problems
xAccelerationFactor = 0;
yAccelerationFactor = 0;
zAccelerationFactor = 0;
self.accelerometer = [UIAccelerometer sharedAccelerometer];
self.accelerometer.updateInterval = .1;
self.accelerometer.delegate = self;
}
Any help or general advice is greatly appreciated. Thanks a ton in advance!
This code should spit out a warning “Control reaches end of non-void function”:
You declare init to return a value of type
id. You return nothing. Then you use that nothing and assign it toselfhere:In the best case the non-returned value is simply
nil. But that wouldn’t crash. So I suppose one of two things is happening:Long story short: never, ever ignore warnings unless you know what you’re doing. Turn on “Treat Warnings As Errors” in Build Settings, it can be quite the instructive disciplinary measure. 🙂