Skip to content

FIX: Reenabling the VirtualMouseInput component may lead to NullReferenceException #2175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ however, it has to be formatted properly to pass verification tests.
- Fixed Inspector Window being refreshed all the time when a PlayerInput component is present with Invoke Unity Events nofication mode chosen [ISXB-1448](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1448)
- Fixed an issue where an action with a name containing a slash "/" could not be found via `InputActionAsset.FindAction(string,bool)`. [ISXB-1306](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1306).
- Fixed Gamepad stick up/down inputs that were not recognized in WebGL. [ISXB-1090](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1090)
- Fixed reenabling the VirtualMouseInput component may sometimes lead to NullReferenceException. [ISXB-1096](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1096)
- Fixed PlayerInput component automatically switching away from the default ActionMap set to 'None'.
- Fixed a console error being shown when targeting visionOS builds in 2022.3.
- Fixed a Tap Interaction issue with analog controls. The Tap interaction would keep re-starting after timeout. [ISXB-627](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-627)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,8 @@ public void ResetActionState(int actionIndex, InputActionPhase toPhase = InputAc
// Wipe state.
actionState->phase = toPhase;
actionState->controlIndex = kInvalidIndex;
actionState->bindingIndex = memory.actionBindingIndices[memory.actionBindingIndicesAndCounts[actionIndex]];
var idx = memory.actionBindingIndicesAndCounts[actionIndex];
actionState->bindingIndex = memory.actionBindingIndices != null ? memory.actionBindingIndices[idx] : 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it even be smart to set bindingIndex to -1 to indicate it's not a valid index?
It would allow doing (actionState->bindingIndex >= 0) as an indirect null-check, but if other indices are zero it makes sense to keep it at zero for consistency

Copy link
Collaborator Author

@K-Tone K-Tone May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very good question to be honest. I'm surprised it's not been set to -1 before. I should see if that's safe to change in order not to break some stuff somewhere, this code is kinda interconnected quite a bit.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, it was just a thought and not something I consider a must-have if it adds complexity

actionState->interactionIndex = kInvalidIndex;
actionState->startTime = 0;
actionState->time = 0;
Expand Down Expand Up @@ -2879,6 +2880,9 @@ internal TValue ReadValue<TValue>(int bindingIndex, int controlIndex, bool ignor
internal TValue ApplyProcessors<TValue>(int bindingIndex, TValue value, InputControl<TValue> controlOfType = null)
where TValue : struct
{
if (totalBindingCount == 0)
return value;

var processorCount = bindingStates[bindingIndex].processorCount;
if (processorCount > 0)
{
Expand Down Expand Up @@ -4139,6 +4143,16 @@ public struct UnmanagedMemory : IDisposable

public ActionMapIndices* mapIndices;

private static byte* AllocFromBlob(ref byte* top, int size)
{
if (size == 0)
return null;

var allocation = top;
top += size;
return allocation;
}

public void Allocate(int mapCount, int actionCount, int bindingCount, int controlCount, int interactionCount, int compositeCount)
{
Debug.Assert(basePtr == null, "Memory already allocated! Free first!");
Expand All @@ -4164,17 +4178,17 @@ public void Allocate(int mapCount, int actionCount, int bindingCount, int contro
// NOTE: This depends on the individual structs being sufficiently aligned in order to not
// cause any misalignment here. TriggerState, InteractionState, and BindingState all
// contain doubles so put them first in memory to make sure they get proper alignment.
actionStates = (TriggerState*)ptr; ptr += actionCount * sizeof(TriggerState);
interactionStates = (InteractionState*)ptr; ptr += interactionCount * sizeof(InteractionState);
bindingStates = (BindingState*)ptr; ptr += bindingCount * sizeof(BindingState);
mapIndices = (ActionMapIndices*)ptr; ptr += mapCount * sizeof(ActionMapIndices);
controlMagnitudes = (float*)ptr; ptr += controlCount * sizeof(float);
compositeMagnitudes = (float*)ptr; ptr += compositeCount * sizeof(float);
controlIndexToBindingIndex = (int*)ptr; ptr += controlCount * sizeof(int);
controlGroupingAndComplexity = (ushort*)ptr; ptr += controlCount * sizeof(ushort) * 2;
actionBindingIndicesAndCounts = (ushort*)ptr; ptr += actionCount * sizeof(ushort) * 2;
actionBindingIndices = (ushort*)ptr; ptr += bindingCount * sizeof(ushort);
enabledControls = (int*)ptr; ptr += (controlCount + 31) / 32 * sizeof(int);
actionStates = (TriggerState*)AllocFromBlob(ref ptr, actionCount * sizeof(TriggerState));
interactionStates = (InteractionState*)AllocFromBlob(ref ptr, interactionCount * sizeof(InteractionState));
bindingStates = (BindingState*)AllocFromBlob(ref ptr, bindingCount * sizeof(BindingState));
mapIndices = (ActionMapIndices*)AllocFromBlob(ref ptr, mapCount * sizeof(ActionMapIndices));
controlMagnitudes = (float*)AllocFromBlob(ref ptr, controlCount * sizeof(float));
compositeMagnitudes = (float*)AllocFromBlob(ref ptr, compositeCount * sizeof(float));
controlIndexToBindingIndex = (int*)AllocFromBlob(ref ptr, controlCount * sizeof(int));
controlGroupingAndComplexity = (ushort*)AllocFromBlob(ref ptr, controlCount * sizeof(ushort) * 2);
actionBindingIndicesAndCounts = (ushort*)AllocFromBlob(ref ptr, actionCount * sizeof(ushort) * 2);
actionBindingIndices = (ushort*)AllocFromBlob(ref ptr, bindingCount * sizeof(ushort));
enabledControls = (int*)AllocFromBlob(ref ptr, (controlCount + 31) / 32 * sizeof(int));
}

public void Dispose()
Expand Down