SFML community forums

Bindings - other languages => DotNet => Topic started by: Nikie on May 27, 2015, 09:58:24 pm

Title: OpenTK AccessViolationException on any OpenGL call
Post by: Nikie 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 (https://github.com/SFML/SFML.Net/issues/114) 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)
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: Ztormi 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.
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: zsbzsb 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 (http://hashcookie.net/uploads/0a15c7d1c8_OpenTK.dll) 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);
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: Ztormi 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)
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: zsbzsb 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?
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: Ztormi 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.
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: graphnode 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.
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: zsbzsb 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?
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: graphnode 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.
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: zsbzsb on September 29, 2015, 09:29:53 pm
Can anyone provide more information on this? I am unable to reproduce this.
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: mkalex777 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.
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: zsbzsb 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.
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: Aravol on February 23, 2016, 02:09:22 am
Also getting this error. I also tried, WITHOUT success

Using the OpenTK version as it ships from the Bindings (http://www.sfml-dev.org/download/sfml.net/) page (SFML 2.2). Building via Visual Studio 2015 Community Edition, .NET framework 4-4.6
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: Alucard_2 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 (https://github.com/SFML/SFML.Net/blob/master/examples/window/Window.cs) 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)
        }
    }
}
 
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: AlanGD 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.
Title: Re: OpenTK AccessViolationException on any OpenGL call
Post by: dabbertorres on May 18, 2017, 10:48:15 am
If you don't mind building the master branch (https://github.com/SFML/SFML.NET), does it still occur?
(CSFML 2.4 can be downloaded from the website)