DarkRift
DarkRift copied to clipboard
Closing the DarkRiftServer in XmlUnityServer throws ObjectDisposedException
Quick Description
After disposing the DarkRiftServer object, the XmlUnityServer class will still try to call methods on the server in its Update method, which will throw ObjectDisposedExceptions each frame.
Explanation
After creating a server object with XmlUnityServer.Create() from the example config, unity will call the update loop each frame with the following code.
private void Update()
{
//Execute all queued dispatcher tasks
if (Server != null)
{
Server.ExecuteDispatcherTasks();
}
}
Calling the Close method will call
public void Close()
{
if (Server != null)
{
Server.Dispose()
}
}
which just disposes the server, but wont nullify the reference. Therefor the above shown update loop will continue to call it every frame, which will result in errors.
Adapting the Update loop to Server != null && !Server.Disposed
will still throw one ObjectDisposedException, which is due to threading I assume (relatively new to all this, so I am not really sure).
To solve it for me, I added a new private bool disposeServer and the following code to Close() and Update()
Update():
private void Update()
{
//Execute all queued dispatcher tasks
if (Server != null)
{
Server.ExecuteDispatcherTasks();
}
if (disposeServer)
{
Server.Dispose();
Server = null;
disposeServer = false;
}
}
Close():
public void Close()
{
if (Server != null)
{
disposeServer = true;
}
}
Another thing to mention, I am using the version on the unity workshop, which is 2.9.1, apparently its a bit outdated.