Monogame keeps crashing randomly!

An unhandled exception of type 'OpenTK.Graphics.GraphicsContextException' occurred in OpenTK.dll

Is all I’m getting once the game crashes, as can be seen here:

It started happening few weeks ago, but I didn’t mind it because it happened once in a blue moon. But now it’s happening every few seconds, and its really frustrating. I don’t know what is causing it and I cant find any information regarding it.

At first I thought simple re-install of monogame will fix it, but yesterday I formatted my pc, installed windows 10, new visual studio and fresh new Monogame, and its still happening! Anyone knows what this is and what can I do?

Unfortunately, I don’t know what might be causing that, but have you tried switching from an OpenGL project to a DirectX project or vice versa?

Got the stack trace?
Sounds like you are accessing OpenGL from somewhere other than the main thread possibly.

@AJP I will try that today @daveleaver I can post here all the code, its not too much, but I’ll try to switch to directx beforehand.

crashing seemed to stop, but new problems appeared.

The crashes occur because window gets destroyed before resources get disposed of, fix for it is: https://github.com/mono/MonoGame/pull/4046 , mono and .net GC works different and I have done a lazy fix with the last commit(just checked if the window handle is valid). This means that if you are using a single window you won’t notice anything, but if you are using multiple windows you have to dispose of all the resources before calling the new window.

@cra0zy I don’t understand what do you mean, I didn’t destroy any window, I didn’t create any new windows, I was just simply playing on 1 window game and it crashes in the middle

I was talking about PR(the fix), and what causes the bug. You didn’t do anything wrong.

@cra0zy I see, so I need to apply manually everything you rewrote and removed in the fixup for it to work? honestly I’m not sure I want to use openGL, when switching to directx it seemed like the game performance got a lot better than before, but if I wont be able to fix the bug I guess I will try your fix.

@cra0zy uhm, I downloaded your monogame solution but I’m abit confused of how do I replace it with my current monogame, these are the source files, how do I use them?

Just use: http://teamcity.monogame.net/repository/download/MonoGame_DevelopWin/10089:id/Windows/MonoGameSetup.exe?guest=1