NullReferenceException on internal event triggers

Description of bug: Triggering events which are handled by the same resource seem to throw the NRE listed below:

[     54297] System.NullReferenceException: Object reference not set to an instance of an object
[     54297]   at CitizenFX.Core.ScriptContext.CopyReferencedParametersOut (CitizenFX.Core.RageScriptContext* cxt) [0x00015] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\ScriptContext.cs:359 
[     54297]   at CitizenFX.Core.ScriptContext.InvokeInternal (CitizenFX.Core.RageScriptContext* cxt, System.UInt64 nativeIdentifier, CitizenFX.Core.IScriptHost scriptHost) [0x00059] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\ScriptContext.cs:344 
[     54297]   at CitizenFX.Core.ScriptContext.InvokeInternal (System.UInt64 nativeIdentifier, CitizenFX.Core.IScriptHost scriptHost) [0x00009] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\ScriptContext.cs:319 
[     54297]   at CitizenFX.Core.ScriptContext.Invoke (System.UInt64 nativeIdentifier, CitizenFX.Core.IScriptHost scriptHost) [0x00000] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\ScriptContext.cs:312 
[     54297]   at CitizenFX.Core.Native.Function.InvokeInternal (CitizenFX.Core.Native.Hash nativeHash, System.Type returnType, CitizenFX.Core.Native.InputArgument[] args) [0x00024] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\Native.cs:28 
[     54297]   at CitizenFX.Core.Native.Function.Call (CitizenFX.Core.Native.Hash hash, CitizenFX.Core.Native.InputArgument[] arguments) [0x00000] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\Native.cs:16 
[     54297]   at CitizenFX.Core.BaseScript.TriggerEventInternal (System.String eventName, System.Byte[] argsSerialized, System.Boolean isRemote) [0x00034] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\BaseScript.cs:175 
[     54297]   at CitizenFX.Core.BaseScript.TriggerEvent (System.String eventName, System.Object[] args) [0x00007] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\BaseScript.cs:116 

It appears that the event handler still gets triggered normally, however the code which originally triggered the event throws the exception.

This bug is also reproducible by utilizing Exports within the same resource.

I’m unsure if this only impacts C# resources or all resources, however this was only tested with C#. This only seems to impact C#.

Reproduction Steps: In order to reproduce, you need to trigger an event within the same resource which will throw this exception on event trigger. The code to reproduce this bug is below:

public BasicScript() {
    // Register event handler for event "Test.Internal"
    EventHandlers["Test.Internal"] += new Action( () => {
        Debug.WriteLine( "Received Test.Internal event" ); // This gets executed as expected
    } );

    // Execute Test.Internal event, which will cause the NRE above to be thrown.
    Debug.WriteLine( "Before TriggerEvent call" );
    TriggerEvent("Test.Internal"); // Throws NRE
    Debug.WriteLine( "After TriggerEvent call" );
}

Here is a demonstration using DevTools:

2 Likes

I’m pretty sure it’s just C#. Made a small Lua resource:

AddEventHandler('banana', function()
    print('Event result')
end)


print('Before Event Call')
TriggerEvent('banana')
print('After event call')

With result, as expected:
NaughtyVulpesveloxGranularOperateHen

A potential fix for this has been pushed to the Git repository and should hit canary within the next few minutes. Does this fix the issue?

1 Like

I can confirm that it fixes the issue. It will no longer throw the exception.