I have a question and dunno how to give it a title

Hi,
We often use code like this:
Code:
      b2EdgeShape groundEdge;
      b2FixtureDef boxShapeDef;
      boxShapeDef.shape=&groundEdge;

to create a shape, and assign it to the "shape" domain of a fixtureDef in function such as init(). As a local variable, I think 'groundEdge' will vanish when the program runs out of method init(), and 'boxShapeDef.shape' will be an invalid pointer...so I think there should be some errors happen or it just crashes.
But seems the program still runs well, could someone tell me why?
Because end of this fonction, this shape is destroy 2 time

With groundEdge and in destructor of boxShapeDef. You need alocate in memory you shape
b2Fixture.cpp, line 54 (in my version):

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


This shows us that Box2D manages its own copy of the b2Shape. Your code as presented is fine to use. The only object you should be allocating on the heap for Box2D directly is the b2World, or specific exceptions such as this one in b2World.h:

Code:
   /// Register a contact event listener. The listener is owned by you and must
   /// remain in scope.
   void SetContactListener(b2ContactListener* listener);


So, that same code would break if you tried using a stack allocated b2ContactListener and returning. Your app would probably crash the next time you ran world->Step.
Yep, my bad, b2FixtureDef has not destructor 😳
b2Fixture.h l.69
Quote:
/// The shape, this must be set. The shape will be cloned, so you
/// can create the shape on the stack.
const b2Shape* shape;

It is you case, you shape is created on the stack, then you need cloned him. Why??? I dont know.


Code:
This shows us that Box2D manages its own copy of the b2Shape.
...
So, that same code would break if you tried using a stack allocated b2ContactListener and returning. Your app would probably crash the next time you ran world->Step.

If Box2D clone this object, where is problem? After it uses it own copy of object and no the first shape.

The problem is not in the step, but the initiation :

Quote:
I think 'groundEdge' will vanish when the program runs out of method init(), and 'boxShapeDef.shape' will be an invalid pointer..


But i can not undertand why b2FixtureDef need pointer is valid when is are destroy. It is not really question on Box2D, but i am curious 😀

edit :
Quote:
...so I think there should be some errors happen or it just crashes.

Ok, problem can be in step
感觉 Orwel 略语无伦次的样子。are you also a Chinese?

anyway thank you and pTymN !

one thing i dont understand is why it pass the domain by reference although Box2D manages its shapes on its own..
isn't "pass by value" a better and more direct way? so that it doesn't need "clone" again
Pass by reference allows you to pass any type of shape using the same format, by using the parent type b2Shape.

myFixtureDef.shape = &myCircleShape;
...or
myFixtureDef.shape = &myPolygonShape;


If you want to pass by value, you would have to do something like this:

myFixtureDef.circleShape = myCircleShape;
...or
myFixtureDef.polygonShape = myPolygonShape;
...or
...or
etc

Passing by value is always a full 'clone' anyway. It uses the copy constructor of the object to initialize it etc, and can be slower.
irresistible force wrote:
Pass by reference allows you to pass any type of shape using the same format, by using the parent type b2Shape.

myFixtureDef.shape = &myCircleShape;
...or
myFixtureDef.shape = &myPolygonShape;


well i didn't notice that.. Many thx to irresistible force~

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