Skip to content
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

[C# Client] Client Crashes on dynamic KeyDown Registration #23

Open
riffy opened this issue Jul 24, 2024 · 0 comments
Open

[C# Client] Client Crashes on dynamic KeyDown Registration #23

riffy opened this issue Jul 24, 2024 · 0 comments

Comments

@riffy
Copy link

riffy commented Jul 24, 2024

Description of the problem

When dynamically registering (outside of ctors) / subscribing to Alt.OnKeyDown event, crashes the client.
The ShowMenu function is invoked as an Action, meaning Action?.Invoke -> Alt.OnKeyDown -> crash.

private void ShowMenu()
{
  if (IsActive) return;
  // Register an alt keydown press since keycontroller will be disabled
  Alt.OnKeyDown += CloseMenuOnEscape;
  ....
}

/// <summary>
/// Closes the menu if Escape key is pressed
/// </summary>
private void CloseMenuOnEscape(Key key)
{
  if (key != Key.Escape) return;
  HideMenu();
}

private void HideMenu()
{
  if (!IsActive) return;
  Alt.OnKeyDown -= CloseMenuOnEscape;
  ...
}

The following Error is logged / produced:

[10:56:12][Error] FATAL EXCEPTION:
[10:56:12][Error] System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

   at System.Collections.Generic.HashSet`1.Enumerator.MoveNext()

   at AltV.Net.Client.Extensions.EnumerableExtensions.ForEachCatching[T](IEnumerable`1 enumerable, Action`1 action, String exceptionLocation)
Exception at event OnKeyDown: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at Client.Services.Input.Controllers.KeyController.OnKeyDown(Key key) in E:\MintV\gameserver\Client\Services\Allowance\Controllers\AvailabilityController.cs:line 66
   at AltV.Net.Client.Extensions.EnumerableExtensions.ForEachCatching[T](IEnumerable`1 enumerable, Action`1 action, String exceptionLocation) 

The only other OnKeyDown registration happens in a constructor, which doesn't crash the client:

public KeyController()
{
  Alt.OnKeyDown += OnKeyDown;
  Alt.OnKeyUp += OnKeyUp;
}

Environment

  • Client AsyncResource
  • .NET 6
  • C# Lang vers. 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant