Soft bodies bug: crash on remove while on TickCallback

Antodologo
Posts: 21
Joined: Sat Feb 06, 2010 3:40 am

Soft bodies bug: crash on remove while on TickCallback

Post by Antodologo »

Hi

I have been experiencing crashes when deleting soft bodies (that are attached to rigid bodies) but now I know how it happens.

I don't know if I am doing something strange, but I think that my approach should be right.

The problem is here:

Code: Select all

void	btSoftRigidDynamicsWorld::internalSingleStepSimulation( btScalar timeStep )
{
	// Let the solver grab the soft bodies and if necessary optimize for it
	m_softBodySolver->optimize( getSoftBodyArray() );                               // COPY SOFT BODIES VECTOR

	if( !m_softBodySolver->checkInitialized() )
	{
		btAssert( "Solver initialization failed\n" );
	}

	btDiscreteDynamicsWorld::internalSingleStepSimulation( timeStep );             // RIGID BODY STEP AND MANAGE CONTACTS

 	///solve soft bodies constraints
	solveSoftBodiesConstraints( timeStep );                                        // UPDATE SOFT BODIES IN VECTOR COPY

	//self collisions
	for ( int i=0;i<m_softBodies.size();i++)

	[...]
}
As you can see, "m_softBodySolver->optimize( getSoftBodyArray() )" copies the soft bodies vector to be used on "btDiscreteDynamicsWorld::internalSingleStepSimulation( timeStep )" and the soft bodies updates.

But what happens if during "btDiscreteDynamicsWorld::internalSingleStepSimulation( timeStep )", a step is finished and the call to "PhysicManagerTickCallback( btDynamicsWorld *world, btScalar timeStep )" results on a call to "btSoftRigidDynamicsWorld::removeSoftBody(btSoftBody* body)"?

In my game, for example, collisions are managed on PhysicManagerTickCallback and a collision (from a projectile) can destroy a rigid body. If the rigid body has soft bodies attached (ropes) they are destroyed too and removed from the soft bodies vector, but... not from the vector copy. So the next access to the deleted soft body results in a crash.

I have fixed the bug calling "m_softBodySolver->optimize( getSoftBodyArray() )" again after the call to "btDiscreteDynamicsWorld::internalSingleStepSimulation( timeStep )" to update the vector copy. But there are other possible fixes.

Anyway I think the bug should be solved in one way or another, it can affect anyone using collisions and soft bodies.

Any ideas about this?

Thank you for your time once more

Greetings :wink: