0
\$\begingroup\$

I'm writing a game engine in C++ with Irrlicht and Bullet Physics. All of my Irrlicht code works perfectly, but upon creating Bullet Physics objects with new, and later freeing them, my program throws a free(): invalid pointer error.

Backtracing with gdb shows that the error is a libc raise() when Irrlicht tries to delete an Octree I am using for the game map: irr::scene::COctreeSceneNode::deleteTree().

The full gdb backtrace is:

free(): invalid pointer

Program received signal SIGABRT, Aborted.
0x00007ffff7455f25 in raise () from /usr/lib/libc.so.6
(gdb) bt
#0  0x00007ffff7455f25 in raise () from /usr/lib/libc.so.6
#1  0x00007ffff743f897 in abort () from /usr/lib/libc.so.6
#2  0x00007ffff7499258 in __libc_message () from /usr/lib/libc.so.6
#3  0x00007ffff74a077a in malloc_printerr () from /usr/lib/libc.so.6
#4  0x00007ffff74a214c in _int_free () from /usr/lib/libc.so.6
#5  0x00007ffff7bd3b69 in irr::scene::COctreeSceneNode::deleteTree() ()
   from /usr/lib/libIrrlicht.so.1.8
#6  0x00007ffff7bd41ae in irr::scene::COctreeSceneNode::~COctreeSceneNode() ()
   from /usr/lib/libIrrlicht.so.1.8
#7  0x00007ffff7bd4474 in virtual thunk to irr::scene::COctreeSceneNode::~COctreeSceneNode() () from /usr/lib/libIrrlicht.so.1.8
#8  0x00007ffff7bf2706 in irr::scene::CSceneManager::removeAll() ()
   from /usr/lib/libIrrlicht.so.1.8
#9  0x00007ffff7bf971e in irr::scene::CSceneManager::~CSceneManager() ()
   from /usr/lib/libIrrlicht.so.1.8
#10 0x00007ffff7bf9c54 in virtual thunk to irr::scene::CSceneManager::~CSceneManager() () from /usr/lib/libIrrlicht.so.1.8
#11 0x00007ffff7d45e5e in irr::CIrrDeviceLinux::~CIrrDeviceLinux() ()
   from /usr/lib/libIrrlicht.so.1.8
#12 0x00007ffff7d45f54 in virtual thunk to irr::CIrrDeviceLinux::~CIrrDeviceLinux() () from /usr/lib/libIrrlicht.so.1.8
#13 0x000055555556b97e in irr::IReferenceCounted::drop() const ()
#14 0x000055555556a88f in IrrlichtHandler::drop_device() ()

The code which seems to be the cause of the error is the initialization:

    /* PHYSICS SETUP*/
    btDefaultCollisionConfiguration *col_config = new btDefaultCollisionConfiguration();
    btCollisionDispatcher *dispatcher = new btCollisionDispatcher(col_config);
    btBroadphaseInterface *overlapping_pair_cache = new btDbvtBroadphase();
    btSequentialImpulseConstraintSolver *solver = new btSequentialImpulseConstraintSolver;
    btDiscreteDynamicsWorld *dynamics_world = new btDiscreteDynamicsWorld(
        dispatcher, overlapping_pair_cache, solver, col_config
    );

    dynamics_world->setGravity(btVector3(0,-10,0));

and deletion of Bullet:

irrlicht_handler.drop_device();

    for(int i=dynamics_world->getNumCollisionObjects()-1; i>=0; i--)
    {
        btCollisionObject *obj = dynamics_world->getCollisionObjectArray()[i];
        btRigidBody *rb = btRigidBody::upcast(obj);
        if(rb && rb->getMotionState())
        {
            delete rb->getMotionState();
        }
        dynamics_world->removeCollisionObject(obj);
        delete obj;
    }

    delete dynamics_world;
    delete solver;
    delete overlapping_pair_cache;
    delete dispatcher;
    delete col_config;

The full code is on Github.

\$\endgroup\$
  • \$\begingroup\$ C++ has smart pointers, any reason not to use them in your code? \$\endgroup\$ – Vaillancourt Feb 7 at 0:34
  • \$\begingroup\$ @Vaillancourt the Bullet Physics example documentation did not use smart pointers and so I had little reason to. I am not sure that implementing them would solve this because this is some fairly basic usage of allocation. I might try implementing smart pointers but I'm afraid it would add unneeded complexity. \$\endgroup\$ – lilcutthroat Feb 7 at 0:41
  • \$\begingroup\$ So what you say is that when you comment your bullet physics code, you don't get the error, but if you add it again, you have the error? \$\endgroup\$ – Vaillancourt Feb 7 at 2:12
  • \$\begingroup\$ @Vaillancourt yes. \$\endgroup\$ – lilcutthroat Feb 7 at 11:51
  • 1
    \$\begingroup\$ ..and you don't have access to a step-by-step debugger to figure out why? \$\endgroup\$ – Vaillancourt Feb 7 at 12:06
0
\$\begingroup\$

I fixed this by switching from the latest github code to the release code. When I posted this question I didn't realize I was using the latest github code.

\$\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.