I'm sry, but another destroying body question :-(

Hi there!, this is the first time I'm writting in this forum. Thank you so much for your hard work!

I've readed lots of post and questions about the same question, but I still can't figure why I'm having this issue. A strange thing is that on Ubuntu it seems to work perfectly, but when I'm working on Windows 7 with a MSVC compiler sometimes it raises me this Assertion:

assertion failed (m_world-is locked() == false)

I've seen that this could be caused because bad deletion of bodies (inside a step, for example).
Here is my code: I've extended the library to use it as third party in my engine:

Code:
void PhysicWorld::step()
{
    world->Step(timeStep / 1000.0f, velocityIterations, positionIterations);

    // Clean widgets destroyed at collisions
    if (dirtyElements.size() > 0)
    {
        for (auto it = dirtyElements.begin(); it != dirtyElements.end(); it++)
            world->DestroyBody(*it);
        dirtyElements.clear();
    }
    if (dirtyJoints.size() > 0)
    {
        for (auto it = dirtyJoints.begin(); it != dirtyJoints.end(); it++)
            world->DestroyJoint(*it);
        dirtyJoints.clear();
    }

    // Update widgets
    for (b2Body *b = world->GetBodyList(); b; b = b->GetNext())
        if (b->GetUserData()) ((PhysicWidget *) b->GetUserData())->updateGraphicComponent();
}


As you can see, in first place, I'm taking the step and, when it finishes, I destroy the elements who has been removed from my scene (stored at dirtyElements) and same for the joints. At last, I update graphical representation of bodies. Probably I'm not properely understanding something, but I can't figure why in Ubuntu it works perfectly and this Assertion only raises on Windows 🙁 Should I add a active waiting in my code somewhere else? Something like:
Code:
    [...]
    world->Step(timeStep / 1000.0f, velocityIterations, positionIterations);

    while (world->IsLocked()) {}

    // Clean widgets destroyed at collisions
    if (dirtyElements.size() > 0)
    [...]


Any clue or tip please?
Thank you so much in advance and best regards 🙂
maije
There's nothing obvious that stands out here. And as you said, it works fine on Ubuntu but not on Windows when using the MSVC compiler. This most likely has to do with the differences of optimizations between compilers.

The locks only occur within the step function of b2World. The only places where you can invoke the world lock assertion without going into the source are listeners. Other than that, seems like MSVC is optimizing something that Box2D doesn't like. What MSVC compiler are you using?

Have you tried using the proper data types instead of "auto"?
From an architecture standpoint, the easiest solution for me was to make objects that aggregate bodies responsible for calling destroy and handling their own collisions. So it ends up looking like this:

1. Collision occurs
2. Object flagged for destruction
3. box2d World step finishes
4. destroy is called on box2d bodies that are aggregates of the parent object
Hi there jayther and seanny and thank you so much for your answers. I didn't receive a mail so I though noboby had reply this question. I'm so sorry because the delay.

Talking about compilers, yep, I do belive singleton pattern is causing some kind of troubles when destructors are called and the proper order in which they must to be removed. I think that windows is not taking care about static instances deleting order and don't looks it's removing that items in the inverse order they were created (only a suposition). To make it more complex, I have 3 threads running accessing same resources (main thread, timed action thread and physic world thread). At this moment, I'm removing any singleton trace from my all code in order to make it more secure on deletions. It's kind of hard desing refactoring and it's taking me so long...

Replying seannybgcode, I think that this is the sequence I'm implementing, maybe I'm making some bad implementation and have to check it more deeply. Maybe I'm not removing properly fixtures from the erased object and that fixtures are maintaining my object partially in memory so it can be called but some characteristics have been deleted. Going to check this and after that, if I figure what's happening, I'll write you back guys 🙂

Thank you so much for your tips and advices. I really appreciate them. I'll tell you (in the hope it can be helpfull for someone else too) when I solve this issue that is still rising sometimes 🙁
Regards,
maije

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