unitycoder / UnityPointCloudViewer

Point Cloud Viewer and Tools for Unity
https://assetstore.unity.com/packages/tools/utilities/point-cloud-viewer-and-tools-16019?aid=1101lGti
128 stars 15 forks source link

Mainthread, null thread #144

Open unitycoder opened 3 months ago

unitycoder commented 3 months ago

Someone reported issues with mainthread.cs and suggested small fix (below this code)

Assets\PointCloudTools\PointCloudViewerDX11\Scripts\Common\MainThread.cs:

     IEnumerator Executer()
        {
            while (true)
            {
                yield return null;
                while (calls.Count > 0)
                {
                    calls[0].Execute(); // ← thread unsafe! GC may occur if it is added by another thread. 
                    lock (callsLock)
                    {
                        calls.RemoveAt(0);
                    }
                }

                while (functions.Count > 0)
                {
                       if(functions[0] != null){ 
                       functions[0](); // ← thread unsafe! GC may occur if it is added by another thread.
                    }
                    lock (functionsLock)
                    {
                        functions.RemoveAt(0);
                    }
                }
            }
        }

modified:

IEnumerator Executer()
        {
            while (true)
            {
                yield return null;
                while (calls.Count > 0)
                {
                    CallInfo call_work = null;
                    lock (callsLock) {
                        call_work = calls[0];
                    }

                    call_work.Execute();

                    lock (callsLock)
                    {
                        calls.RemoveAt(0);
                    }
                }

                while (functions.Count > 0)
                {
                    Func func_work = null;
                    lock (functionsLock)
                    {
                        func_work = functions[0];
                    }
            if( func_work != null){
                        func_work();
                    }
                    lock (functionsLock)
                    {
                        functions.RemoveAt(0);
                    }
                }
            }
        }