Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by gnema · May 30, 2013 at 07:07 AM · c++threadsdllnotfoundexceptionmono-behaviour

dll not found exception when c++ methods are call from multiple threads

Hi,

I have few c++ dll methods being called from my c# script. When I call the methods from main thread they work fine. But when I call the methods simultaneously from multiple threads I get "dll not found" exception. I am using normal c# threads. And in those threads I am not calling any Unity functions.

Sometimes I get garbage exceptions too.

The c++ methods are thread safe.

Thanks and I appreciate any help.

Comment
Add comment · Show 2
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 gnema · May 31, 2013 at 04:29 AM 1
Share

I have tried creating a simple c++ dll which has only few methods. I noticed that some very simple methods (which does not involve pointers) work fine in multi-threading with unity.

However when I call a method which creates a new Int and then call another method which deletes this Int (in c++), this throws exception under multi threading.

I am posting this sample code.

c++ methods:

 // simpledll.cpp : Defines the exported functions for the DLL application.
 //
 
 #include "stdafx.h"
 #include <vector>
 #ifdef __cplusplus
 extern "C" 
 #endif
 __declspec(dllexport) float __stdcall  blankfloat()
 {
     return (5) ;
 }
 #ifdef __cplusplus
 extern "C" 
 #endif
 __declspec(dllexport) void* __stdcall  blankpointer()
 {
     return (void*)(NULL) ;
 }
 #ifdef __cplusplus
 extern "C" 
 #endif
 __declspec(dllexport) void* __stdcall  createIntPointer()
 {
     return (void*)(new int()) ;
 }
 #ifdef __cplusplus
 extern "C" 
 #endif
 __declspec(dllexport) void __stdcall  deleteIntPointer(void* intPtr)
 {
     delete (int*)intPtr;
 }

c# code: using UnityEngine; using System.Collections.Generic; using System.Threading; using System; using System.IO; using System.Runtime.InteropServices;

 public class $$anonymous$$ultiThreadTest : $$anonymous$$onoBehaviour {
     
     [DllImport ("simpledll", EntryPoint="blankpointer")]
     private static extern IntPtr blankpointer ();
     
     [DllImport ("simpledll", EntryPoint="blankfloat")]
     private static extern float blankfloat ();
     
     [DllImport ("simpledll", EntryPoint="createIntPointer")]
     private static extern IntPtr createIntPointer ();
     
     [DllImport ("simpledll", EntryPoint="deleteIntPointer")]
     private static extern void deleteIntPointer (IntPtr ptr);
     
     List<Thread> threadList = new List<Thread>();
     bool alive = true;
     int numOfThreads = 2; 
     
     StreamWriter file;
     // Use this for initialization
     void Start () {
         
         file = new StreamWriter(Application.strea$$anonymous$$gAssetsPath + "/threadlog.log");
         file.AutoFlush = true;
         
         print(blankfloat().ToString());
         
         for(int i = 0; i < numOfThreads; i++)
         {
             Thread newThread =  new Thread(new ThreadStart(ReadThreaded));
             
             threadList.Add(newThread);
             
             newThread.Start();
         }
     }
     
     void ReadThreaded()
     {
         while(alive)
         {
             try
             {
                 
                 IntPtr ptr = createIntPointer();
         
                 
                 
                 deleteIntPointer(ptr);
                 
             }
             catch(Exception exp)
             {
                 lock(this)
                 {
                     file.WriteLine("Exception $$anonymous$$essage: " + exp.$$anonymous$$essage);
                     
                     if(exp.GetBaseException() != null)
                     {
                         file.WriteLine("Base Exception: " + exp.GetBaseException().$$anonymous$$essage);
                     }
                     if(exp.InnerException != null)
                     {
                         file.WriteLine("Inner Exception: " + exp.InnerException.$$anonymous$$essage);
                     }
                 }
             }
         }
     }
     // Update is called once per frame
     void Update () {
     
     }
             
     void OnDestroy()
     {
         alive = false;
         foreach(Thread t in threadList)
         {
             t.Join();
         }
                 
         threadList.Clear();
     }
 }
 
avatar image gnema · May 31, 2013 at 05:06 AM 0
Share

Just to add, I used the above c++ dll code in a Visual C# (.net) project. I was able to run 20 parallel threads with no exceptions at all.

I am not using any Unity functionality in the above threads. This makes me think whether this problem is related to $$anonymous$$ono.

0 Replies

· Add your reply
  • Sort: 

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

13 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

Related Questions

C++ Callback in another thread 1 Answer

Native Plugin works in editor but not in build 0 Answers

DLLNotFoundException 0 Answers

DllNotFoundException in standalone 2 Answers

Failed to load DLL with error "The specified module cannot be found." 1 Answer


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