Calling functions from Native DLL (C++) inconsistently crashes Unity


I created a custom C++ dll as a wrapper for the e57Format library that reads in data from .e57 point cloud files. (x64 only)

(GitHub - asmaloney/libE57Format: Library for reading & writing the E57 file format)

The DLL depends on other external DLLs such as xerces-c XML (also x64)

Everything is able to run fine without any issues, however this doesn’t always happen.

Sometimes when I attach the debugger and step through some code, it runs fine, other times when I do the same, it just straight up crashes.

And sometimes when I press “Play” without the debugger or without breakpoints, it also just straight up crashes. However, this is not always the case, since the same code can sometimes run just fine without any changes.

This might indicate a data race scenario, where either threads are run in an incorrect order or DLLs being loaded in incorrectly.

Note: I don’t use multithreading in the DLL.

The crash is shown below:


All DLLs are placed in Assets/Plugins and I do not get the Dllnotfoundexception

I am using the C++ debuglogger solution from:

I also call functions in C# from the C++ dll through delegate/function pointers and marshal data from C++ to C#, which already shows correct results. (Unsafe code + unsafe keyword)

I’ve tried:

  • enabling load on startup

  • platform settings to Windows and x64 only

  • calling the function from a different thread

None of these solutions have worked so far.
Any help would be appreciated.

Note: I seem to not find any logs from this, maybe I am looking in the wrong directories?

Update: (+ Fix)

I managed to stop the inconsistent crashing.

The issue was due to the absence of exception handling (try-catch-throw).

I do this on the C++ side and return an errorcode int back to C# to backtrack what the exception could be.

(Sometimes the e57 reader wouldn’t open and would therefore throw an exception, which I didn’t catch, hence the inconsistent crashing)

However, sending a “native” std::exception from C++ to C#/Unity will crash Unity with the same crash as mentioned before.

(C++ code snippet below: The Checke57Error returns an errorcode (int) ranging from 0 (success code, shouldn’t ever get returned) to 50, I return this to C# code and log it in the console as an extra debug measure.)