From a2f962d72343810c77af2ad97a1ebfd3b3866805 Mon Sep 17 00:00:00 2001 From: Filip Henningsson Date: Fri, 17 Nov 2023 12:01:41 +0100 Subject: [PATCH] Wrapping contact/separation callbacks in try/catch to avoid freezes --- AGXUnity/ContactEventHandler.cs | 41 +++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/AGXUnity/ContactEventHandler.cs b/AGXUnity/ContactEventHandler.cs index 53dba3b2..be6fb2d7 100644 --- a/AGXUnity/ContactEventHandler.cs +++ b/AGXUnity/ContactEventHandler.cs @@ -504,7 +504,12 @@ private void TriggerSeparationCallbacks() separationData.Component2 = GetComponent( g2 ); separationData.Geometry1 = g1; separationData.Geometry2 = g2; - listener.SeparationCallback( separationData ); + try { + listener.SeparationCallback( separationData ); + } + catch ( System.Exception e ) { + Debug.LogException( e ); + } g1.ReturnToPool(); g2.ReturnToPool(); sep.ReturnToPool(); @@ -534,25 +539,31 @@ private void GenerateDataAndExecuteCallbacks( bool hasForce ) break; ref var contactData = ref GeometryContactHandler.ContactData[ (int)contactIndex ]; - var hasModifications = listener.ContactCallback( ref contactData ); - // Ignoring synchronization of any modifications when we are - // in post, when the changes won't have any effect. - if ( hasForce || !hasModifications ) - continue; + try { + bool hasModifications = listener.ContactCallback( ref contactData ); - var gc = GeometryContactHandler.Native.getGeometryContact( contactIndex ); + // Ignoring synchronization of any modifications when we are + // in post, when the changes won't have any effect. + if ( hasForce || !hasModifications ) + continue; - gc.setEnable( contactData.Enabled ); + var gc = GeometryContactHandler.Native.getGeometryContact( contactIndex ); - var gcPoints = gc.points(); - for ( int pointIndex = 0; pointIndex < contactData.Points.Count; ++pointIndex ) { - var gcPoint = gcPoints.at( (uint)pointIndex ); - contactData.Points[ pointIndex ].Synchronize( gcPoint ); - gcPoint.ReturnToPool(); + gc.setEnable( contactData.Enabled ); + + var gcPoints = gc.points(); + for ( int pointIndex = 0; pointIndex < contactData.Points.Count; ++pointIndex ) { + var gcPoint = gcPoints.at( (uint)pointIndex ); + contactData.Points[ pointIndex ].Synchronize( gcPoint ); + gcPoint.ReturnToPool(); + } + gcPoints.ReturnToPool(); + gc.ReturnToPool(); + } + catch ( System.Exception e ) { + Debug.LogException( e ); } - gcPoints.ReturnToPool(); - gc.ReturnToPool(); } } OnEndPerformingCallbacks();