Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: OpenTK AccessViolationException on any OpenGL call  (Read 26324 times)

0 Members and 1 Guest are viewing this topic.

Nikie

  • Newbie
  • *
  • Posts: 2
    • View Profile
OpenTK AccessViolationException on any OpenGL call
« on: May 27, 2015, 09:58:24 pm »
The simplest OpenGL calls through OpenTK are crashing with
Quote
An unhandled exception of type 'System.AccessViolationException' occurred in OpenTK.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Crashes with any combination of precompiled CSFML+.Net - 2.0, 2.1, 2.2 and with latest 2.3 git sources.
Also tried switching OpenTK from latest git version to precompiled binaries from OpenTK website - same crash.
Win7 and VS2015RC (I think it also crashed with 2013, can't say for sure).
NVidia 650TI, latest 352.86 (Witcher 3) drivers, same thing with 350.12 (GTAV) drivers. OpenGL - 4.5.
Also reported here on github by another fellow.

I also noticed that when you create a window with OpenTK the context has IsCurrent set to true, while with SFML it's false.

Minimal code, the beginning is a copy of Window example:
(click to show/hide)

Ztormi

  • Jr. Member
  • **
  • Posts: 71
  • Web developer by day. Game developer by night.
    • View Profile
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #1 on: June 01, 2015, 01:18:46 pm »
I've had problems with this aswell. The only way I've managed to use OpenTK alongside with sfml.net is not to use GraphicsContext but create OpenTK.GameWindow instead, sounds silly but it does work.
« Last Edit: June 01, 2015, 01:22:32 pm by Ztormi »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #2 on: June 01, 2015, 03:15:02 pm »
Unless you guys give stack traces / detailed crash information I can't do much (why don't you add the OpenTK source to your project and see exactly where it is crashing?). It works fine for me no matter what version of OpenTK I use. And if you want, here is my copy of OpenTK that I compiled from their develop branch a few months back.

Quote
NVidia 650TI, latest 352.86 (Witcher 3) drivers, same thing with 350.12 (GTAV) drivers. OpenGL - 4.5.

What is the output of the following code?

Console.WriteLine(window.Settings);
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Ztormi

  • Jr. Member
  • **
  • Posts: 71
  • Web developer by day. Game developer by night.
    • View Profile
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #3 on: June 02, 2015, 01:38:38 pm »
It crashes at OpenTK GraphicsContext constructor because Factory.Default.CreateGetCurrentGraphicsContext() (passed as getCurrent delegate) returns ContextHandle with handle 0.

https://github.com/opentk/opentk/blob/develop/Source/OpenTK/Graphics/GraphicsContext.cs#L207

Debugging reveals that default factory is OpenTK.Platform.SDL2.Sdl2Factory so ContextHandle is fetched from OpenTK.Platform.SDL2.Sdl2Factory.CreateGetCurrentGraphicsContext. Eventually the handle is got from Sdl2.GetCurrentContext which returns 0.


What is the output of the following code?

Console.WriteLine(window.Settings);

[ContextSettings] DepthBits(24) StencilBits(0) AntialiasingLevel(0) MajorVersion(4) MinorVersion(5)

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #4 on: June 02, 2015, 08:51:11 pm »
So you are saying the crash for you happens within the constructor of the dummy context? Because everyone else is saying it crashes on any GL function call.

GraphicsContext context = new GraphicsContext(new ContextHandle(IntPtr.Zero), null); // << Here?

It crashes at OpenTK GraphicsContext constructor because Factory.Default.CreateGetCurrentGraphicsContext() (passed as getCurrent delegate) returns ContextHandle with handle 0.

...

Eventually the handle is got from Sdl2.GetCurrentContext which returns 0.

The idea here is for OpenTK to internally think it has a valid context because SFML provides the context when it is really working with an invalid context address.

And would it be possible for you to upload your version of OpenTK so I can test with that? Also did you test to see if the crash also happens with the copy I uploaded?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Ztormi

  • Jr. Member
  • **
  • Posts: 71
  • Web developer by day. Game developer by night.
    • View Profile
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #5 on: June 02, 2015, 11:09:53 pm »
So you are saying the crash for you happens within the constructor of the dummy context? Because everyone else is saying it crashes on any GL function call.

GraphicsContext context = new GraphicsContext(new ContextHandle(IntPtr.Zero), null); // << Here?

Yeah, that's the case for me with the code OP posted. No luck with this either: http://en.sfml-dev.org/forums/index.php?topic=9693.0

(click to show/hide)

The idea here is for OpenTK to internally think it has a valid context because SFML provides the context when it is really working with an invalid context address.

Yeah, I figured that's supposed to be the case.

And would it be possible for you to upload your version of OpenTK so I can test with that? Also did you test to see if the crash also happens with the copy I uploaded?

Yes I tried yours aswell, no luck.

My build is in the attachment. Usually I just grab OpenTK from NuGet though.

To be honest I haven't troubled myself too much with this, the only use case I've got is pretty much just occasionally setting polygonmode to draw wireframe for vertex array debugging purposes.
« Last Edit: June 02, 2015, 11:21:45 pm by Ztormi »

