luca-piccioni / OpenGL.Net

Modern OpenGL bindings for C#.
MIT License
568 stars 108 forks source link

Assert "leaving a context current" #71

Closed antoinecla closed 6 years ago

antoinecla commented 6 years ago

Step to reproduce:

Result: On start, assert: "leaving a context current". Clicking Ignore allows the application to run without other issues.

Stack trace:

OpenGL.Net.dll!OpenGL.DeviceContext.Dispose(bool disposing) Line 734    C#
OpenGL.Net.dll!OpenGL.DeviceContextEGL.Dispose(bool disposing) Line 1183    C#
OpenGL.Net.dll!OpenGL.DeviceContext.Dispose() Line 745  C#
OpenGL.Net.dll!OpenGL.Egl.Initialize() Line 149 C#
OpenGL.Net.dll!OpenGL.Egl.Egl() Line 54 C#
[Native to Managed Transition]  
[Managed to Native Transition]  
OpenGL.Net.dll!OpenGL.DeviceContext.DeviceContext() Line 43 C#
[Native to Managed Transition]  
[Managed to Native Transition]  
OpenGL.Net.WinForms.dll!OpenGL.GlControl.CreateDeviceContext(OpenGL.DevicePixelFormat controlReqFormat) Line 564    C#
OpenGL.Net.WinForms.dll!OpenGL.GlControl.CreateDeviceContext() Line 541 C#
OpenGL.Net.WinForms.dll!OpenGL.GlControl.OnHandleCreated(System.EventArgs e) Line 1106  C#
System.Windows.Forms.dll!System.Windows.Forms.Control.WmCreate(ref System.Windows.Forms.Message m)  Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m)   Unknown
System.Windows.Forms.dll!System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.WndProc(ref System.Windows.Forms.Message m)  Unknown
System.Windows.Forms.dll!System.Windows.Forms.UserControl.WndProc(ref System.Windows.Forms.Message m)   Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)   Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam)  Unknown
[Native to Managed Transition]  
[Managed to Native Transition]  
System.Windows.Forms.dll!System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(int dwExStyle, string lpszClassName, string lpszWindowName, int style, int x, int y, int width, int height, System.Runtime.InteropServices.HandleRef hWndParent, System.Runtime.InteropServices.HandleRef hMenu, System.Runtime.InteropServices.HandleRef hInst, object pvParam)   Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.CreateHandle(System.Windows.Forms.CreateParams cp)   Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateHandle()    Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateControl(bool fIgnoreVisible)    Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateControl(bool fIgnoreVisible)    Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateControl()   Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.WmShowWindow(ref System.Windows.Forms.Message m)  Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m)   Unknown
System.Windows.Forms.dll!System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Form.WmShowWindow(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Form.WndProc(ref System.Windows.Forms.Message m)  Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)   Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam)  Unknown
[Native to Managed Transition]  
[Managed to Native Transition]  
System.Windows.Forms.dll!System.Windows.Forms.Control.SetVisibleCore(bool value)    Unknown
System.Windows.Forms.dll!System.Windows.Forms.Form.SetVisibleCore(bool value)   Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.Visible.set(bool value)   Unknown
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context)    Unknown
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm)   Unknown
Test2.exe!Test2.Program.Main() Line 19  C#
luca-piccioni commented 6 years ago

Good catch!

Indeed DeviceContext.GetCurrentContext() is using WGL functions because it access to Egl.IsRequired not being initialized yet.

The assertion is somewhat pedantic, but it's always a good idea to reset current GL context before device context disposition, isn't? On my system, calling wglGetCurrentContext was returning zero, hiding the problem.