karlschiffmann's picture

Crash on creation on Windows 7 laptop

Hello - we are getting a crash on the default CreateContext() that only seems to happen (so far) on a Windows 7 laptop. This is based upon your example on using the glControl in a Windows Form. Below is the code implementing the Form. Please advise me on what I can do to remedy this. Thank you!

Crash report:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at OpenTK.Platform.Windows.Wgl.Imports.CreateContext(IntPtr hDc)
at OpenTK.Platform.Windows.WinGLContext..ctor(GraphicsMode format, WinWindowInfo window, IGraphicsContext sharedContext, Int32 major, Int32 minor, GraphicsContextFlags flags)
at OpenTK.Platform.Windows.WinFactory.CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, Boolean directRendering, Int32 major, Int32 minor, GraphicsContextFlags flags)
at OpenTK.Graphics.GraphicsContext..ctor(GraphicsMode mode, IWindowInfo window, Int32 major, Int32 minor, GraphicsContextFlags flags)
at OpenTK.Platform.Windows.WinGraphicsMode.SelectGraphicsModeARB(ColorFormat color, Int32 depth, Int32 stencil, Int32 samples, ColorFormat accum, Int32 buffers, Boolean stereo)
at OpenTK.Platform.Windows.WinGraphicsMode.SelectGraphicsMode(ColorFormat color, Int32 depth, Int32 stencil, Int32 samples, ColorFormat accum, Int32 buffers, Boolean stereo)
at OpenTK.Graphics.GraphicsMode.get_Index()
at OpenTK.Platform.Windows.WinGLContext.SetGraphicsModePFD(GraphicsMode mode, WinWindowInfo window)
at OpenTK.Platform.Windows.WinGLContext..ctor(GraphicsMode format, WinWindowInfo window, IGraphicsContext sharedContext, Int32 major, Int32 minor, GraphicsContextFlags flags)
at OpenTK.Platform.Windows.WinFactory.CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, Boolean directRendering, Int32 major, Int32 minor, GraphicsContextFlags flags)
at OpenTK.Graphics.GraphicsContext..ctor(GraphicsMode mode, IWindowInfo window, Int32 major, Int32 minor, GraphicsContextFlags flags)
at OpenTK.WinGLControl.CreateContext(Int32 major, Int32 minor, GraphicsContextFlags flags)
at OpenTK.GLControl.OnHandleCreated(EventArgs e)
at System.Windows.Forms.Control.WmCreate(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v2.0.50727/mscorlib.dll
----------------------------------------
TestOpenTK_Triangle
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Junk/TestOpenTK_Triangle.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5...
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/Sy...
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d...
----------------------------------------
OpenTK.GLControl
Assembly Version: 0.9.9.3
Win32 Version: 0.9.9.3
CodeBase: file:///C:/Junk/OpenTK.GLControl.DLL
----------------------------------------
OpenTK
Assembly Version: 0.9.9.3
Win32 Version: 0.9.9.3
CodeBase: file:///C:/Junk/OpenTK.DLL
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

FormMain.cs:

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Threading;					//	for "lock"
 
namespace TestOpenTK_Triangle
{
	public partial class FormMain : Form
	{
	    bool loaded = false;
 
	    public FormMain()
		{
			InitializeComponent();
		}
		private void glControl1_Load(object sender, EventArgs e)
		{
			loaded = true;
			SetupViewport();
			Application.Idle += new EventHandler(Application_Idle); // press TAB twice after +=
	    }
 
		void Application_Idle(object sender, EventArgs e)
		{
			idleCallback();
		}
		private void idleCallback()
		{
			glControl1.Invalidate();
			Thread.Sleep(20);       // avoid 100% cpu usage
		}
 
		private void SetupViewport()
		{
			int w = this.ClientSize.Width;
			int h = this.ClientSize.Height;
			glControl1.Width = w;
			glControl1.Height = h;
			GL.MatrixMode(MatrixMode.Projection);
			GL.LoadIdentity();
			GL.Viewport(0, 0, w, h); // Use all of the glControl painting area
			GL.Ortho(-1, 1, -1, 1, -1, 1);
		}
 
		private void glControl1_Paint(object sender, PaintEventArgs e)
		{
			if (!loaded) // Play nice
			{
			//                        return;
				glControl1_Load(null, null);
			}
			displayCallback();
		}
		private void displayCallback()
		{
			GL.ClearColor(0.0F, 0.0F, 0.0F, 1.0F);
			GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
			GL.Begin(BeginMode.Triangles);
			GL.Color3(0.0, 0.0, 1.0);
			GL.Vertex2(+0.000, +1.000);
			GL.Color3(1.0, 0.0, 0.0);
			GL.Vertex2(-0.866, -0.500);
			GL.Color3(0.0, 1.0, 0.0);
			GL.Vertex2(+0.866, -0.500);
			GL.End();
			GL.Rotate(2.0, 0.0, 0.0, 1.0);
			glControl1.SwapBuffers();
		}
 
		private void glControl1_Resize(object sender, EventArgs e)
		{
			SetupViewport();
			glControl1.Invalidate();
		}
 
		private void FormMain_Resize(object sender, EventArgs e)
		{
			glControl1_Resize(sender, e);
		}
	}
}

FormMain.Designer.cs:

namespace TestOpenTK_Triangle
{
	partial class FormMain
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
 
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
 
		#region Windows Form Designer generated code
 
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.glControl1 = new OpenTK.GLControl();
			this.SuspendLayout();
			// 
			// glControl1
			// 
			this.glControl1.AutoSize = true;
			this.glControl1.BackColor = System.Drawing.Color.Black;
			this.glControl1.Location = new System.Drawing.Point(-2, -2);
			this.glControl1.Name = "glControl1";
			this.glControl1.Size = new System.Drawing.Size(295, 279);
			this.glControl1.TabIndex = 0;
			this.glControl1.VSync = true;
			this.glControl1.Load += new System.EventHandler(this.glControl1_Load);
			this.glControl1.Paint += new System.Windows.Forms.PaintEventHandler(this.glControl1_Paint);
			this.glControl1.Resize += new System.EventHandler(this.glControl1_Resize);
			// 
			// FormMain
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Controls.Add(this.glControl1);
			this.Name = "FormMain";
			this.Text = "OpenTK variations";
			this.Resize += new System.EventHandler(this.FormMain_Resize);
			this.ResumeLayout(false);
			this.PerformLayout();
 
		}
 
		#endregion
 
		private OpenTK.GLControl glControl1;
	}
}

Comments

Comment viewing options

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

Hello!

We have been using OpenTK to display real-time sonar data in a Windows Form. The rendering is triggered by a 60-hz timer and the drawing is done in the main UI thread. Data rendered arrives over the Ethernet in other threads, which set flags that enable rendering from the timer event handler. Everything works fine under all versions of Windows and graphics cards we use, EXCEPT for ATI boards. We constructed a simple example that may demonstrate the problem. The number of "system calls" (as seen from the Windows Performance monitor) rises dramatically with OpenTK and the ATI boards. A similar example program written in C and Win32 and calling directly to the Windows OpenGL.dll does not exhibit these problems. Some members of our team would like to switch away from OpenTK, but I would like to avoid that. Do you have any access to ATI graphics boards so that perhaps you can investigate this possible issue?

Please reference the C# code submitted in the post "Crash on creation on Windows 7 laptop".

Thank you!

the Fiddler's picture

Please make sure you are using the latest version of OpenTK (1.0-rc1) and up to date ATI drivers (versions 10.3 and 10.4 were reported to crash under some circumstances, while 10.5 fixed that issue).

If you can still reproduce the crash with the latest driver and OpenTK versions, please say so and I will try to reproduce.