Search Unity

UnityEditor freezes on exit after GL.IssuePluginEvent with C# delegate

Discussion in 'Scripting' started by L-proger, May 28, 2019.

  1. L-proger

    L-proger

    Joined:
    Dec 5, 2012
    Posts:
    5
    Code (CSharp):
    1. using System;
    2. using System.Runtime.InteropServices;
    3. using UnityEngine;
    4.  
    5. public class TestDelegate : MonoBehaviour{
    6.  
    7.     public delegate void TestDelegateType(int value);
    8.     TestDelegateType testDelegate;
    9.  
    10.     void Start() {
    11.         testDelegate = test;
    12.         GL.IssuePluginEvent(Marshal.GetFunctionPointerForDelegate(testDelegate), 100500);
    13.     }
    14.  
    15.     void test(int value) {
    16.         Debug.Log("test: " + value);
    17.     }
    18. }
    19.  
    UnityEditor (Windows 10, x64) (or unity app build) freezes on exit with sample code from above.

    I attached a debugger and noticed, that mono is waiting for threads termination in function "mono_thread_suspend_all_other_threads". It sends APC to threads and wait for their termination, but after calling .NET delegate in render thread (GL.IssuePluginEvent) it looks like some unity thread can not longer transition to "alterable state" (and execute APC).

    I'm searching for some way to fix this without using additional native libraries, maybe somebody here can help me )
     
  2. L-proger

    L-proger

    Joined:
    Dec 5, 2012
    Posts:
    5
    Calling mono_thread_detach on RenderThread helps..