Ver Fonte

Bugfix: Lock was released too late

Jorrit Rouwe há 4 anos atrás
pai
commit
fdf16385dd
2 ficheiros alterados com 29 adições e 2 exclusões
  1. 11 2
      Jolt/Physics/Body/BodyLock.h
  2. 18 0
      Jolt/Physics/Collision/NarrowPhaseQuery.cpp

+ 11 - 2
Jolt/Physics/Body/BodyLock.h

@@ -32,8 +32,8 @@ public:
 		}
 	}
 
-	/// Destructor will unlock the body
-								~BodyLockBase()
+	/// Explicitly release the lock (normally this is done in the destructor)
+	inline void					ReleaseLock()
 	{
 		if (mBodyLockMutex != nullptr)
 		{
@@ -41,9 +41,18 @@ public:
 				mBodyLockInterface.UnlockWrite(mBodyLockMutex);
 			else
 				mBodyLockInterface.UnlockRead(mBodyLockMutex);
+
+			mBodyLockMutex = nullptr;
+			mBody = nullptr;
 		}
 	}
 
+	/// Destructor will unlock the body
+								~BodyLockBase()
+	{
+		ReleaseLock();
+	}
+
 	/// Test if the lock was successful (if the body ID was valid)
 	inline bool					Succeeded() const
 	{

+ 18 - 0
Jolt/Physics/Collision/NarrowPhaseQuery.cpp

@@ -44,6 +44,9 @@ bool NarrowPhaseQuery::CastRay(const RayCast &inRay, RayCastResult &ioHit, const
 					// Collect the transformed shape
 					TransformedShape ts = lock.GetBody().GetTransformedShape();
 
+					// Release the lock now, we have all the info we need in the transformed shape
+					lock.ReleaseLock();
+
 					// Do narrow phase collision check
 					if (ts.CastRay(mRay, mHit))
 					{
@@ -105,6 +108,9 @@ void NarrowPhaseQuery::CastRay(const RayCast &inRay, const RayCastSettings &inRa
 					// Notify collector of new body
 					mCollector.OnBody(body);
 
+					// Release the lock now, we have all the info we need in the transformed shape
+					lock.ReleaseLock();
+
 					// Do narrow phase collision check
 					ts.CastRay(mRay, mRayCastSettings, mCollector);
 
@@ -158,6 +164,9 @@ void NarrowPhaseQuery::CollidePoint(Vec3Arg inPoint, CollidePointCollector &ioCo
 					// Notify collector of new body
 					mCollector.OnBody(body);
 
+					// Release the lock now, we have all the info we need in the transformed shape
+					lock.ReleaseLock();
+
 					// Do narrow phase collision check
 					ts.CollidePoint(mPoint, mCollector);
 
@@ -213,6 +222,9 @@ void NarrowPhaseQuery::CollideShape(const Shape *inShape, Vec3Arg inShapeScale,
 					// Notify collector of new body
 					mCollector.OnBody(body);
 
+					// Release the lock now, we have all the info we need in the transformed shape
+					lock.ReleaseLock();
+
 					// Do narrow phase collision check
 					ts.CollideShape(mShape, mShapeScale, mCenterOfMassTransform, mCollideShapeSettings, mCollector);
 
@@ -288,6 +300,9 @@ void NarrowPhaseQuery::CastShape(const ShapeCast &inShapeCast, const ShapeCastSe
 					// Notify collector of new body
 					mCollector.OnBody(body);
 
+					// Release the lock now, we have all the info we need in the transformed shape
+					lock.ReleaseLock();
+
 					// Do narrow phase collision check
 					ts.CastShape(mShapeCast, mShapeCastSettings, mCollector, mShapeFilter);
 
@@ -340,6 +355,9 @@ void NarrowPhaseQuery::CollectTransformedShapes(const AABox &inBox, TransformedS
 					// Notify collector of new body
 					mCollector.OnBody(body);
 
+					// Release the lock now, we have all the info we need in the transformed shape
+					lock.ReleaseLock();
+
 					// Do narrow phase collision check
 					ts.CollectTransformedShapes(mBox, mCollector);