ObjectDisposedException: The object was used after being disposed

We are using Unity Performance Reporting, and are getting these errors logged quite often:

MESSAGE
ObjectDisposedException: The object was used after being disposed.

STACK TRACE
System.Threading.WaitHandle.WaitOne (Int32 millisecondsTimeout, Boolean exitContext)
System.Threading.EventWaitHandle.Reset ()
Pathfinding.RVO.Simulator+Worker.Execute (Int32 task)
Pathfinding.RVO.Simulator.OnDestroy ()
Pathfinding.RVO.Simulator.Finalize ()
UnityEngine.UnhandledExceptionHandler:HandleUnhandledException(Object, UnhandledExceptionEventArgs)

I think this is also causing a native hard crash that we’ve been seeing, but the stack trace really is unusable. I suspect this because the native crash appears to be threading-related, and AFAIK A* is the only place where threads are used in our game.

EDIT: It’s actually very revealing, you can see the connection between the stack trace above and this one (Simulator_OnDestroy, Simulator_Finalize):

MESSAGE
Native Crash - il2cpp::icalls::mscorlib::System::Threading::NativeEventCalls::ResetEvent_internal(long) (Event.h)

STACK TRACE
Thread 3 (crashed)
0   mygame                          il2cpp::icalls::mscorlib::System::Threading::NativeEventCalls::ResetEvent_internal(long) (Event.h:39)
1   mygame                          Worker_Execute_m1040327398 (Bulk_Assembly-CSharp_7.cpp:4213)
2   mygame                          Simulator_OnDestroy_m4257592864 (Bulk_Assembly-CSharp_7.cpp:56324)
3   mygame                          Simulator_Finalize_m361839408 (Bulk_Assembly-CSharp_7.cpp:56356)
4   mygame                          RuntimeInvoker_Void_t1866870234(void (*)(), MethodInfo const*, void*, void**) (Il2CppInvokerTable.cpp:46240)
5   mygame                          il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**) (Runtime.cpp:471)
6   mygame                          il2cpp::gc::GarbageCollector::RunFinalizer(void*, void*) (GarbageCollector.cpp:156)
7   mygame                          GC_invoke_finalizers (finalize.c:992)
8   mygame                          il2cpp::gc::FinalizerThread(void*) (GarbageCollector.cpp:97)
9   mygame                          il2cpp::os::Thread::RunWrapper(void*) (Thread.cpp:110)
10  mygame                          il2cpp::os::ThreadImpl::ThreadStartWrapper(void*) (ThreadImpl.cpp:106)
11  libsystem_pthread.dylib             <system symbols missing> 
12  libsystem_pthread.dylib             <system symbols missing> 
13  libsystem_pthread.dylib             <system symbols missing>

Any ideas on what might be causing this? This appears to happen when switching scenes.

Note: This happened on an iPad Pro 10.5" with iOS 10.3.3.

Hi

That’s interesting. I have never seen that before.

From what I can tell, this seems to be a bug in the il2cpp compiler/runtime. My code never disposes the wait handle, so it must have been done by it being garbage collected or something like that. However that should (afaik) not happen if an object with a finalizer can still reach it (which is the case here).

In any case the finalizer is only a safeguard in case one was using an RVO simulator without using the Unity component wrapper for it. You should be able to safely remove it. Find this code in the RVOCoreSimulator.cs script and remove it:

/** Terminates any worker threads */
~Simulator () {
	OnDestroy();
}

Let me know if this resolves the issue.