Getting b2CircleDef properties from b2BodyDef

So, in my little editor, I stuffed all my b2BodyDefs into a vector as well as added them to the world so that I could save them later.

Code:
   m_bodies.push_back(bd);
   m_world->CreateBody(&bd);


Now I'm trying to save them... I can get all the b2PolyDef properties correctly, but when I try to access the circle's properties..they're all nonsensical:

Code:
   for(int i=0; i<m_bodies.size(); ++i)
   {      
                for (int j=0; j<b2_maxShapesPerBody; ++j)
      {
         if(m_bodies[i].shapes[j] != NULL)
         {
            switch(m_bodies[i].shapes[j]->type)
            {
            case e_circleShape:
               {
                  const b2CircleDef* cd = (const b2CircleDef*)m_bodies[i].shapes[j];
                  radius = new TiXmlElement("Radius");
                  shape->LinkEndChild(radius);
                  radius->SetDoubleAttribute("Value",cd->radius);
               }
               break;


Am I not allowed to cast a b2ShapeDef like that, or what? It works for b2PolyDef so I can't figure why it won't work for b2CircleDef.
The shape members are b2Shapes, not b2ShapeDefs.

Why not store the defs when you originally build them?

See this thread: http://www.box2d.org/forum/viewtopic.php?f=5&t=120&p=832&hilit=save#p832
You specified "shapes" as b2ShapeDef* shapes[b2_maxShapesPerBody];
I think you misread my code. I'm not trying to get the data out of a b2Body, but the b2BodyDefs.

I figured I could get the ShapeDefs out of the BodyDefs... and indeed I can get the PolyDefs out of the BodyDefs, so I assumed I was on the right track. That thread is discussing relevant stuff, but no solution has been posted yet. I'm simply inquiring why I can do this

const b2PolyDef* pd = (const b2PolyDef*)m_bodies[i].shapes[j];

but not

const b2CircleDef* cd = (const b2CircleDef*)m_bodies[i].shapes[j];
Ah, okay. What you are doing seems fine. Perhaps you are manually setting the shape type incorrectly.

Did you look at cd in the debugger?
I wish I could. For some strange reason my code immediately crashes in debug mode, but runs fine under release. It's complaining about this line here (access violation):

for (b2Shape* s = b->m_shapeList; s; s = s->m_next)

in the draw function I stole from Render.cpp...
It sounds like some is going very bad, like you are using an object after it has been freed or freeing something twice, etc. Make sure you nullify all you pointers once you free something.
I was about to post on this again, but I think I just found the problem, if anyone was wondering. The b2BodyDef only stores a pointer to an array of shapes. As soon as the original shape def is destroyed, the pointer inside b2BodyDef points into space. Meaning, we can't just store a copy of the b2BodyDef and expect the shapes to remain intact. It was a miracle that the poly defs didn't get jumbled as well (maybe because they're larger?).

While I get this sorted out, I've opted for a deliberate memory leak (it's the only way I can get this to work... I've tried hacking the b2BodyDef but that only created more problems)

Code:
   b2CircleDef      *cd = new b2CircleDef();
   cd->friction   = .85f;
   cd->radius      = r;
   cd->type      = e_circleShape;


That will make sure the circle def is persistent.

ERIN:

Might it be a good idea to modify b2BodyDef::AddShape to "new" the shape data, and delete it in the destructor? That way the shape data isn't lost before the b2BodyDef is destroyed?

Otherwise, something like this would produce undefined results, no?

Code:
b2BodyDef bd;
{
b2CircleDef cd;
bd.AddShape(&cd); // stores a pointer to cd
} // cd is destroyed
world->CreateBody(&bd);
This is drastically changed for version 2.0. People want to dynamically add and remove shapes from bodies. So the body definition will no longer know about shapes. Shapes will be added to the body after it is created.
Well then, will we be able to retrieve the shape data from the body?
Better yet, you'll get a shape pointer when you create the shape. You'll also be able to iterate over the body's shapes. Right now shapes are re-centered on the body's center of mass, but this might change. I'm not sure yet. Other than that, you should be able to directly rebuild a shape definition from the shape.

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