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-side error when Action<...>.Invoke() #2383

Open
1 task done
axeok opened this issue Nov 30, 2024 · 2 comments
Open
1 task done

C# client-side error when Action<...>.Invoke() #2383

axeok opened this issue Nov 30, 2024 · 2 comments
Labels
Class: bug Something isn't working

Comments

@axeok
Copy link

axeok commented Nov 30, 2024

Description of the problem

Up to recently everything was okay until something happened with Actions in C#.

I've met trouble invoking action with primitive types, while I never encountered this issue before (I was using it succesfully before).

Invoke action with primitive type (maybe any structure) - Object reference not set to an instance of an object.
Invoke action with string (and custom classes) - OK.
Dynamic invoke with primitive type - OK.
Foreach invoke action.GetInvocationList() - OK.

Error happens only with .Invoke() method with primitive(structure) types.

With client config disableRestrictedSandbox = true error was gone and .Invoke() works perfectly.

image
image
image

Reproduction steps

using AltV.Net.Client;
using AltV.Net.Client.Elements.Data;

namespace TestProject.Client;

public class TestResource : Resource
{
    private delegate void TestDelegate(int value);
    
    private Action<int>? _test1;
    private Action<ConsoleColor>? _test2;
    private Action<string>? _test3;
    private TestDelegate? _test4;

    public override void OnStart()
    {
        Alt.Logger.LogInfo("Started TestResource");

        _test1 += value => Alt.Logger.LogInfo($"Test1: {value}");
        _test2 += value => Alt.Logger.LogInfo($"Test2: {value}");
        _test3 += value => Alt.Logger.LogInfo($"Test3: {value}");
        _test4 += value => Alt.Logger.LogInfo($"Test4: {value}");

        Alt.OnKeyDown += AltOnOnKeyDown;
    }

    private void AltOnOnKeyDown(Key key)
    {
        if (key != Key.J)
        {
            return;
        }

        try
        {
            _test1?.Invoke(5);
        }
        catch (Exception e)
        {
            Alt.Logger.LogError($"_test1: {e}");
        }

        try
        {
            _test2?.Invoke(ConsoleColor.Green);
        }
        catch (Exception e)
        {
            Alt.Logger.LogError($"_test2: {e}");
        }

        try
        {
            _test3?.Invoke("Invoke");
        }
        catch (Exception e)
        {
            Alt.Logger.LogError($"_test3: {e}");
        }
        
        _test4?.Invoke(11111);

        _test1?.DynamicInvoke(123);
        _test2?.DynamicInvoke(ConsoleColor.Red);
        _test3?.DynamicInvoke("Dynamic invoke");
    }

    public override void OnStop()
    {
        Alt.Logger.LogInfo("Stopped TestResource");
    }
}

Expected behaviour

.Invoke() method works as well

Additional context

No response

Operating system

Windows 11

Version

16.2.26

Crashdump ID

No response

Confirmation of issue's presence

  • By submitting this ticket, I affirm that I have verified the presence of this issue on the latest RC (Release Candidate) version available at the time of writing this ticket.
@axeok axeok added the Class: bug Something isn't working label Nov 30, 2024
@Kaiwoknats
Copy link

We had the same problem when we tried to use a dynamically set Action and then trying to trigger it crashed the client without any logs for us. We then found a workaround and it was just using a class for it like a class named "dummyClass" with one Property of type boolean and everything worked fine again. That wrapping is sad but worked for us.

@axeok
Copy link
Author

axeok commented Dec 1, 2024

DynamicInvoke and using delegate instead of Action also works.
But... I think we need fix for default Action)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Class: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants