- Home /
calling Debug.Log from unmanaged thread crashes on experimental dotnet 4.6 runtime
EDIT: I should make it clear. platform is OSX UnityEditor.
hi, I have a plugin which uses unmanaged thread internally (using c++11 std::thread)
these threads call UnityEngine.Debug.Log via [UnmanagedFunctionPointer(CallingConvention.Cdecl)] delegate to show some information on Editor console.
when it runs under stable scripting runtime (dotnet 3.5), no problem occurs. but when I use experimental runtime (dotnet 4.6), it crashes inside Domain Unloader thread on entering play mode twice.
I could narrow down the problem to below little example.
when it runs on dotnet 3.5, it always shows "called back" 5 times then shows "done" .
but for dotnet 4.6, first time it runs ok, but crash on 2nd time (means stop 1st time run and restart).
native plugin (built as die.bundle)
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
static pthread_t *threads;
static int n_threads = 0;
void *proc(void *a) {
while (1) {
((void (*)())a)();
sleep(1);
}
return NULL;
}
extern void start_thread(int n, void (*func)()) {
threads = malloc(n * sizeof(pthread_t));
for (int i = 0; i < n; i++) {
pthread_create(threads + i, NULL, proc, func);
}
n_threads = n;
}
extern void stop_thread() {
for (int i = 0; i < n_threads; i++) {
pthread_cancel(threads[i]);
pthread_join(threads[i], NULL);
}
free(threads);
}
MonoBehaviour uses this plugin
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Collections;
using UnityEngine;
class Crash : MonoBehaviour
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void ThreadCB();
[DllImport ("die")]
extern static void start_thread(int n, ThreadCB cb);
[DllImport ("die")]
extern static void stop_thread();
bool stopped;
void Start()
{
start_thread(1, () => {
Debug.Log("called back");
});
stopped = false;
}
void Update() {
if (Time.time > 5.0f && !stopped) {
stop_thread();
Debug.Log("done");
stopped = true;
}
}
}
I suspect that it relate with this bug https://bugzilla.xamarin.com/show_bug.cgi?id=50537 and need cleanup mono thread state somehow (I estimate mono runtime auto attached thread state when unmanaged thread entering it, as MS dotnet runtime does). but check with mono_thread_internal_current_is_attached shows these threads does not seems to be attached MonoInternalThread. so now I completely stuck.
does anyone got same issue? if does, can solve this problem? any suggestions are welcome.
regards,
P.S. I think store log text in some kind of concurrent queue and call Debug.Log on main thread probably solves problem. but I would make it works as it does on dotnet 3.5 runtime.
Have you filed a bug report? Since if worked with 3.5 and a callback for multithreaded logs exist, it should work.
https://docs.unity3d.com/ScriptReference/Application-log$$anonymous$$essageReceivedThreaded.html
ya actually 3.5 never causes this issue. and if I remove all managed delegate call from unmanaged thread, issue disappeared on 4.6. so I guess it somehow related with unmanaged thread entering into mono runtime (likely to be mono_thread_attach related). now UnityQA $$anonymous$$m asking me more detail.
I haven't really done much from unmanaged code in Unity. However I suspect the main issue comes from the calling convetion and or GC issues. Your function pointer is not an unmanaged function pointer. You point to a managed method. The default calling convertion in managed code is STDCALL not CDECL. That means managed code methods cleanup the stack themselfs. I'm not sure if the JIT compiler can adjust the calling convention on the fly as it would need to change the actual return behaviour of the actual method.
The bugreport you've linked is about a managed thread which tries to call an unmanaged method. It doesn't seem to be related to your issue
Finally you do not ensure that you delegate is not garbage collected. See the examples shown over here. Your delegate lives in managed memory.
Answer by dokyo · Jan 02, 2018 at 10:11 PM
because so long after issue opened but problem not resolved, I put issue link here https://fogbugz.unity3d.com/default.asp?949512_dab6v5ranqbebqr5 so that anyone can see the further progress.
Your answer
![](https://koobas.hobune.stream/wayback/20220612133816im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
DLL build issues with Unity 3 1 Answer
Trying to use FreeImage won't work in Editor 1 Answer
Error Log: !query -> pending (Happening again!) 0 Answers
No line numbers in stack trace 1 Answer