exc_bad_access at b2Fixture::Create

Hi, in my iOS game, I'm storing the fixture definition as a property of a game object class , which means each game object has a physical representation. Then when the game starts, I retrieve the fixture definition from the game objects and create physical bodies accordingly. My code is like this:

Code: Select all

@interface GameObject : NSObject
@property b2FixtureDef fixtureDef
@property CGSize size
...
(void)setFixtureDef {
  fixtureDef.density = 1.0f;
  fixtureDef.friction = 0.3f;
  fixtureDef.restitution = 0.1f;
  b2PolygonShape shape;
  shape.setAsBox(size.width, size.height);
  fixtureDef.shape = &shape;
}

Then in GameLogic, I loop through all the fixtures of the game objects and create the associating physical bodies.

But it gives me exc_bad_access when I do body->createFixture(fixtureDef), in particular, the error is at line

Code: Select all

m_shape = def->shape->Clone(allocator);

in b2Fixture::Create. I think the problem is my shape object is not retained when I retrieve the fixture definition in GameLogic. I am doing something wrong. I'm not familiar with C++, so I hope somebody can point me to how I can achieve the above-described task.
You'll need to store the shape as well, because after the setFixtureDef function finishes, the shape is destroyed and the pointer you set in fixtureDef.shape is invalid.
I've thought of that but don't know how to store the shape property in a way that allows inheritance. I have multiple game objects that inherit from the GameObject class, and each of them can define its own shape. How can I do it when b2Shape is an abstract class?

Original thread: https://box2d.org/forum/viewtopic.php?t=8212