Newbie problem crash when deleting bodies

Hello! I started using box2D yesterday. I wanted to create simple class to integrate my graphics engine with box2d.
I haven't even started to program when i got the problem:
The program is just crashing after start.
I don't know what i did wrong, the debugger shows me that the problem is in destructor.
In the console i can see:
Quote:
Assertion failed: m_bodyCount > 0 file (...) B2World.cpp line 133

Here's the test code:
Code:
#include <Box2D/Box2D.h>
#include <vector>

class shape
{
public:
    shape(b2World&);
    ~shape();

private:
  b2Body* physical;
};


shape::~shape()
{
    if(physical != nullptr) {
    physical->GetWorld()->DestroyBody(physical);
    physical = nullptr;
    }
}
shape::shape(b2World& world)
: physical(nullptr)
{
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;


    bodyDef.position.Set(1.0f, 1.0f);
    physical = world.CreateBody(&bodyDef);
    //skipping fixture etc..

}


int main()
{




b2Vec2 gravity(0.0f,10.0f);
b2World world(gravity, true);


std::vector<shape> objects;

objects.push_back(shape(world));

objects.clear();

    while(true)
    {

       // world.Step(1/60.f, 8, 3);

    }
}

Thanks for answers!
One time I spent 6 hours trying to solve this same problem, because the problem only popped up after I had a ton of code, and I didn't have a debugger or any real way to get good help. I still don't understand it even after looking at CreateBody and b2Body. You have to store the b2BodyDef in your shape class.
Code:
private:
  b2Body* physical;
  b2BodyDef bodyDef;//set this to the values you want and use it to create the body
};
The reason why this is not working is because push_back() copies the object. The signature of push_back says it references the object, but inside the function itself, it creates a copy.

In that one line, you create a shape object, then that object is copied and the duplicate is stored in the vector and the original is essentially unused. By this time, it can be one of any of these two possibilities:

  • Because of the copy, you have two shape objects in memory with the same b2Body pointer. After the clear command, the copy stored in the vector is destroyed. By the end of the block, the original shape is destroyed (thus calling the destructor), where it tries to destroy with the same b2Body pointer.
  • The unused original shape object is destroyed pretty much soon after since that particular instance isn't used anywhere else (optimizer in some C++ compilers would do that sort of thing).

In either case, this would be solved if you used pointers for initializing a shape object and store pointers in the vector, as it is the safe way to do it anyway (since you store a pointer in a class, where a shallow copy will not duplicate the b2Body).

Also, you don't have to add b2BodyDef in the class. b2BodyDef objects are reusable and disposable, as the values, not the object, are copied when using CreateWorld().

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