Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7643443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:27:02+00:00 2026-05-31T09:27:02+00:00

Okay guys, I’ve got one thats got me baffled. I’ve done some searching for

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:27:03+00:00Added an answer on May 31, 2026 at 9:27 am

    This code should spit out a warning “Control reaches end of non-void function”:

    - (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;
    }
    

    You declare init to return a value of type id. You return nothing. Then you use that nothing and assign it to self here:

     if (self=[super init]) {
    

    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:

    • self remains uninitialized with a random value.
    • self is initialized with the return value from the stack, but since you never actually returned a value the return value is just a random value.

    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. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, some Guys will know what i mean and edit my Question but they
Okay guys, I'm sure this has a very simple solution but my searches are
Hye guys. Okay. I have done this coding. But it seems have error. Can
hey guys, I got some difficulty here. It is purely coding algorithm problem. Okay,
Okay guys, I am working hashmap, using one of my classes as Key. My
Okay, here goes. Stack Overflow virgin here but hopefully you guys will be able
Okay, I might confuse some people who might be able to answer this question,
Okay guys, maybe you can help me out with this one. I'm about ready
Hey guys. I have some difficulty in creating a filename. okay, here is what
Okay guys, I have read through all the other posts and question on jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.