Mesen netplay fix (wip)

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
Posts: 2
Joined: Sat Dec 19, 2020 6:08 am

Mesen netplay fix (wip)

Post by rixion » Sat Dec 19, 2020 7:17 am

Hi guys,

i'm trying to fix an issue with mesen's netplay. After a while of playing online with someone the game will bring up an error and disconnect without crashing. Here is a link of the issue i opened on github.


I looked into the source files and i found a file called frmMain.cs. Here at the very top there is a class frmMain with multiple methods out of which one called StartEmuThread().
Here is a chain with the definition and the relevant references for this method:

StartEmuThread() definition
StartEmuThread() reference
Run() import from external dll
Run() export
Run() definition

It seems that the crash happens when trying to run InteropEmu.Run() which is called by StartEmuThread(). I tried removing line 692 from frmMain.cs but that just makes the emulator not start anymore, it just shows up the ui with a black screen.

I placed a breakpoint on StartEmuThread() and InteropEmu.Run() in visual studio to see if they get accessed only once when crashing or multiple times. Apparently StartEmuThread() gets accessed every time the emulator pauses/unpauses using Esc key and every time you load a save state. The if statement however inside StartEmuThread() doesn't get triggered.

I also looked at the method that references StartEmuThread() which is called _notifListener_OnNotification(). Inside here there is a switch with a case for GameInitCompleted and here is the StartEmuThread() method.
GameInitCompleted is defined as an enum in a file called INotificationListener.h and is referenced in multiple files but i think the most relevant is GameServerConnection.cpp and also in InteropEmu.cs.

Code: Select all

private void StartEmuThread()
	if(_emuThread == null) {
		_emuThread = new Thread(() => {
			try {
				_emuThread = null;
			} catch(Exception ex) {
				MesenMsgBox.Show("UnexpectedError", MessageBoxButtons.OK, MessageBoxIcon.Error, ex.ToString());
				_emuThread = null;
My assumption is that there is something going on with that _emuThread structure because even though the StartEmuThread() breakpoint gets triggered all the time, the if statement never gets triggered, but i'm not sure.

If anyone has any ideas i would love to hear them out.
Also does anyone know what the notation Mesen.GUI.Forms.frmMain.<StartEmuThread>b__215_0() mean?
I know that those are namespaces and frmMain.StartEmuThread() is a method of frmMain but why is it written like that? And what is the b__215_0 number? I searched for it in the source files but it's not written anywhere.


User avatar
Posts: 4411
Joined: Fri Nov 19, 2004 7:35 pm

Re: Mesen netplay fix (wip)

Post by Dwedit » Sat Dec 19, 2020 7:23 pm

SEH Exception: It means that native code is triggering a processor's exception (access violation, division by 0, etc...)

Mesen is designed to be mostly Native Code, then a shell around it written in C# for the user interface. So getting an SEH exception means that there's an exception in the native part, the part that is not C#.

So if you can trigger the exception on command, try running it under a Native Code debugger, such as X64dbg, and that would pick up the exception before the C# program can swallow it up. Only thing is that X64dbg will show x64 assembly code instead of C++ code.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

Posts: 2
Joined: Sat Dec 19, 2020 6:08 am

Re: Mesen netplay fix (wip)

Post by rixion » Sun Dec 20, 2020 11:49 am

I've been trying to replicate the bug but i can't seem to figure out what causes it. I know that InteropEmu.Run() does get called sometimes and it runs successfully, usually happens when someone creates a server for the first time or if i pause the emulator using Esc key but it doesn't trigger an exception no matter how many times i press Esc or start the server. Also when it does happen it happens randomly usually during gameplay in neither of those 2 cases above.
Also i'm using visual studio and for some reason i can't single step instructions when debugging, if i press step into it just resumes the program.

Post Reply