a6482323's picture

AccessViolationException when calling SwapBuffers() under certain conditions

I'm getting an error when I call SwapBuffers() under certain conditions:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I created the following class to test it. It seems to occur when the game window is minimized. It can also be fixed by using the parameterless GameWindow.Run() method, or by calling GL.Finish() at the end of the RenderFrame event handler but before calling SwapBuffers(). Here's some code:

    public class Game
    {
        GameWindow window;
 
        public Game(String[] args)
        {
            window = new GameWindow(640, 480, OpenTK.Graphics.GraphicsMode.Default, "Engine Window", GameWindowFlags.FixedWindow, DisplayDevice.Default);
        }
 
        public void Run()
        {
            window.UpdateFrame += new EventHandler<FrameEventArgs>(window_UpdateFrame);
            window.RenderFrame += new EventHandler<FrameEventArgs>(window_RenderFrame);
            window.Run(120.0, 0.0);
            //if I use window.Run(); instead of window.Run(120.0, 0.0); it works fine
        }
 
        void window_RenderFrame(object sender, FrameEventArgs e)
        {
            OpenTK.Graphics.OpenGL4.GL.ClearColor(Color4.CornflowerBlue);
            OpenTK.Graphics.OpenGL4.GL.Clear(OpenTK.Graphics.OpenGL4.ClearBufferMask.DepthBufferBit | OpenTK.Graphics.OpenGL4.ClearBufferMask.ColorBufferBit);
            //if I call OpenTK.Graphics.OpenGL4.GL.Finish(); here it works fine
            window.SwapBuffers();
        }
 
        void window_UpdateFrame(object sender, FrameEventArgs e)
        {
            Console.WriteLine("updating");
        }
    }

My questions are: why does this error happen; why do those solutions work and which solution is better?


Comments

Comment viewing options

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

What operating system, what gpu and driver version, what OpenTK version, debug or release build?

First look at it, this code doesn't cause any issues on my system.

a6482323's picture

OS: Windows 7 x64
GPU: GeForce GTX 460 1gb
Driver version: 337.88
OpenTK version: 1.1 release

it seems intermittent for me. sometimes it happens within seconds, while other times I have to leave it for a minute or so.

Frassle's picture

Hmm still can't repo. Can you create a class that inherits GameWindow and overrides the OnRenderFrame method (see Examples/OpenTK/GameWindow/GameWindowSimple.cs for an example). If possible try with a debug build of OpenTK as well, a full exception message and stack trace would be useful here.