Goz3rr's picture

Game doesn't seem to close properly

When trying this code:

GameWindow window = new GameWindow(1280, 720, GraphicsMode.Default, "OpenTK Test", GameWindowFlags.Default, DisplayDevice.Default);
window.Run();

I get this console output when closing

Sdl2 application quit
The thread 0x211c has exited with code 259 (0x103).
The thread 0xaec has exited with code 259 (0x103).
Sdl2GraphicsContext (handle: 196608) leaked, did you forget to call Dispose()?
GraphicsContext leaked, did you forget to call Dispose()?
Disposing OpenTK.Platform.SDL2.Sdl2NativeWindow
Disposing OpenTK.Platform.SDL2.Sdl2InputDriver
Disposing OpenTK.Platform.SDL2.Sdl2JoystickDriver
[OpenTK] OpenTK.Platform.SDL2.Sdl2Factory leaked, did you forget to call Dispose()?
OpenTK.Toolkit leaked, did you forget to call Dispose()?
OpenTK.Platform.Factory leaked, did you forget to call Dispose()?
The program '[36] TestGame.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[36] TestGame.vshost.exe' has exited with code 0 (0x0).

Is there anything i'm doing wrong that causes these messages about not calling Dispose?

When adding the following:

window.RenderFrame += delegate(object sender, FrameEventArgs eventArgs) {
    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
    window.SwapBuffers();
};

The game now takes longer to close and:

The program '[4180] TestGame.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[4180] TestGame.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

The correct way to clean up is the following:

using (Toolkit.Init())
using (var window = new GameWindow(...))
{
    window.Run();
}

The reason is that GameWindow is holding OS resources (i.e. a window handle, an OpenGL context) that must be cleaned up by the thread that created them. The GC is running on a different thread, which cannot delete those resources, so OpenTK is informing you of a potential memory leak.

In practice, this does not matter if your application quits once the window is closed. However, if you are implementing a plugin or an application that does not quit after its window is closed, then you should explicitly dispose all OS resources.

Goz3rr's picture

I switched from

using(T game = new T()) { // where T : KuubGameWindow, new()
    game.Run();
}

to

using(Toolkit.Init()) {
    using(T game = new T()) {
        game.Run();
     }
}

And while the warnings about disposing stopped, my fps is now all over the place (sometimes jumping up to 500) and the access violating when quitting still happens

the Fiddler's picture

gw.Run() instructs the GameWindow to raise update/render events as fast as possible. This means the fps variability you see is normal and expected.

Use gw.Run(60) to limit your UpdateFrame rate to 60 updates / sec. Use gw.VSync = VSyncMode.Adaptive to limit your RenderFrame rate to your monitor's capabilities.

There is not enough information to trace the AccessViolationException. Can you capture a complete stack trace? Alternatively, try using the latest version of SDL from http://libsdl.org, or remove SDL and try with the native backend.

Goz3rr's picture

Sorry i should've mentioned Window.VSync = VSyncMode.On;
For some reason it stopped now though, without changing anything the fps is now almost a solid 60 the entire time, with an occasional hiccup to ~45 for a frame

If i remove SDL2.dll from the output the Access Violation will stop, but the game will also stop working on a friends pc. Using the latest x64 SDL2 still causes the access violation, latest x86 (My OS is x64 Windows 8.1) will cause the game to crash from a NullReferenceException in OpenTK.dll, Sdl2NativeWindow.cs line 501

Goz3rr's picture

It seems the latest version of OpenTK (from github) combined with SDL2 2.0.3 (32 bit) works fine now, although vshost32.exe will crash when closing the game:

SDL2 is supported
Detected configuration: Windows / .Net
[SDL2] Joystick device 0 opened successfully. 
		'PS(R) Gamepad' has 4 axes, 12 buttons, 1 hats, 0 balls
GraphicsMode.Default = Index: , Color: 32 (8888), Depth: 16, Stencil: 0, Samples: 0, Accum: 0 (0000), Buffers: 2, Stereo: False
Creating GraphicsContext.
    GraphicsMode: Index: , Color: 32 (8888), Depth: 16, Stencil: 0, Samples: 0, Accum: 0 (0000), Buffers: 2, Stereo: False
    IWindowInfo: OpenTK.Platform.SDL2.Sdl2WindowInfo
    GraphicsContextFlags: Default
    Requested version: 1.0
    SDL2 created GraphicsContext (handle: 196608)
        GraphicsMode: Index: , Color: 24 (8880), Depth: 24, Stencil: 0, Samples: 0, Accum: 64 (16161616), Buffers: 2, Stereo: False
        GraphicsContextFlags: Default
Bindings loaded in 9,2211 ms.
Entering main loop.
Sdl2 application quit
Disposing context 196608.
Disposing OpenTK.Platform.SDL2.Sdl2GraphicsContext (handle: 196608)
Disposing OpenTK.Platform.SDL2.Sdl2NativeWindow
Disposing OpenTK.Platform.SDL2.Sdl2InputDriver
Disposing OpenTK.Platform.SDL2.Sdl2JoystickDriver
The thread 0x2a0c has exited with code 259 (0x103).
The thread 0x2a30 has exited with code 259 (0x103).
The program '[2764] OpenTKTest.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[2764] OpenTKTest.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.

For some reason when starting the process manually and then attaching the debugger nothing happens, it only happens when running through visual studio:

 	ntdll.dll!_DbgBreakPoint@0()	Unknown
 	ntdll.dll!_DbgUiRemoteBreakin@4()	Unknown
 	kernel32.dll!74e4919f()	Unknown
 	[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]	
 	[External Code]

When turning on native code debugging it also stops, so i have no clue what is going on here. I guess i'll deal with the slightly longer loading times while debugging