View Full Version : AccessViolationException on GL.CreateShader (OpenTK)

08-23-2017, 11:34 AM

I am cross posting this from StackOverflow on the advice of a colleague. Also note that I am using OpenTK, though I believe that this should not make a difference as it is a fairly straight forward wrapper for OpenGL. Please forgive me if some of my terminology is incorrect. I have not been using OpenGL for that long.

I'm having some trouble getting shaders integrated into my program. Up until this point I have been using the default pipeline but it is no longer able to accomplish all that I want to do. I have gotten some code together that I believe should work.

public int CompileShaders()

int vertexShader = GL.CreateShader(ShaderType.VertexShader);
string shader = System.IO.File.ReadAllText(AppDomain.CurrentDomain .BaseDirectory + @"Shaders\\test.vert");
GL.ShaderSource(vertexShader, shader);

int fragmentShader = GL.CreateShader(ShaderType.FragmentShader);
shader = System.IO.File.ReadAllText(AppDomain.CurrentDomain .BaseDirectory + @"Shaders\\test.frag");
GL.ShaderSource(fragmentShader, shader);

int shaderProgram = GL.CreateProgram();
GL.AttachShader(shaderProgram, vertexShader);
GL.AttachShader(shaderProgram, fragmentShader);

GL.DetachShader(shaderProgram, vertexShader);
GL.DetachShader(shaderProgram, fragmentShader);

_shaderProgram = shaderProgram;
return shaderProgram;

When this code is hit, the first call to GL.CreateShader throws an AccessViolationException. The confusing bit is most of the other articles on this issue deal with a OpenGL context that has not been initialized. However, the GLContext for my program is initialized and used (as in I have already drawn to it and have it on screen) long before this code is run. I also tested running this code in the GLControl Load event handler, where it runs fine.

To go into a bit of detail on the structure of the code, there exsists a GLScene that contains GLLayers. The layers then contain geometry in the form of GLEntities, that can be one of numerous types, GLMesh for example. The code to compile the shaders and maintain a reference to the shader program is part of GLEntity. The GLEntity class and all classes that inherit from it have draw functions that will utilize the shader via the GL.UseProgram command. So the order of events in the program is as follows. The program opens and initializes internal variables as well as the OpenGL context with a viewport. The user then opens a file, at which point geometry is generated and added to the layers in GLScene. After the completion of the geometry generation, the shaders are supposed to compile.

Do any of you have an idea as to what could be causing the issue?

Thank you for your help.

08-24-2017, 06:38 AM
I realized that my mistake was attempting to compile the shaders on a different thread from the main rendering thread.