From dea2a8eb9cc34fa4ddcb86f0954985ba7f90a17a Mon Sep 17 00:00:00 2001 From: "Dane Z. Bush" Date: Sun, 13 Jan 2019 04:00:45 -0500 Subject: [PATCH] Check for null reference before callback During high-speed usage, callbacks will throw a null-reference error because the object has been deleted or collected before the callback can be executed on it. Add checks to see if the object is still valid. --- Source/SharpDX.XAudio2/VoiceShadow.cs | 32 ++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Source/SharpDX.XAudio2/VoiceShadow.cs b/Source/SharpDX.XAudio2/VoiceShadow.cs index 85a8e8184..f925ce7e5 100644 --- a/Source/SharpDX.XAudio2/VoiceShadow.cs +++ b/Source/SharpDX.XAudio2/VoiceShadow.cs @@ -64,35 +64,57 @@ public VoiceVtbl() static private void OnVoiceProcessingPassStartImpl(IntPtr thisObject, int bytes) { var shadow = ToShadow(thisObject); + if (shadow == null) + return; var callback = (VoiceCallback)shadow.Callback; + if (callback == null) + return; callback.OnVoiceProcessingPassStart(bytes); + } static private void OnVoiceProcessingPassEndImpl(IntPtr thisObject) { var shadow = ToShadow(thisObject); + if (shadow == null) + return; var callback = (VoiceCallback)shadow.Callback; + if (callback == null) + return; callback.OnVoiceProcessingPassEnd(); + } static private void OnStreamEndImpl(IntPtr thisObject) { var shadow = ToShadow(thisObject); + if (shadow == null) + return; var callback = (VoiceCallback)shadow.Callback; + if (callback == null) + return; callback.OnStreamEnd(); } static private void OnBufferStartImpl(IntPtr thisObject, IntPtr address) { var shadow = ToShadow(thisObject); + if (shadow == null) + return; var callback = (VoiceCallback)shadow.Callback; + if (callback == null) + return; callback.OnBufferStart(address); } static private void OnBufferEndImpl(IntPtr thisObject, IntPtr address) { var shadow = ToShadow(thisObject); + if (shadow == null) + return; var callback = (VoiceCallback)shadow.Callback; + if (callback == null) + return; callback.OnBufferEnd(address); } @@ -100,14 +122,22 @@ static private void OnBufferEndImpl(IntPtr thisObject, IntPtr address) static private void OnLoopEndImpl(IntPtr thisObject, IntPtr address) { var shadow = ToShadow(thisObject); + if (shadow == null) + return; var callback = (VoiceCallback)shadow.Callback; + if (callback == null) + return; callback.OnLoopEnd(address); } static private void OnVoiceErrorImpl(IntPtr thisObject, IntPtr bufferContextRef, int error) { var shadow = ToShadow(thisObject); + if (shadow == null) + return; var callback = (VoiceCallback)shadow.Callback; + if (callback == null) + return; callback.OnVoiceError(bufferContextRef, new Result(error)); } } @@ -117,4 +147,4 @@ protected override CppObjectVtbl GetVtbl get { return Vtbl; } } } -} \ No newline at end of file +}