Search Unity

[Bug] Memory leaks & crash when queueing events from threads other than the main thread

Discussion in 'Input System' started by doctorpangloss, Aug 31, 2022.

  1. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    Unity hangs with "Assertion failed on expression: 'gCurrentManagedTempMem != NULL'" when queuing events from a thread that isn't the main thread. Changing the allocator from Allocator.Temp to Allocator.Persistent when expanding the input event buffer shows this error:

    A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
    Unity.Collections.NativeArray`1:.ctor(Int32, Allocator, NativeArrayOptions)
    UnityEngine.InputSystem.LowLevel.InputEventBuffer:AllocateEvent(Int32, Int32, Allocator) (at Packages\com.unity.inputsystem@1.4.2\InputSystem\Events\InputEventBuffer.cs:214)
    UnityEngine.InputSystem.LowLevel.InputEventBuffer:AppendEvent(InputEvent*, Int32, Allocator) (at Packages\com.unity.inputsystem@1.4.2\InputSystem\Events\InputEventBuffer.cs:171)
    UnityEngine.InputSystem.LowLevel.InputEventStream:Write(InputEvent*) (at Packages\com.unity.inputsystem@1.4.2\InputSystem\Events\InputEventStream.cs:99)
    UnityEngine.InputSystem.InputManager:QueueEvent(InputEvent*) (at Packages\com.unity.inputsystem@1.4.2\InputSystem\InputManager.cs:1653)
    UnityEngine.InputSystem.InputManager:QueueEvent(StateEvent&) (at Packages\com.unity.inputsystem@1.4.2\InputSystem\InputManager.cs:1670)
    UnityEngine.InputSystem.InputSystem:QueueStateEvent(InputDevice, MouseState, Double) (at Packages\com.unity.inputsystem@1.4.2\InputSystem\InputSystem.cs:2590)
    (My Code)
    ...

    As far as I can tell, the input event buffers should be disposed eventually. What gives? Sounds like a bug in the implementation.

    This reopens https://issuetracker.unity3d.com/is...ateevent-cannot-be-called-from-another-thread

    Can I just... work around this for now?