diff --git a/Assets/Tests/InputSystem/CoreTests_Actions.cs b/Assets/Tests/InputSystem/CoreTests_Actions.cs index 260bb37de3..7ac1ab8044 100644 --- a/Assets/Tests/InputSystem/CoreTests_Actions.cs +++ b/Assets/Tests/InputSystem/CoreTests_Actions.cs @@ -11663,6 +11663,42 @@ public void Actions_DisablingAllActions_RemovesAllTheirStateMonitors() (InputManagerStateMonitors.StateChangeMonitorsForDevice x) => x.memoryRegions.All(r => r.sizeInBits == 0))); } + // Stopping Play Mode during an InputTestFixture test disposed the InputForUI provider's actions while the fixture's + // InputManager was still installed, which left their state change monitors in the InputManager they were enabled in. + [Test] + [Category("Actions")] + public void Actions_DisposingActionWhileAnotherInputManagerIsInstalled_RemovesItsStateMonitors() + { + var gamepad = InputSystem.AddDevice(); + var action = new InputAction(binding: "/buttonSouth"); + action.Enable(); + var state = action.m_ActionMap.m_State; + + var manager = InputSystem.manager; + using (var otherRuntime = new InputTestRuntime()) + { + var otherManager = InputManager.CreateAndInitialize(otherRuntime, null, fakeManagerForRemotingTests: true); + InputSystemTestHooks.TestHook_SwitchToDifferentInputManager(otherManager); + try + { + action.Dispose(); + } + finally + { + // Dispose first: it resets the current state buffers, which switching back reinstalls. + otherManager.Dispose(); + InputSystemTestHooks.TestHook_SwitchToDifferentInputManager(manager); + } + } + + var monitors = manager.m_StateMonitors.m_MonitorsPerDevice[gamepad.m_DeviceIndex]; + Assert.That(monitors.listeners.Take(monitors.count).Select(x => x.monitor), Has.None.SameAs(state)); + + // A monitor left behind would call into the action state whose memory Dispose() released. + Press(gamepad.buttonSouth); + LogAssert.NoUnexpectedReceived(); + } + // https://fogbugz.unity3d.com/f/cases/1367442/ [Test] [Category("Actions")] diff --git a/Packages/com.unity.inputsystem/CHANGELOG.md b/Packages/com.unity.inputsystem/CHANGELOG.md index a856ea9440..f070821cbe 100644 --- a/Packages/com.unity.inputsystem/CHANGELOG.md +++ b/Packages/com.unity.inputsystem/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fixed duplicate entries appearing in the Input Actions Editor binding path search for some devices, such as Mouse and XR controllers. [UUM-160646](https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-160646) - Fixed closed Input Device Debugger windows staying in memory until the next domain reload. [ISX-3179] - Fixed XR HMD device regeneration not copying the `canRunInBackground` parameter from its base layout, causing the XR app view to become locked to the tracking origin and frozen in place when the application regained focus after the headset went to sleep or a system menu was opened. [UUM-139700](https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-139700) +- Fixed a `NullReferenceException` on every mouse move in Play Mode after Play Mode was stopped during an `InputTestFixture` test with domain reload disabled. Actions disposed while another `InputManager` was installed left their state change monitors in the `InputManager` they were enabled in. ## [1.20.1] - 2026-10-05 diff --git a/Packages/com.unity.inputsystem/InputSystem/Runtime/Actions/InputActionState.cs b/Packages/com.unity.inputsystem/InputSystem/Runtime/Actions/InputActionState.cs index 439f826fff..55d845b902 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Runtime/Actions/InputActionState.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Runtime/Actions/InputActionState.cs @@ -143,6 +143,10 @@ private int GetControlBindingPriority(int controlIndex) => private bool m_InProcessControlStateChange; private bool m_Suppressed; private InputEventPtr m_CurrentlyProcessingThisEvent; + // The InputManager this state last installed its state change monitors in. Monitors and timeouts are removed + // from it rather than from InputSystem.manager, which can be a different manager by then, for example while + // InputTestFixture has its own manager installed. + private InputManager m_MonitorManager; private Action m_OnBeforeUpdateDelegate; private Action m_OnAfterUpdateDelegate; private static readonly ProfilerMarker k_InputInitialActionStateCheckMarker = new ProfilerMarker("InitialActionStateCheck"); @@ -1109,7 +1113,7 @@ internal void OnActionPriorityChanged(InputAction action) return; var clampedPriority = (ushort)InputAction.ClampPriority(action.Priority); - var manager = InputSystem.manager; + var manager = monitorManager; var bindingStartIndex = mapIndices[mapIndex].bindingStartIndex; var bindingCount = mapIndices[mapIndex].bindingCount; var bindingStatesPtr = memory.bindingStates; @@ -1267,7 +1271,10 @@ private void EnableControls(int mapIndex, int controlStartIndex, int numControls "Control start index out of range"); Debug.Assert(controlStartIndex + numControls <= totalControlCount, "Control range out of bounds"); - var manager = InputSystem.manager; + // Keep all of this state's monitors in one manager, so that they can all be removed from it again. + if (!HasEnabledControls()) + m_MonitorManager = InputSystem.manager; + var manager = m_MonitorManager; for (var i = 0; i < numControls; ++i) { var controlIndex = controlStartIndex + i; @@ -1304,7 +1311,7 @@ private void DisableControls(int mapIndex, int controlStartIndex, int numControl "Control start index out of range"); Debug.Assert(controlStartIndex + numControls <= totalControlCount, "Control range out of bounds"); - var manager = InputSystem.manager; + var manager = monitorManager; for (var i = 0; i < numControls; ++i) { var controlIndex = controlStartIndex + i; @@ -1355,6 +1362,9 @@ private void SetInitialStateCheckPending(BindingState* bindingStatePtr, bool val } } + // The manager holding this state's monitors, which its timeouts go to as well. + private InputManager monitorManager => m_MonitorManager ?? InputSystem.manager; + private bool IsControlEnabled(int controlIndex) { var intIndex = controlIndex / 32; @@ -1362,6 +1372,15 @@ private bool IsControlEnabled(int controlIndex) return (enabledControls[intIndex] & mask) != 0; } + private bool HasEnabledControls() + { + var intCount = (totalControlCount + 31) / 32; + for (var i = 0; i < intCount; ++i) + if (enabledControls[i] != 0) + return true; + return false; + } + private void SetControlEnabled(int controlIndex, bool state) { var intIndex = controlIndex / 32; @@ -2292,7 +2311,7 @@ internal void StartTimeout(float seconds, ref TriggerState trigger) Debug.Assert(trigger.controlIndex >= 0 && trigger.controlIndex < totalControlCount, "Control index out of range"); Debug.Assert(trigger.interactionIndex >= 0 && trigger.interactionIndex < totalInteractionCount, "Interaction index out of range"); - var manager = InputSystem.manager; + var manager = monitorManager; var currentTime = trigger.time; var control = controls[trigger.controlIndex]; var interactionIndex = trigger.interactionIndex; @@ -2321,7 +2340,7 @@ private void StopTimeout(int interactionIndex) ref var interactionState = ref interactionStates[interactionIndex]; - var manager = InputSystem.manager; + var manager = monitorManager; manager.RemoveStateChangeMonitorTimeout(this, interactionState.timerMonitorIndex, interactionIndex); // Update state.