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 8315035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:01:24+00:00 2026-06-08T21:01:24+00:00

As suggested in the Box2D manual, I’m setting my body->userData to the CCSprite associated

  • 0

As suggested in the Box2D manual, I’m setting my body->userData to the CCSprite associated with it. However, when I later try to access it – even within the same method, or the same for loop – it inevitably throws EXC_BAD_ACCESS.

The error shows up any time the following code structure appears:

b2BodyDef bodyDef;
b2Body* blockBody;
Block* block;

//...

block = [Block spriteWithFile:spriteName];

//Block configuration
block.position = CGPointMake(x.floatValue * BLOCK_SIZE_PIXELS, y.floatValue * BLOCK_SIZE_PIXELS);        
block.anchorPoint = CGPointZero;
[self addChild:block z:7]; 
[self.blockArray addObject:block];

//Body configuration
bodyDef.type = b2_dynamicBody;
bodyDef.position = toMeters(block.position);
bodyDef.angle = 0.0f;
bodyDef.linearDamping = 1.0f;
bodyDef.angularDamping = 0.0f;
bodyDef.gravityScale = 1.0f;
bodyDef.allowSleep = true;
bodyDef.awake = true;
bodyDef.fixedRotation = true;
bodyDef.userData = █

blockBody = world->CreateBody(&bodyDef);

vertices[0] = toMeters(CGPointZero);  
vertices[1] = toMeters(CGPointMake(BLOCK_SIZE_PIXELS, 0));
vertices[2] = toMeters(CGPointMake(BLOCK_SIZE_PIXELS, BLOCK_SIZE_PIXELS));
vertices[3] = toMeters(CGPointMake(0, BLOCK_SIZE_PIXELS));

blockShape.Set(vertices, 4);

fixtureDef.shape = &blockShape;
fixtureDef.density = 1.0f;
blockFixture = blockBody->CreateFixture(&fixtureDef);

block.body = blockBody;


//UPON INVOKING THIS LINE,
//PROGRAM CRASHES WITH EXCEPTION: EXC_BAD_ACCESS
Block* test = (__bridge Block*)blockBody->GetUserData();

This code appears in loadlevel: in GameLayer.m, which inherits from CCLayer.

Block inherits from GameSprite, which defines .body and inherits from CCSprite. Header files:

//
//  Block.h
//
//  Created by [REDACTED] on 7/20/12.
//  Copyright (c) 2012 [REDACTED]. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GameSprite.h"
//#import "Constants.h"

@interface Block : GameSprite
{
//    GameSprite* _sprite;
//    int _platformTag;
}

+(Block*) blockWithFile: (NSString*) filename;
+(Block*) blockWithTexture: (CCTexture2D*) texture;
@end

//
//  GameSprite.h
//  LegendaryOiramBrothers
//
//  Created by [REDACTED] on 7/20/12.
//  Copyright (c) 2012 [REDACTED]. All rights reserved.
//

#import "CCSprite.h"
#import "Constants.h"
#import "Box2D.h"

@interface GameSprite : CCSprite
{
    b2Body* body;
}

- (void) update;

- (void) setPosition:(CGPoint)position;
- (b2Vec2) getVelocity;
- (void) setVelocity:(b2Vec2) vel;

@property (nonatomic) b2Body* body;

@end

From what I can find online, my problem seems to be an incorrect autorelease somewhere – but I don’t see where the block would have an opportunity to deallocate. As a result, I am rather confused.

Thanks for your help.

  • 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-06-08T21:01:26+00:00Added an answer on June 8, 2026 at 9:01 pm

    Can you spot the error in this line?

    bodyDef.userData = &block;
    

    You’re assigning the pointer’s address, not the pointer itself. This should fix it:

    bodyDef.userData = block;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Someone suggested moving a table full of settings, where each column is a setting
a colleague suggested I give this framework a try. My question is, does anyone
Someone suggested a HEX editor which I downloaded and ran. However looking at these
What is the suggested approach for setting up a subscription to a message published
Suggested Reading Similar: Are soft deletes a good idea? Good Article: http://weblogs.asp.net/fbouma/archive/2009/02/19/soft-deletes-are-bad-m-kay.aspx How I
As suggested I allow all of my WPF UIElement s to have a dynamic
As suggested by Apple, I have put a button on my application, to restore
As suggested by (among others) Kazi Manzur Rashid in this blog post , I
It is has been suggested that it is best to initialize a $('#form').validate({}) function
This is a suggested re-up on a question I had before. Its really and

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.