Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dokyo · Aug 29, 2017 at 06:10 PM · editordlldebug

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,

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dokyo · Aug 29, 2017 at 06:14 PM 0
Share

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.

avatar image hexagonius · Aug 30, 2017 at 03:15 AM 0
Share

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

avatar image dokyo · Sep 05, 2017 at 08:54 AM 0
Share

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.

avatar image Bunny83 · Jan 03, 2018 at 01:23 AM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Check project and cancel the build process 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges