- Home /
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
I've finally been able to reproduce it in a small sample project with the Unity sample rendering plugin. It turns out the first call to the dll should be done on the main thread or the error above occurs (only in a build though).
For those interested, the to reproduce it:
public class PluginTest : $$anonymous$$onoBehaviour {
[DllImport("RenderingPlugin")]
public static extern void SetTimeFromUnity(float time);
TcpClient TcpClient;
// Use this for initialization
void Start () {
Connect("127.0.0.1", 13654);
}
void Connect(string hostname, int port)
{
this.TcpClient = new TcpClient();
this.TcpClient.BeginConnect(hostname, port, new AsyncCallback(ConnectFinished), null);
}
private void ConnectFinished(IAsyncResult ar)
{
Debug.Log("Calling plugin from thread : " + Thread.CurrentThread.$$anonymous$$anagedThreadId);
SetTimeFromUnity(1.0f);
try
{
this.TcpClient.EndConnect(ar);
}
catch (SocketException)
{
Debug.Log("Error connecting!");
}
}
}
Answer by steven3Dim · Jul 06, 2017 at 01:14 PM
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'.
Your answer
Follow this Question
Related Questions
Unity Crashes Every time I Try To Run This Code 1 Answer
Unity Crashes On Startup 1 Answer
Why is Unity 4.3 so unstable? 0 Answers
Cannot open specific project -1 Answers
How do I report a bug in Unity? 2 Answers