Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory corruption when enabling CCD in a multi threaded world #4117

Closed
jrouwe opened this issue Jan 2, 2022 · 3 comments
Closed

Memory corruption when enabling CCD in a multi threaded world #4117

jrouwe opened this issue Jan 2, 2022 · 3 comments

Comments

@jrouwe
Copy link

jrouwe commented Jan 2, 2022

When configuring a body to have a non 0 setCcdSweptSphereRadius and setCcdMotionThreshold in combination with a multi threaded world (btDiscreteDynamicsWorldMt) the following callstack corrupts memory:

btCollisionDispatcherMt::getNewManifold(const btCollisionObject * body0, const btCollisionObject * body1) Line 69 C++
btDiscreteDynamicsWorld::createPredictiveContactsInternal(btRigidBody * * bodies, int numBodies, float timeStep) Line 905 C++
btDiscreteDynamicsWorldMt::UpdaterCreatePredictiveContacts::forLoop(int iBegin, int iEnd) Line 101 C++
ParallelForJob::executeJob(int threadId) Line 122 C++
WorkerThreadFunc(void * userPtr) Line 371 C++
win32threadStartFunc(void * lpParam) Line 241 C++

At this time btCollisionDispatcherMt::m_batchUpdating is false, so multiple threads are racing on this piece of code:

manifold->m_index1a = m_manifoldsPtr.size();
m_manifoldsPtr.push_back(manifold);

This sometimes causes an incorrect index to be written into the manifold and sometimes it corrupts the array.

@jrouwe
Copy link
Author

jrouwe commented Jan 2, 2022

Note for a repro, take the code from here:

https://github.com/jrouwe/BulletPerformanceTest

And in PerformanceTest.cpp change 'for (int mq = 0; mq < 2; ++mq)' into 'int mq = 1;' and change 'for (int num_threads = 1; num_threads <= (int)thread::hardware_concurrency(); ++num_threads)' into 'int num_threads=8'.

@erwincoumans
Copy link
Member

erwincoumans commented Jan 3, 2022

Thanks for the report. Can you check if this PR helps?
#4119

You can increate the performance slightly by setting the s_minimumContactManifoldsForBatching as follows:

	// Create solver
		gSolver = new btSequentialImpulseConstraintSolverMt();
		btSequentialImpulseConstraintSolverMt::s_minimumContactManifoldsForBatching = 2000;

Note that Bullet isn't as well optimized as PhysX or Jolt.

@jrouwe
Copy link
Author

jrouwe commented Jan 3, 2022

That fixes the issue, thanks!

@jrouwe jrouwe closed this as completed Jan 3, 2022
jrouwe added a commit to jrouwe/BulletPerformanceTest that referenced this issue Jan 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants