Trillian's picture

Occasional AccessViolationExceptions when creating the GraphicsContext

Project:The Open Toolkit library
Version:1.1-2014-01-02
Component:Code
Category:bug report
Priority:critical
Assigned:Unassigned
Status:closed
Description

I have a game using a GLControl. Once in a while, but quite infrequently (maybe one run on 50) I get an AccessViolationException as the GLControl attempts to initialize its context.

I have captured the stack trace of the exception on two occasions and they had the following top methods in common. As the topmost method involves pointers, might I suggest to double-check that any pointer arithmetic is correct?

Thanks.

   at OpenTK.Platform.Windows.Wgl.Delegates.CreateContextAttribsARB.Invoke(IntPtr hDC, IntPtr hShareContext, Int32* attribList)
   at OpenTK.Platform.Windows.Wgl.Arb.CreateContextAttribs(IntPtr hDC, IntPtr hShareContext, Int32[] attribList) in C:\Users\Stefanos\Desktop\opentk-all\opentk-0.9.8-3\Source\OpenTK\Platform\Windows\Bindings\Wgl.cs:line 551
   at OpenTK.Platform.Windows.WinGLContext..ctor(GraphicsMode format, IWindowInfo window, IGraphicsContext sharedContext, Int32 major, Int32 minor, GraphicsContextFlags flags) in C:\Users\Stefanos\Desktop\opentk-all\opentk-0.9.8-3\Source\OpenTK\Platform\Windows\WinGLContext.cs:line 104
   at OpenTK.Platform.Windows.WinFactory.CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, Boolean directRendering, Int32 major, Int32 minor, GraphicsContextFlags flags) in C:\Users\Stefanos\Desktop\opentk-all\opentk-0.9.8-3\Source\OpenTK\Platform\Windows\WinFactory.cs:line 31
   at OpenTK.Platform.Factory.CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, Boolean directRendering, Int32 major, Int32 minor, GraphicsContextFlags flags) in C:\Users\Stefanos\Desktop\opentk-all\opentk-0.9.8-3\Source\OpenTK\Platform\Factory.cs:line 38
   at OpenTK.Graphics.GraphicsContext..ctor(GraphicsMode mode, IWindowInfo window, Int32 major, Int32 minor, GraphicsContextFlags flags) in C:\Users\Stefanos\Desktop\opentk-all\opentk-0.9.8-3\Source\OpenTK\Graphics\GraphicsContext.cs:line 119
   at OpenTK.Platform.Windows.WinGLControl.CreateContext(Int32 major, Int32 minor, GraphicsContextFlags flags) in C:\Users\Stefanos\Desktop\opentk-all\opentk-0.9.8-3\Source\OpenTK\Platform\Windows\WinGLControl.cs:line 53
   at OpenTK.GLControl.OnHandleCreated(EventArgs e) in C:\Users\Stefanos\Desktop\opentk-all\opentk-0.9.8-3\Source\OpenTK\GLControl.cs:line 110
   at System.Windows.Forms.Control.WmCreate(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.UserControl.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

Comments

Comment viewing options

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

#1

Status:open» confirmed

What video card, OS and CPU architecture (x86 or amd64) are you using?

I can reproduce this issue semi-reliably (about 1 out of 20 runs will crash) - but only on Nvidia video cards. I have never encountered this issue on Ati, Intel or Mesa3d (software) implementations. As far as I can tell, this issue appears on both x64 and amd64 versions of Windows, but only when calling wglCreateContextAttribsARB.

Furthermore, this appears on both the GLControl and the GameWindow, which means the issue is isolated somewhere in the code shared between the two implementations (WinGLContext, WinGraphicsMode). The symptoms could indicate some form of stack corruption - but I haven't been able to locate where and how that could happen.

Once the issue appears, the process will never be able to call wglCreateContextAttribsARB successfully without stopping and restarting it.

Trillian's picture

#2

Status:confirmed» open

I have compiled a version of OpenTK and debugged my game using it. The parameters of the OpenTK.Platform.Windows.Wgl.Arb.CreateContextAttribs method are:

  • hDC = 838930312
  • hShareContext = 0
  • attribList= [8337, 1, 8338, 0, 0]

The doc says that those attribute values are the default, so I expect they'd cause no problem. However, it mentions " specifies a list of attributes for the context. The list consists of a sequence of pairs terminated by the value 0." The "terminated by the VALUE 0" might mean that you have to terminate the list with a zero pair, instead of a single zero in order to have an even number of values. I have seen code (via google code search) which does just that. Maybe you could terminate that list with another zero, just in case?

Trillian's picture

#3

Hi, the Fiddler, thanks for your quick reply.

My configuration might be important as my game has been run on numerous PCs (mostly Windows, some Linux) and a Mac, but the problem has only occured on two machines. My config is: Windows 7 on Intel x86, nVidia GeForce 8600M, the other one is also on Windows 7, and I think it has an nVidia video card too. Maybe some drivers have different behaviors in regard to the termination of that attribute list.

the Fiddler's picture

#4

Title:Occasional AccessViolationExceptions when creating the GLControl» Occasional AccessViolationExceptions when creating the GraphicsContext
Status:open» in progress (review)

The docs read,

Quote:

attribList specifies a list of attributes for the context. The list consists of a sequence of <name,value> pairs terminated by the value 0.

My understanding is that this is a single 0 (hence the distinction between "value" and "pair"), but it doesn't hurt to be defensive, I guess.

I have added a second 0 and haven't observed a crash after ~20 launches (but this doesn't mean anything by itself, the issue is infrequent enough that I may have simply gotten lucky). Change committed to SVN rev. 2475.

Trillian's picture

#5

A single zero would have been logical, but because because the spec is not super precise about that and gives no examples, implementors might have understood that two zeroes are expected. As you say, it doesn't hurt to be defensive.

Thanks for taking action. I'll try to migrate my project to that revision and see if I get those again. It was pretty rare, so I'll wait a few weeks, and if it doesn't occur again, I'll post back and assume that fixes it.

cjanssen's picture

#6

I just wanted to follow up on this issue as I am having this problem in the 1.0beta2. I'm running Windows 7 x64, with a Quadro FX 3700 (driver version 191.66). For me I get an AccessViolationException every single time I run following code:

GraphicsMode graphicsMode = new GraphicsMode(new ColorFormat(24), 24, 8, 0, new ColorFormat(0), 2, stereo);
IWindowInfo windowInfo = Utilities.CreateWindowsWindowInfo(windowHandle);
GraphicsContext graphicsContext = new OpenTK.Graphics.GraphicsContext(graphicsMode, windowInfo, 3, 0, GraphicsContextFlags.Default);

stereo is false and windowHandle is the Form's Handle. Here is the output when I'm running the app:

Creating GraphicsContext.
DisplayDevice 1 (primary) supports 519 resolutions.
DisplayDevice 2 (secondary) supports 519 resolutions.
Creating default GraphicsMode (32, 16, 0, 0, 0, 2, False).
Creating GraphicsContext.
Device context: 889265060
Selecting pixel format... GraphicsMode: Index: 11, Color: 24 (8880), Depth: 0, Stencil: 0, Samples: 0, Accum: 64 (16161616), Buffers: 2, Stereo: False
IWindowInfo: Windows.WindowInfo: Handle 1443302, Parent (Windows.WindowInfo: Handle 2689402, Parent (null))
GraphicsContextFlags: Default
Requested version: 1.0
Loaded opengl32.dll: 1582170112
OpenGL will be bound to handle: 1443302
Setting pixel format... 11
Creating temporary context for wgl extensions.
Load extensions for OpenTK.Platform.Windows.Wgl... 50 extensions loaded in 10 ms.
Load extensions for OpenTK.Platform.Windows.Wgl... 50 extensions loaded in 1 ms.
Using WGL_ARB_create_context... A first chance exception of type 'System.AccessViolationException' occurred in OpenTK.dll

