Access violation in plugin: 'GetThreadedGfxDevice(...) returned nullptr.'

I’m having an illusive crash in a native (rendering) plugin. It is being called from a .NET assembly using interop. The crash only occurs relatively consistently in a build, but only sporadically in the editor and when it does, the editor crashes.
The weird thing is that it crashes as soon as the C# method with the first native call in it, enters scope (so not at the call itself). So it appears to have something to do with the plugin initialization. It seems related to timing and what other threads are running at that moment, because I can’t reproduce it in a simple project and as soon as I start removing random other parts of my application, the crash disappears (or not). I’ve tried replacing my native plugin with the Unity sample rendering plugin and even then it sometimes shows the same issue.

Below is the stacktrace of the exception. I was hoping someone with source code access to plugins.cpp could give a pointer (no pun intented) to what the “GetThreadedGfxDevice(…) returned nullptr.” exception in RegisterPlugin could indicate?

I’m using Unity 5.4, OpenVR (HTC Vive), 64-bit build.

0x00007FF6A3BCDC0B (MimesysConnect) [c:\buildslave\unity\build\runtime\misc\plugins.cpp:243] RegisterPlugin 
0x00007FF6A3BCDD7C (MimesysConnect) [c:\buildslave\unity\build\runtime\misc\plugins.cpp:301] InitializePlugin 
0x00007FF6A3BCE32B (MimesysConnect) [c:\buildslave\unity\build\runtime\misc\plugins.cpp:509] FindAndLoadUnityPlugin 
0x00007FFF3EB79929 (mono) mono_lookup_pinvoke_call
0x00007FFF3EB8D051 (mono) mono_marshal_string_to_utf16
0x00007FFF3EC32FD6 (mono) mono_set_break_policy
0x00007FFF3EC5362C (mono) mono_set_defaults
0x00007FFF3EC546F7 (mono) mono_set_defaults
0x00007FFF3EC54D15 (mono) mono_set_defaults
0x00007FFF3EC54DA8 (mono) mono_set_defaults
0x00007FFF3EC4CE7F (mono) mono_debugger_run_finally

Thanks very much! With your tip I solved my problem, which was exactly the same as yours, by simply calling the function to the external dll first in my main thread.

This was really a mystery for me to find. Can you tell me what tools (debugger/program output) you used to isolate it? Or did you just add a whole lot of Debug.Log statements in your code?

I had exactly the same Stack-trace as you, but I don’t see any reference to GetThreadedGfxDevice. Is that from a custom dll you use?

Maybe you should add a reply as an answer and mark this code as answered. This forum doesn’t even show your answer at first, but only when you press the text balloon link ‘Show 1’.