Discussion in 'iOS Apps' started by maplestar, Jun 6, 2010.

  1. maplestar macrumors member

    Oct 16, 2008
    I read up on this error and it's supposedly thrown when I access a variable or object that has been released. But I'm not sure how to fix this error! I'm using Cocos2D to program a basic SpriteController that keeps track of all my Sprites and sends them a touchBegan: to keep track of touches.

    I made a Basic HelloWorldScene.m that initializes like this:

    And then, when the Scene is touched, I pass it on to the SpriteController

    Finally, the SpriteController loops through all its Sprites, checks if they respond to touchBegan: and sends them the command.

    The program displays the 2 red1.gif boxes on the screen during initialization fine. And after debugging around, I figured that the flow of the program does enter the TouchBegan and is passed to the SpriteController. However, as soon as I begin looping through allMySprites, I get the EXC_BAD_ACCESS error. After playing around with it, I determined that I simply can't access any of the objects in allMySprites beyond initialization. So even if I added a [allMySprites lastObject] directly in the scene, it'd throw the error. Why can't I access the objects in allMySprites? They don't seem to be released by the time I want to use them, but it just doesn't let me touch them.

    EDIT: Removed all the smiley's in my code :D
  2. amorya macrumors 6502

    Jun 17, 2007
    I think you need to read up the difference between a class and an object. You have a class, SpriteController, but you never actually instantiate it (that is, make an object of the class SpriteController).

    The code you have posted doesn't show where allMySprites is defined, but I can hazard a guess at what's wrong. I bet you have made that an instance variable for SpriteController.

    To make a new SpriteController you would do something like:

    SpriteController* aController = [[SpriteController alloc] init];
    Then, instead of [SpriteController addSprite: … ], you would do

    [aController addSprite: … ]
    That change is not in itself enough, as you would need some place to keep the SpriteController around. For that, you should make an instance variable (possibly with a property) in your HelloWorldScene to hold it.


  3. maplestar thread starter macrumors member

    Oct 16, 2008
    Sorry, I guess I didn't make myself completely clear. When I add a sprite in the initialization, the "addSprite:" method instantiates and creates the object in (don't have actual code, sorry) this manner:

    allMySprites = [NSMutableArray array];

    So it points to an mutable array object. allMySprites is a static NSMutableArray object, not an instantiation variable. So the addSprite method is a class method that adds to the allMySprite object, not to the specific SpriteController.
  4. andr3w macrumors newbie

    Jun 6, 2010
  5. maplestar thread starter macrumors member

    Oct 16, 2008
    Really? This is development for my iPhone app, so I'm not sure where else to post this thread other than the iPhone app section.

    Anyways, I know what object is being accessed which is causing the problem, but I'm not sure how to fix it. By playing around with the debugger and placing a few printf/NSLog's, I determined that the program always crashes when I try to access the objects inside allMySprites. I can access allMySprites just fine, but somehow, I'm not allowed to access the objects inside or call any methods with allMySprites.
  6. andr3w macrumors newbie

    Jun 6, 2010
    The "iPhone/iPad Programming" forum:


    Enabling NSZombie in Instruments will stop the execution of the program at the crash and show you the retain/release history of the object you are accessing that is causing the crash. Something is causing the object to be over released.

Share This Page