Also, I noticed that no matter what version I request the Request version that is listed is 1.0. I enabled the AccessViolationException break and checked the stack trace and the error seems to be the result of Debug.Print("GraphicsMode: {0}", mode);. I included the relevant portion of the stack trace below.

 	OpenTK.dll!OpenTK.Platform.Windows.Wgl.Arb.CreateContextAttribs(System.IntPtr hDC = 1912674959, System.IntPtr hShareContext = 0, int[] attribList = {int[6]}) Line 551 + 0x29 bytes	C#
 	OpenTK.dll!OpenTK.Platform.Windows.WinGLContext.WinGLContext(OpenTK.Graphics.GraphicsMode format = {Index: 11, Color: 24 (8880), Depth: 0, Stencil: 0, Samples: 0, Accum: 64 (16161616), Buffers: 2, Stereo: False}, OpenTK.Platform.Windows.WinWindowInfo window = {Windows.WindowInfo: Handle 3279152, Parent (Windows.WindowInfo: Handle 2884744, Parent (null))}, OpenTK.Graphics.IGraphicsContext sharedContext = null, int major = 1, int minor = 0, OpenTK.Graphics.GraphicsContextFlags flags = Default) Line 112 + 0xca bytes	C#
 	OpenTK.dll!OpenTK.Platform.Windows.WinFactory.CreateGLContext(OpenTK.Graphics.GraphicsMode mode = {Index: 11, Color: 24 (8880), Depth: 0, Stencil: 0, Samples: 0, Accum: 64 (16161616), Buffers: 2, Stereo: False}, OpenTK.Platform.IWindowInfo window = {Windows.WindowInfo: Handle 3279152, Parent (Windows.WindowInfo: Handle 2884744, Parent (null))}, OpenTK.Graphics.IGraphicsContext shareContext = null, bool directRendering = true, int major = 1, int minor = 0, OpenTK.Graphics.GraphicsContextFlags flags = Default) Line 53 + 0x4d bytes	C#
 	OpenTK.dll!OpenTK.Graphics.GraphicsContext.GraphicsContext(OpenTK.Graphics.GraphicsMode mode = {Index: 11, Color: 24 (8880), Depth: 0, Stencil: 0, Samples: 0, Accum: 64 (16161616), Buffers: 2, Stereo: False}, OpenTK.Platform.IWindowInfo window = {Windows.WindowInfo: Handle 3279152, Parent (Windows.WindowInfo: Handle 2884744, Parent (null))}, int major = 1, int minor = 0, OpenTK.Graphics.GraphicsContextFlags flags = Default) Line 112 + 0x3a bytes	C#
 	OpenTK.dll!OpenTK.Platform.Windows.WinGraphicsMode.SelectGraphicsModeARB(OpenTK.Graphics.ColorFormat color = {24 (8880)}, int depth = 24, int stencil = 8, int samples = 0, OpenTK.Graphics.ColorFormat accum = {0 (0000)}, int buffers = 2, bool stereo = false) Line 158 + 0x96 bytes	C#
 	OpenTK.dll!OpenTK.Platform.Windows.WinGraphicsMode.SelectGraphicsMode(OpenTK.Graphics.ColorFormat color = {24 (8880)}, int depth = 24, int stencil = 8, int samples = 0, OpenTK.Graphics.ColorFormat accum = {0 (0000)}, int buffers = 2, bool stereo = false) Line 70 + 0x2f bytes	C#
 	OpenTK.dll!OpenTK.Graphics.GraphicsMode.Index.get() Line 186 + 0x89 bytes	C#
 	OpenTK.dll!OpenTK.Graphics.GraphicsMode.ToString() Line 329 + 0x37 bytes	C#
 	mscorlib.dll!System.Text.StringBuilder.AppendFormat(System.IFormatProvider provider = {}, string format, object[] args = {object[1]}) + 0x1ec bytes	
 	mscorlib.dll!string.Format(System.IFormatProvider provider, string format, object[] args) + 0x6c bytes	
 	System.dll!System.Diagnostics.Debug.Print(string format, object[] args) + 0x35 bytes	
>	OpenTK.dll!OpenTK.Graphics.GraphicsContext.GraphicsContext(OpenTK.Graphics.GraphicsMode mode = {Index: 9, Color: 24 (8880), Depth: 24, Stencil: 8, Samples: 0, Accum: 64 (16161616), Buffers: 2, Stereo: False}, OpenTK.Platform.IWindowInfo window = {Windows.WindowInfo: Handle 1313228, Parent (null)}, int major = 3, int minor = 0, OpenTK.Graphics.GraphicsContextFlags flags = Default) Line 99 + 0x39 bytes	C#

Any help would be great, thanks so much.

the Fiddler's picture

#7

Quote:

Also, I noticed that no matter what version I request the Request version that is listed is 1.0.

This is correct, we need a temporary 1.0 context to retrieve the extensions necessary for a 3.x context.

I haven't been able to reproduce this on my Win7 x64 system (Quadro NVS 135M, 195.62 drivers). I notice there are newer drivers for your card (191.77), so upgrading might be worth a try.

cjanssen's picture

#8

Ah yes, I was just stepping through the code and realized that it was generating a temporary context.

I upgraded the drivers (191.78), unfortunately still no luck

the Fiddler's picture

#9

Does the crash still occur if you request a legacy (1.0) context?

cjanssen's picture

#10

Actually it looks like this error might be caused by an interaction between Tao and OpenTK. I actually have other windows that contain Tao.OpenGL based implementations (I'm mid-migration) and if they are opened before OpenTK has had a chance to initialize it seems to fail. If the first GL based control created is OpenTK all subsequent problems go away. It seems sort of weird since none of the controls use both APIs simultaneously (IE control A is entirely OpenTK, control B is entirely Tao.OpenGL).