graphnode

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #6 on: August 20, 2015, 01:24:10 am »
I'm also getting access violation on the examples that use OpenTK. Weirdly not on a couple of computers.

I tried to fix using
GraphicsMode graphicsMode = new GraphicsMode(32, (int)contextSettings.DepthBits, (int)contextSettings.StencilBits, (int)contextSettings.AntialiasingLevel);
IWindowInfo windowInfo = Utilities.CreateWindowsWindowInfo(window.SystemHandle);
GraphicsContext context = new GraphicsContext(graphicsMode, windowInfo);
context.LoadAll();

which makes it work on all computers but loses the cross-platform feature because of the
Utilities.CreateWindowsWindowInfo()
call.

No idea what I should do next besides making a code path for each OS.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #7 on: August 20, 2015, 03:28:03 am »
Well I was just playing with OpenTK again these last few days and I found different documentation with regards to creating a dummy context (didn't get around to testing it yet).

http://www.opentk.com/doc/graphics/graphicscontext

According to that we should be able to create a dummy context as follows...

GraphicsContext context = new GraphicsContext(new ContextHandle(window.SystemHandle), null);

Can you check if this fixes it for you?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

graphnode

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #8 on: August 20, 2015, 05:15:52 am »
It throws a key not found. I'm currently debugging OpenTK code and it seems it was waiting for a context pointer not a window pointer.

I'm now looking on why
GraphicsContext context = new GraphicsContext(new ContextHandle(IntPtr.Zero), null);
doesn't work. It should, it seems to find the context correctly when IntPtr.Zero is passed but crashes with access violation.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #9 on: September 29, 2015, 09:29:53 pm »
Can anyone provide more information on this? I am unable to reproduce this.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #10 on: October 07, 2015, 12:20:33 am »
I'm using SFMLNET on win7 x64, GeForce GTX460, nvidia driver 10.18.13.5390, VS2013, binaries from download section and all works perfectly. I even didn't seen Access violation for all time when I'm using it on Win7. But I'm not using GraphicsContext...

On WinXP I have stable app crash with access violation on app close.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #11 on: October 07, 2015, 12:33:14 am »
I'm using SFMLNET on win7 x64, GeForce GTX460, nvidia driver 10.18.13.5390, VS2013, binaries from download section and all works perfectly. I even didn't seen Access violation for all time when I'm using it on Win7. But I'm not using GraphicsContext...

This thread is specifically about OpenTK crashing, it has nothing to do with SFML.Net crashing.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Aravol

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #12 on: February 23, 2016, 02:09:22 am »
Also getting this error. I also tried, WITHOUT success
  • use sf::Context by directly DllImporting the sfContext_Create and sfContext_SetActive functions (copy pasted from source).
  • Ensure the window has been created, cleared, dispatched (DispatchEvents), and can be drawn to BEFORE attempting any OpenGL Calls
  • Going through a full clear/dispatch/display loop before making any OpenGL Calls
  • Tried with both 32 and 64 bit builds
  • Tried with .NET 4, 4.5, 4.5.1, 4.5.2, and 4.6

Using the OpenTK version as it ships from the Bindings page (SFML 2.2). Building via Visual Studio 2015 Community Edition, .NET framework 4-4.6
« Last Edit: February 23, 2016, 02:42:19 am by Aravol »

Alucard_2

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #13 on: October 15, 2016, 02:09:22 am »
Sorry for ressurrecting this, but I had the exactly same problem and managed to solve, so I thought it could be a good idea to let the solution here for someone who, just as me, needs it.


I was trying to reproduce this example but failing by getting AccessViolaton exception. Based on Ztormi's link I managed to make it work by the following:

using System;
using System.Runtime.InteropServices;
using SFML.Window;
using SFML.System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTKUtilities = OpenTK.Platform.Utilities;


namespace SharpGL.Examples.Window
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            var contextSettings = new ContextSettings {
                DepthBits = 24,
            };

            var window = new Window(new VideoMode(640, 480), "SFML window with OpenGL", Styles.Default, contextSettings);

            window.SetActive();

            Toolkit.Init();
           
            // Solution: creating a windowInfo and associating it with the GraphicsContext
            var windowInfo = new OpenTKUtilities.CreateWindowInfo(window.SystemHandle);
            var context = new GraphicsContext(new ContextHandle(IntPtr.Zero), windowInfo); // windowInfo instead of null
            context.MakeCurrent(windowInfo);
            context.LoadAll();

            // Everything else from now on is the same from the example (starting from Events setup)
        }
    }
}
 
« Last Edit: October 15, 2016, 02:11:40 am by Alucard_2 »

AlanGD

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: OpenTK AccessViolationException on any OpenGL call
« Reply #14 on: May 17, 2017, 03:41:44 pm »
Hi Guys, sorry the bump.

I'm having this exact problem with the latest release downloaded from the official page. The fixes mentioned here didn't work for me however. The code is exactly as in the OP and crashes on 2 of my 3 machines with the same error.

I'm only using binaries that shipped with SFML.NET.