Search Unity

Question Unity crashes after using function pointer for JACK Audio

Discussion in 'Scripting' started by scratchin-benni, May 26, 2023.

  1. scratchin-benni

    scratchin-benni

    Joined:
    Apr 9, 2022
    Posts:
    7
    Hi,
    i am trying to a JACK client in Unity. For that, I need to register a process callback function. I did this using a delegate, from which I obtain a function pointer. Here's the code:

    The client already crashed randomly before when I didn't keep a reference to the delegate. This was probably caused by an access violation, because the function behind the function pointer was garbage collected. Now since this is fixed, it still crashes when I stop the application in the editor with the message "Fatal error in GC: Suspend thread loop failed.".

    Can anyone help me with this?

    Edit: I found out that when I set a breakpoint in OnDestroy() and I then let the script continue, the crash won't occur anymore. So it seems like the extra time avoids the crash. Hope that helps.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Runtime.InteropServices;
    4.  
    5. public class SimpleJackClient : MonoBehaviour
    6. {
    7.  
    8.     [DllImport("libjack64.dll")]
    9.     private static extern IntPtr jack_client_open(string clientName, int options, out int status);
    10.  
    11.     [DllImport("libjack64.dll")]
    12.     private static extern IntPtr jack_client_close(IntPtr jackClient);
    13.  
    14.     [DllImport("libjack64.dll")]
    15.     private static extern int jack_activate(IntPtr client);
    16.  
    17.     [DllImport("libjack64.dll")]
    18.     private static extern int jack_deactivate(IntPtr client);
    19.  
    20.     [DllImport("libjack64.dll")]
    21.     private static extern int jack_set_process_callback(IntPtr client, IntPtr callback, IntPtr arg);
    22.  
    23.     public delegate int CallbackDelegate(int samplesPerFrame, IntPtr arg);
    24.  
    25.     // Hold a reference to the process callback to avoid it to be garbage collected
    26.     private CallbackDelegate callbackDelegate;
    27.  
    28.     public string clientName = "UnityClient";
    29.  
    30.     private IntPtr jackClientPtr;
    31.  
    32.     private void Start()
    33.     {
    34.         int status;
    35.         jackClientPtr = jack_client_open(clientName, 0, out status);
    36.  
    37.         callbackDelegate = new CallbackDelegate(Process);
    38.         IntPtr processCallbackPtr = Marshal.GetFunctionPointerForDelegate(callbackDelegate);
    39.         jack_set_process_callback(jackClientPtr, processCallbackPtr, IntPtr.Zero);
    40.  
    41.         jack_activate(jackClientPtr);
    42.     }
    43.  
    44.     public int Process(int samplesPerFrame, IntPtr arg)
    45.     {          
    46.         return 0;
    47.     }
    48.  
    49.     private void OnDestroy()
    50.     {
    51.         if (jackClientPtr != IntPtr.Zero)
    52.         {
    53.             jack_deactivate(jackClientPtr);
    54.             jack_client_close(jackClientPtr);
    55.         }
    56.     }
    57.  
    58. }
    Stack trace:
    0x00007FFC70C44F2F (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\external\bdwgc\win32_threads.c:1248] GC_suspend
    0x00007FFC70C4466B (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\external\bdwgc\win32_threads.c:1324] GC_stop_world
    0x00007FFC70C449EB (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\external\bdwgc\alloc.c:782] GC_stopped_mark
    0x00007FFC70C45558 (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\external\bdwgc\alloc.c:580] GC_try_to_collect_inner
    0x00007FFC70C363E5 (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\external\bdwgc\alloc.c:1224] GC_gcollect
    0x00007FF6A9528B5D (Unity) CleanupAfterLoad
    0x00007FF6A957C50C (Unity) LoadSceneOperation::CompletePreloadManagerLoadSceneEditor
    0x00007FF6A957D09B (Unity) LoadSceneOperation::IntegrateMainThread
    0x00007FF6A9580C91 (Unity) PreloadManager::UpdatePreloadingSingleStep
    0x00007FF6A958126F (Unity) PreloadManager::WaitForAllAsyncOperationsToComplete
    0x00007FF6AA79106C (Unity) EditorSceneManager::RestoreSceneBackups
    0x00007FF6AA498FED (Unity) PlayerLoopController::ExitPlayMode
    0x00007FF6AA4A8AE1 (Unity) PlayerLoopController::SetIsPlaying
    0x00007FF6AA4ABF4B (Unity) Application::TickTimer
    0x00007FF6AA900FBA (Unity) MainMessageLoop
    0x00007FF6AA90588B (Unity) WinMain
    0x00007FF6ABCE315E (Unity) __scrt_common_main_seh
    0x00007FFD1A0426AD (KERNEL32) BaseThreadInitThunk
    0x00007FFD1BC6A9F8 (ntdll) RtlUserThreadStart
     
    Last edited: May 26, 2023
    ilmario likes this.