crash at exit

jbacon
Posts: 11
Joined: Wed May 13, 2009 12:30 pm

crash at exit

Post by jbacon »

Hi,

I'm having an issue when I exit my app which uses bullet -
I do clean up everything nicely when I quit 'gracefully', but when I
exit the app say via closing it's OpenGL window (on win7),
Bullet fails to assert in:

virtual ~btRigidBody()
{
//No constraints should point to this rigidbody
//Remove constraints from the dynamics world before you delete the related rigidbodies.
btAssert(m_constraintRefs.size()==0);
}

now I'm not sure why bullet is torn down *before* my app's atexit function is called which
should clean up nicely (removing constraints, bodies, etc.) and the stacktrace in MSVC
doesn't help either,

-StyleClash.exe!btRigidBody::~btRigidBody() Line 176 C++
-StyleClash.exe!`btTypedConstraint::getFixedBody'::`2'::`dynamic atexit destructor for 's_fixed''() + 0xd bytes C++
-msvcr90d.dll!doexit(int code=0, int quick=0, int retcaller=0) Line 591 C
-msvcr90d.dll!exit(int code=0) Line 412 + 0xd bytes C

even though I compile the bullet sources along with my app.

could anyone shed some light on this and suggest how I can make sure that
I clean up properly ?

thanks,

J
marios
Posts: 52
Joined: Mon Jul 19, 2010 3:11 am

Re: crash at exit

Post by marios »

Before you remove/delete rigidbody from world,you should remove all constraints connected with this body, for example like this:

Code: Select all

int num=body->getNumConstraintRefs(); //body of course is body you want to delete
for(int j=0;j<num;++j)
    dynamicsWorld->removeConstraint(body->getConstraintRef(0));
dynamicsWorld->removeRigidBody(body);

//here you can free memory
jbacon
Posts: 11
Joined: Wed May 13, 2009 12:30 pm

Re: crash at exit

Post by jbacon »

Dear Marios,

I understand that. The question is: when and how is the bullet world torn down
when the application terminates ? is there an atexit call in bullet that I missed that
destroys all btRigidBodies (but not their constraints ?) nad if not why does
~btRigidBody get called *before* any atexit function I registered myself ?

J
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: crash at exit

Post by Erwin Coumans »

It looks like the crash happens because there is a constraint attached to a fixed body, which is going out of scope at program exit.

The program seems to first call the destructor of that global fixed body (btTypedConstraint::getFixedBody is a static global), and afterwards call your atexit callback?

Is there a way to cleanup the physics constraints/world beforehand?
Thanks,
Erwin
jbacon
Posts: 11
Joined: Wed May 13, 2009 12:30 pm

Re: crash at exit

Post by jbacon »

Hi Erwin,

The thing is, I'd *love* to clean up the physics before bullet
is torn down, but that happens *before* my atexit is called.

Is there any way around this ?

J
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: crash at exit

Post by Erwin Coumans »

try to avoid using atexit, but cleanup Bullet beforehand, in a normal method.

Good luck,
Erwin
jbacon
Posts: 11
Joined: Wed May 13, 2009 12:30 pm

Re: crash at exit

Post by jbacon »

Hi Erwin,

That would be my goal, but what if I can't ? What is happening now,
is that when my (GLut) app is closed by closing the window, bullet is torn
down before I can do anything. How could I resolve this ?

J
jbacon
Posts: 11
Joined: Wed May 13, 2009 12:30 pm

Re: crash at exit

Post by jbacon »

Hi,

It's still problematic - even though now I catch the exit with an atexit function and clean my stuff,
bullet still crashes, and the stack trace makes me wonder: what is this "dynamic atexit destructor for 's_fixed"
in this stacktrace ? and how do I clean out the s_fixed body myself ??

msvcr90d.dll!_NMSG_WRITE(int rterrnum=10) Line 198 C
msvcr90d.dll!abort() Line 59 + 0x7 bytes C
msvcr90d.dll!_wassert(const wchar_t * expr=0x00c8354c, const wchar_t * filename=0x00c8c108, unsigned int lineno=177) Line 163 C
StyleClash.exe!btRigidBody::~btRigidBody() Line 177 + 0x29 bytes C++
StyleClash.exe!`btTypedConstraint::getFixedBody'::`2'::`dynamic atexit destructor for 's_fixed''() + 0xd bytes C++

thanks a lot if (any of) you can take a look at it !

J
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: crash at exit

Post by Erwin Coumans »

It seems that you are creating constraints between a dynamic body, without providing a second body (relying on the constructor of constraints passing only a single body instead of two).

One option is to provide your own static/fixed body, and use it to initialize the constraint(s), using for example

btRigidBody* CreateFixedBody()
{
btRigidBody* fixedBody = new btRigidBody(0, 0,0);
fixedBody .setMassProps(btScalar(0.),btVector3(btScalar(0.),btScalar(0.),btScalar(0.)));
return fixedBody ;
}

Hope this helps,
Erwin
jbacon
Posts: 11
Joined: Wed May 13, 2009 12:30 pm

Re: crash at exit

Post by jbacon »

That does help. I was under the impression that giving just one
body to a constraint was ok. this is an easy fix however.

Thanks !

J
onne
Posts: 4
Joined: Mon Feb 10, 2014 12:25 pm

Re: crash at exit

Post by onne »

I have a similar issue, when I create two bodies and add a constraint between them. The difference is that I have the 'disableCollisionsBetweenLinkedBodies=false' (as by default).

...in this case the constraint reference is not being set(so there is no way to get the constraint info from the btRigidBody), then when removing the attached body the constraint remains alive (causing crash on the next physics step).