0
\$\begingroup\$

I have the following code for deleting a Box2D body:

void b2World::DestroyBody(b2Body* b)
{
    b2Assert(m_bodyCount > 0);
    b2Assert(IsLocked() == false);
    if (IsLocked())
    {
        return;
    }

    // Delete the attached joints.
    b2JointEdge* je = b->m_jointList;
    while (je)
    {
        b2JointEdge* je0 = je;
        je = je->next;

        if (m_destructionListener)
        {
            m_destructionListener->SayGoodbye(je0->joint);
        }

        DestroyJoint(je0->joint);

        b->m_jointList = je;
    }
    b->m_jointList = NULL;

    // Delete the attached contacts.
    b2ContactEdge* ce = b->m_contactList;
    while (ce)
    {
        b2ContactEdge* ce0 = ce;
        ce = ce->next;
        m_contactManager.Destroy(ce0->contact);
    }
    b->m_contactList = NULL;

    // Delete the attached fixtures. This destroys broad-phase proxies.
    b2Fixture* f = b->m_fixtureList;
    while (f)
    {
        b2Fixture* f0 = f;
        f = f->m_next;

        if (m_destructionListener)
        {
            m_destructionListener->SayGoodbye(f0);
        }

        f0->DestroyProxies(&m_contactManager.m_broadPhase);
        f0->Destroy(&m_blockAllocator);
        f0->~b2Fixture();
        m_blockAllocator.Free(f0, sizeof(b2Fixture));

        b->m_fixtureList = f;
        b->m_fixtureCount -= 1;
    }
    b->m_fixtureList = NULL;
    b->m_fixtureCount = 0;

    // Remove world body list.
    if (b->m_prev)
    {
        b->m_prev->m_next = b->m_next;
    }

    if (b->m_next)
    {
        b->m_next->m_prev = b->m_prev;
    }

    if (b == m_bodyList)
    {
        m_bodyList = b->m_next;
    }

    --m_bodyCount;
    b->~b2Body();
    m_blockAllocator.Free(b, sizeof(b2Body));
}

When I close the game I get an exception, and the debugger points to the first line :

b2Assert(m_bodyCount>0);

What exactly does this mean?

\$\endgroup\$
2
\$\begingroup\$

An assertion failure happens when the expression returns 0, in this case when m_bodyCount is lower or equal to 0. This means that, whenever your code executes b2World::DestroyBody();, all the previously created b2Bodys are already destroyed, if there ever were any created in the first place.

If you did create at least one (1) b2Body* instance, make sure that that specific instance isn't deleted multiple times in the code (in multiple instances of your class A for example), since very weird bugs can occur because of that.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ my project has the next class: class A with b2Body * and b2Fixture* pointers. Its destructor was deleting b2Body* pointer and i would get that assertion failure. But as i added deleting b2Fixture pointer in the very same destructor i seem to have issue get fixed. But that doesnt make sense to me as i thought all fixtures gets deleted with body all along. \$\endgroup\$ – 0x00004 Dec 20 '19 at 12:20
  • \$\begingroup\$ You're correct regarding that fixtures contained in a b2Body* instance get destroyed during the b2Body* deletion. Like you wrote - it doesn't really make sense that the code isn't crashing when you're deleting the fixture the same time as the body but that it crashes when only the body is destroyed. Clearly a bug somewhere. Make sure that the b2Body* isn't used/deleted multiple times in the code, since very weird things can happen because of that. \$\endgroup\$ – Daniel_1985 Dec 20 '19 at 15:00
0
\$\begingroup\$

The issue was that i was deleting every pointer to b2 objects in a class itself.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.