- Home /
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.
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();
}
}
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.
Your answer
Follow this Question
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