kvark's picture

Shutdown logic using .NET

I'm porting my C++ GLFW-based game engine to BOO & OpenTK.

One of the major problems I encountered is implementing Shutdown logic. This logic is designed to destroy all objects that use GL context-depending data to be actually destroyed in GameWindow::Unload() method, just before the context itself is destroyed. In C++ I just had smart pointers that did all the job perfectly, except that I had to do my own reference counting. How can one implement shutdown logic in .NET properly?

Right now Garbage Collector tries to destroy my Shader objects after the GameWindow closed, causing exceptions to rise on GL.DeleteShader. The same with textures & VBO's. One of possible solutions is to perform try-except on any GL resource freeing. Is there a more proper way?


Comment viewing options

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

The trick is that you cannot delete OpenGL resources from the finalizer thread. The solution is to implement the IDisposable pattern for all resources - check out the code from a recent thread for a possible implementation.

Question: are you sure you need to deallocate resources on shutdown? If you don't plan to actually reuse the context, you could just exit without cleaning up: OpenTK will delete the context, which will in turn free all allocated resources. The OS will take care of the rest when the process is torn down.

Of course, you'll probably have to implement IDisposable anyway, to take care of resource deallcation while the game is running (e.g. on level changes), but deallocating everything on shutdown sounds like overkill.

kvark's picture

Thanks for pointing me to the close topic.

I've read some docs criticizing IDisposable (http://www.codeproject.com/KB/dotnet/idisposable.aspx) and decided not to mess with it for now.

Answer: No, I'm not sure. I know that OS will delete all context-related stuff on context closing.
BTW, the context is not destroyed properly in my app (using OpenTK-0.9.8-1 & Boo):

Destroy event fired from window: Windows.WindowInfo: Handle 3212484, Parent (null)
GameWindowExitException caught - exiting main loop.
Restoring priority.
Disposing context 131072.
Destroying window: Windows.WindowInfo: Handle 3212484, Parent (null)
Input window detached from parent 3212484.
[Warning] Failed to release device context -1442770760. Windows error: 1412.

the Fiddler's picture

Hm, can you please file a bug report about this error? Never seen it before.

Edit: the error code doesn't really make sense, it means "Class still has open windows." . We are not even trying to dispose the class at this point!

kvark's picture


Just to mention: in the log the context is disposed before window is destroyed.
This seems to be correct behavior as window actually holds the context...

the Fiddler's picture

Thanks. It seems that the render (OpenGL) context is destroyed correctly. The warning refers to a potential device context leak, which is relatively harmless nowadays (in theory you could run out of memory or DCs if you *really* tried, but this is unlikely in practice).

Still, I'll try to reproduce and fix this.