Skip to content

Commit 6601e91

Browse files
authored
FIX: Reenabling the VirtualMouseInput component may lead to NullReferenceException (#2175)
1 parent f52ba87 commit 6601e91

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ however, it has to be formatted properly to pass verification tests.
2222
- 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)
2323
- 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).
2424
- Fixed Gamepad stick up/down inputs that were not recognized in WebGL. [ISXB-1090](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1090)
25+
- Fixed reenabling the VirtualMouseInput component may sometimes lead to NullReferenceException. [ISXB-1096](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1096)
2526
- Fixed PlayerInput component automatically switching away from the default ActionMap set to 'None'.
2627
- Fixed a console error being shown when targeting visionOS builds in 2022.3.
2728
- 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)

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionState.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,8 @@ private void RestoreActionStatesAfterReResolvingBindings(UnmanagedMemory oldStat
593593
if (newBindingState.isComposite)
594594
{
595595
var compositeIndex = newBindingState.compositeOrCompositeBindingIndex;
596-
memory.compositeMagnitudes[compositeIndex] = oldState.compositeMagnitudes[compositeIndex];
596+
if (oldState.compositeMagnitudes != null)
597+
memory.compositeMagnitudes[compositeIndex] = oldState.compositeMagnitudes[compositeIndex];
597598
}
598599

599600
var actionIndex = newBindingState.actionIndex;
@@ -872,7 +873,8 @@ public void ResetActionState(int actionIndex, InputActionPhase toPhase = InputAc
872873
// Wipe state.
873874
actionState->phase = toPhase;
874875
actionState->controlIndex = kInvalidIndex;
875-
actionState->bindingIndex = memory.actionBindingIndices[memory.actionBindingIndicesAndCounts[actionIndex]];
876+
var idx = memory.actionBindingIndicesAndCounts[actionIndex];
877+
actionState->bindingIndex = memory.actionBindingIndices != null ? memory.actionBindingIndices[idx] : 0;
876878
actionState->interactionIndex = kInvalidIndex;
877879
actionState->startTime = 0;
878880
actionState->time = 0;
@@ -2879,6 +2881,9 @@ internal TValue ReadValue<TValue>(int bindingIndex, int controlIndex, bool ignor
28792881
internal TValue ApplyProcessors<TValue>(int bindingIndex, TValue value, InputControl<TValue> controlOfType = null)
28802882
where TValue : struct
28812883
{
2884+
if (totalBindingCount == 0)
2885+
return value;
2886+
28822887
var processorCount = bindingStates[bindingIndex].processorCount;
28832888
if (processorCount > 0)
28842889
{
@@ -4139,6 +4144,16 @@ public struct UnmanagedMemory : IDisposable
41394144

41404145
public ActionMapIndices* mapIndices;
41414146

4147+
private static byte* AllocFromBlob(ref byte* top, int size)
4148+
{
4149+
if (size == 0)
4150+
return null;
4151+
4152+
var allocation = top;
4153+
top += size;
4154+
return allocation;
4155+
}
4156+
41424157
public void Allocate(int mapCount, int actionCount, int bindingCount, int controlCount, int interactionCount, int compositeCount)
41434158
{
41444159
Debug.Assert(basePtr == null, "Memory already allocated! Free first!");
@@ -4164,17 +4179,17 @@ public void Allocate(int mapCount, int actionCount, int bindingCount, int contro
41644179
// NOTE: This depends on the individual structs being sufficiently aligned in order to not
41654180
// cause any misalignment here. TriggerState, InteractionState, and BindingState all
41664181
// contain doubles so put them first in memory to make sure they get proper alignment.
4167-
actionStates = (TriggerState*)ptr; ptr += actionCount * sizeof(TriggerState);
4168-
interactionStates = (InteractionState*)ptr; ptr += interactionCount * sizeof(InteractionState);
4169-
bindingStates = (BindingState*)ptr; ptr += bindingCount * sizeof(BindingState);
4170-
mapIndices = (ActionMapIndices*)ptr; ptr += mapCount * sizeof(ActionMapIndices);
4171-
controlMagnitudes = (float*)ptr; ptr += controlCount * sizeof(float);
4172-
compositeMagnitudes = (float*)ptr; ptr += compositeCount * sizeof(float);
4173-
controlIndexToBindingIndex = (int*)ptr; ptr += controlCount * sizeof(int);
4174-
controlGroupingAndComplexity = (ushort*)ptr; ptr += controlCount * sizeof(ushort) * 2;
4175-
actionBindingIndicesAndCounts = (ushort*)ptr; ptr += actionCount * sizeof(ushort) * 2;
4176-
actionBindingIndices = (ushort*)ptr; ptr += bindingCount * sizeof(ushort);
4177-
enabledControls = (int*)ptr; ptr += (controlCount + 31) / 32 * sizeof(int);
4182+
actionStates = (TriggerState*)AllocFromBlob(ref ptr, actionCount * sizeof(TriggerState));
4183+
interactionStates = (InteractionState*)AllocFromBlob(ref ptr, interactionCount * sizeof(InteractionState));
4184+
bindingStates = (BindingState*)AllocFromBlob(ref ptr, bindingCount * sizeof(BindingState));
4185+
mapIndices = (ActionMapIndices*)AllocFromBlob(ref ptr, mapCount * sizeof(ActionMapIndices));
4186+
controlMagnitudes = (float*)AllocFromBlob(ref ptr, controlCount * sizeof(float));
4187+
compositeMagnitudes = (float*)AllocFromBlob(ref ptr, compositeCount * sizeof(float));
4188+
controlIndexToBindingIndex = (int*)AllocFromBlob(ref ptr, controlCount * sizeof(int));
4189+
controlGroupingAndComplexity = (ushort*)AllocFromBlob(ref ptr, controlCount * sizeof(ushort) * 2);
4190+
actionBindingIndicesAndCounts = (ushort*)AllocFromBlob(ref ptr, actionCount * sizeof(ushort) * 2);
4191+
actionBindingIndices = (ushort*)AllocFromBlob(ref ptr, bindingCount * sizeof(ushort));
4192+
enabledControls = (int*)AllocFromBlob(ref ptr, (controlCount + 31) / 32 * sizeof(int));
41784193
}
41794194

41804195
public void Dispose()

0 commit comments

Comments
 (0)