Question by
pixelord · Apr 07, 2020 at 07:48 AM ·
pluginnative pluginthreading
Accessing Native plugin from a thread.
I am trying to build a native plugin that would process a rendertexture in C++ code. My C# script creates a thread and I call the native plugin function from that thread.
lock(thisLock)
{
this.textureData = screenShot.GetRawTextureData();
//SetRenderTextureFromUnity(this.textureData, this.textureData.Length);
Debug.Log(string.Format("Rawtexture size {0}", textureData.Length));
}
new System.Threading.Thread(() =>
{
lock(thisLock)
{
SetRenderTextureFromUnity(this.textureData, this.textureData.Length);
Debug.Log("Writing to shared memory");
}
}).Start();
The problem is this code works in editor but after I build it, the player crashed. I did some testing, and it looks like, if I call the plugin from main thread, it works fine. Liek below:
this.textureData = screenShot.GetRawTextureData();
SetRenderTextureFromUnity(this.textureData, this.textureData.Length);
Debug.Log(string.Format("Rawtexture size {0}", textureData.Length));
I inevstigated my crash report, and it shows this error: (Last few line of the error dump is quite interesting. Can anyone give me an idea, what might be going on?)
TracerRenderer by Unity Technologies [version: Unity 2018.4.15f1_13f5a1bf9ca1]
UnityPlayer.dll caused an Access Violation (0xc0000005)
in module UnityPlayer.dll at 0033:39b8e879.
Error occurred at 2020-04-07_100058.
E:\Development\BoxerUnityBuild\TracerRenderer.exe, run by SimMachine01.
23% physical memory in use.
32572 MB physical memory [25051 MB free].
462 MB process peak paging file [462 MB used].
272 MB process peak working set [272 MB used].
System Commit Total/Limit/Peak: 9499MB/42300MB/9933MB
System Physical Total/Available: 32572MB/25051MB
System Process Count: 264
System Thread Count: 3344
System Handle Count: 99209
Disk space data for 'C:\Users\SIMMAC~1\AppData\Local\Temp\Unity Technologies\TracerRenderer\Crashes\Crash_2020-04-07_000057511\': 52294942720 bytes free of 255266385920 total.
Read from location 0000000000000000 caused an access violation.
Context:
RDI: 0x00007ffda3b10000 RSI: 0x00000215186f4148 RAX: 0x0000000000000000
RBX: 0x00000215186e7301 RCX: 0x0000000000000000 RDX: 0x0000000000000000
RIP: 0x00007ffd39b8e879 RBP: 0x000000abcc6fc910 SegCs: 0x0000000000000033
EFlags: 0x0000000000010202 RSP: 0x000000abcc6fc6f0 SegSs: 0x000000000000002b
R8: 0x0000000000000000 R9: 0x00007ffd39380000 R10: 0x0000000000000000
R11: 0x00000215186f4100 R12: 0x000000abcc6fd9a0 R13: 0x000002152bfbe860
R14: 0x000000abcc6fc7c0 R15: 0x0000000000000000
Bytes at CS:EIP:
48 8b 10 ff 92 68 05 00 00 eb 02 32 db e8 a5 42
Mono DLL loaded successfully at 'E:\Development\BoxerUnityBuild\MonoBleedingEdge\EmbedRuntime\mono-2.0-bdwgc.dll'.
Stack Trace of Crashed Thread 6976:
0x00007FFD39B8E879 (UnityPlayer) UnityMain
0x00007FFD39B8D4CA (UnityPlayer) UnityMain
0x00007FFD4A931B27 (mono-2.0-bdwgc) mono_lookup_pinvoke_call
0x00007FFD4A944466 (mono-2.0-bdwgc) mono_install_ftnptr_eh_callback
0x00007FFD4AA19E92 (mono-2.0-bdwgc) mono_set_defaults
0x00007FFD4A9E1C79 (mono-2.0-bdwgc) mono_unity_unlock_dynamic_function_access_tables64
0x00007FFD4A9E6841 (mono-2.0-bdwgc) mono_unity_unlock_dynamic_function_access_tables64
0x00007FFD4A9EAC62 (mono-2.0-bdwgc) mono_get_runtime_build_info
0x00007FFD4AAC6EBC (mono-2.0-bdwgc) mono_unity_backtrace_from_context
0x0000021518451073 (Mono JIT Code) <unknown class>.<unknown method>()
0x0000021531AC15D6 (mscorlib) System.Threading.ThreadHelper.ThreadStart_Context()
0x0000021531AC0FDE (mscorlib) System.Threading.ExecutionContext.RunInternal()
0x0000021531AC0D2B (mscorlib) System.Threading.ExecutionContext.Run()
0x0000021531AC0B3B (mscorlib) System.Threading.ExecutionContext.Run()
0x0000021531AC0953 (mscorlib) System.Threading.ThreadHelper.ThreadStart()
0x0000021531AC08B4 (mscorlib) System.Object.runtime_invoke_void__this__()
0x00007FFD4A9EB7B0 (mono-2.0-bdwgc) mono_get_runtime_build_info
0x00007FFD4A971892 (mono-2.0-bdwgc) mono_perfcounters_init
0x00007FFD4A97AA62 (mono-2.0-bdwgc) mono_runtime_invoke_array
0x00007FFD4A994CFF (mono-2.0-bdwgc) mono_threads_set_shutting_down
0x00007FFD4A994A46 (mono-2.0-bdwgc) mono_threads_set_shutting_down
0x00007FFDAF8E7BD4 (KERNEL32) BaseThreadInitThunk
0x00007FFDAFC4CED1 (ntdll) RtlUserThreadStart
Stacks for Running Threads:
Call Stack for Thread 6992:
0x00007FFDAFC7C144 (ntdll) NtWaitForSingleObject
0x00007FFDADA18BC3 (KERNELBASE) WaitForSingleObjectEx
ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FFD39931E1E)
0x00007FFD39931E1E (UnityPlayer) (function-name not available)
ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FFD3991C87B)
0x00007FFD3991C87B (UnityPlayer) (function-name not available)
0x00007FFD39ABFB30 (UnityPlayer) UnityMain
0x00007FFD39A8869F (UnityPlayer) UnityMain
0x000002152D59DC03 (UnityEngine.CoreModule) UnityEngine.Texture2D.ReadPixelsImpl_Injected()
0x000002152D59DB43 (UnityEngine.CoreModule) UnityEngine.Texture2D.ReadPixelsImpl()
0x000002152D59D94B (UnityEngine.CoreModule) UnityEngine.Texture2D.ReadPixels()
0x000002152D59D8BB (UnityEngine.CoreModule) UnityEngine.Texture2D.ReadPixels()
0x000002152D595A93 (Assembly-CSharp) Capture.Update()
0x000002152D1BDAD0 (mscorlib) System.Object.runtime_invoke_void__this__()
0x00007FFD4A9EB7B0 (mono-2.0-bdwgc) mono_get_runtime_build_info
0x00007FFD4A971892 (mono-2.0-bdwgc) mono_perfcounters_init
0x00007FFD4A97A88F (mono-2.0-bdwgc) mono_runtime_invoke
0x00007FFD39C81674 (UnityPlayer) UnityMain
0x00007FFD39C7EEA0 (UnityPlayer) UnityMain
0x00007FFD39C6A4A9 (UnityPlayer) UnityMain
0x00007FFD39C6A663 (UnityPlayer) UnityMain
0x00007FFD39A58620 (UnityPlayer) UnityMain
0x00007FFD39B88B67 (UnityPlayer) UnityMain
0x00007FFD39B88C09 (UnityPlayer) UnityMain
0x00007FFD39B8ABBF (UnityPlayer) UnityMain
ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FFD39963BD5)
0x00007FFD39963BD5 (UnityPlayer) (function-name not available)
ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FFD3996220A)
0x00007FFD3996220A (UnityPlayer) (function-name not available)
ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FFD39966A90)
0x00007FFD39966A90 (UnityPlayer) (function-name not available)
0x00007FFD3996ACCB (UnityPlayer) UnityMain
ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FF740A211FA)
0x00007FF740A211FA (TracerRenderer) (function-name not available)
0x00007FFDAF8E7BD4 (KERNEL32) BaseThreadInitThunk
0x00007FFDAFC4CED1 (ntdll) RtlUserThreadStart
Comment