NullReferenceException and KeyNotFoundException thrown in EventDispatcher.DispatchEvent

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

OlegPumpkin
Posts: 4
Joined: 28 Aug 2017, 06:47

NullReferenceException and KeyNotFoundException thrown in EventDispatcher.DispatchEvent

Postby OlegPumpkin » 19 Apr 2018, 02:28

I have a Unity game live on iOS and Android. I get reports of exceptions in EventDispatcher.DispatchEvent on iOS 10 and 11. They are somewhat uncommon, but still a significant number. The stacktraces doesn't help me much unfortunately. Would you be able to bring some light on what could be the cause?

NullReferenceException: A null value was found where an object instance was required.

Code: Select all

Rethrow as Exception: Error dispatching event objectMessage: A null value was found where an object instance was required.
Sfs2X.Core.EventDispatcher.DispatchEvent (Sfs2X.Core.BaseEvent evt)


KeyNotFoundException: The given key was not present in the dictionary.

Code: Select all

System.Collections.Generic.Dictionary`2[System.Object,System.Object].get_Item (System.Object key)
Rethrow as Exception: Error dispatching event extensionResponse: The given key was not present in the dictionary.   at System.Collections.Generic.Dictionary`2[System.Object,System.Object].get_Item (System.Object key)
Sfs2X.Core.EventDispatcher.DispatchEvent (Sfs2X.Core.BaseEvent evt)


In my implementation I do the following:
  • SmartFox.ProcessEvents is called every frame on the SmartFox instance.
  • In certain cases I close the connection by calling SmartFox.Disconnect from the CONNECTION, LOGIN_ERROR, or ROOM_JOIN_ERROR event handlers.
  • I keep the SmartFox instance around after having disconnected and continue to call SmartFox.ProcessEvents on it until I want to establish a new connection. At that time I call SmartFox.RemoveAllEventListeners and then replace the SmartFox instance with a new one.

Assuming EventDispatcher.DispatchEvent is called from inside SmartFox.ProcessEvents I suspect that I'm doing something in the handlers that I shouldn't. Is it safe to call Disconnect and RemoveAllEventListeners from event handlers?
User avatar
Bax
Site Admin
Posts: 4397
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: NullReferenceException and KeyNotFoundException thrown in EventDispatcher.DispatchEvent

Postby Bax » 19 Apr 2018, 10:06

This is what the DispatchEvent method mentioned in the stack traces does:

Code: Select all

        public void DispatchEvent(BaseEvent evt)
        {
            EventListenerDelegate dlg = listeners[evt.Type] as EventListenerDelegate;
            if (dlg != null)
            {
                evt.Target = this.target;
                try
                {
                    dlg(evt);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error dispatching event " + evt.Type + ": " + ex.Message + " " + ex.StackTrace, ex);
                }
            }
        }

From your report, the "Error dispatching event" message indicates that the catch instruction was executed, so the error occurred in the try. This means that the error originates in dlg object. This object is nothing else than one of your listeners (to either objectMessage event or extensionResponse event, as the errors indicate).

So we think that you should inspect those listeners and make sure you never try to access a null object in the objectMessage listener or to get a non-existing key on a dictionary in the extensionResponse listener. Makes sense?

By the way, what API version are you using?
Are you distributing the release version of the api? or the debug version?
If you re using the release version, maybe you should try with the debug one, just in case it shows more information.
Paolo Bax
The SmartFoxServer Team
OlegPumpkin
Posts: 4
Joined: 28 Aug 2017, 06:47

Re: NullReferenceException and KeyNotFoundException thrown in EventDispatcher.DispatchEvent

Postby OlegPumpkin » 20 Apr 2018, 09:24

Thanks for the explanation! I assumed wrongly that the original exception occurred in DispatchEvent since it was what remained of the stacktrace. Anyhow, I'm on the right track now of finding what causes the exceptions. Thanks again.

I was using the release version of API 1.7.5. But per your suggestion I have now switched it out for the debug version.

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 2 guests