Processing
 

OpenGL 4.5 functions segfaulting

14/09/2016 20:22#1

Doddy

Member

Joined at: 4 months ago

Post: 1

Thank: 0

Thanked: 0

I'm trying to write an MCVE OpenGL application in C on Windows 7. I'm finding that OpenGL 4.5 functions specifically, are segfaulting ("access violation") for no reason that I can determine.

My computer is an Nvidia Optimus laptop with a discrete GeForce 765M card which, according to the Nvidia spec, supports OpenGL 4.5. I have also observed this on Linux by running primusrun glxgears -info (with Bumblebee/Primus to allow running applications on the discrete card).

My (Windows) GeForce driver is version 372.70 (latest ATOW).

So, using Visual Studio 15, I have the following minimal example:

#include <stdio.h> #include <stdlib.h> #include <assert.h>  #include <GL\glew.h> #include <gl\GL.h> #include <gl\GLU.h> #include <GLFW\glfw3.h>  typedef struct {    GLenum type;    const char* fileName; } shader_t;  GLuint vao; GLuint bufferName; GLint vPosition = 0; GLuint program; GLuint vertex, frag;  shader_t shaders[] = {    {GL_VERTEX_SHADER, "vertex.glsl"},    {GL_FRAGMENT_SHADER, "frag.glsl"} };  GLuint compileShader(shader_t shader) {    GLuint ret = glCreateShader(shader.type);     FILE* file = fopen(shader.fileName, "r");     fseek(file, 0, SEEK_END);    size_t fileSize = ftell(file);    fseek(file, 0, SEEK_SET);     GLchar* src = malloc(fileSize + 1);     fread(src, 1, fileSize, file);    src[fileSize] = 0;     fclose(file);     glShaderSource(ret, 1, (const GLchar**)&src, NULL);     free(src);     glCompileShader(ret);     GLint compiled;     glGetShaderiv(ret, GL_COMPILE_STATUS, &compiled);     if (!compiled)    {       GLsizei len;        glGetShaderiv(ret, GL_INFO_LOG_LENGTH, &len);        GLchar* log = malloc(len + 1);       glGetShaderInfoLog(ret, len, &len, log);       log[len] = 0;        fprintf(stderr, "Compile failed: %s", log);       free(log);       exit(EXIT_FAILURE);    }     return ret; }  void init() {    const GLfloat verts[6][2] =    {       {-0.90, -0.90},       { 0.85, -0.90},       {-0.90,  0.85},       { 0.90, -0.85},       { 0.90,  0.90},       {-0.85,  0.90}    };     glCreateBuffers(1, &bufferName); // XXX <- segfault here!    glNamedBufferStorage(bufferName, sizeof(GLfloat) * 6 * 2, verts, 0);     program = glCreateProgram();     vertex = compileShader(shaders[0]);    frag = compileShader(shaders[1]);     glUseProgram(program);     glGenVertexArrays(1, &vao);    glBindVertexArray(vao);    glBindBuffer(GL_ARRAY_BUFFER, bufferName);    glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, (void*) 0);    glEnableVertexAttribArray(vPosition); }  void display() {    const GLfloat black[] = {0.0f, 0.0f, 0.0f, 0.0f};     glClearBufferfv(GL_COLOR, 0, black);     glBindVertexArray(vao);    glDrawArrays(GL_TRIANGLES, 0, 1); }  int main(int argc, char** argv) {    glfwInit();     GLFWwindow* window = glfwCreateWindow(640, 480, "Triangles", NULL, NULL);    assert(window);     glfwMakeContextCurrent(window);    GLenum glewErr = glewInit();     if (glewErr != GLEW_OK)    {       fprintf(stderr, "glewInit returned %d", glewErr);       return EXIT_FAILURE;    }     if (GLEW_VERSION_4_3)       printf("OpenGL 4.3 is supported!");     if (GLEW_VERSION_4_4)       printf("OpenGL 4.4 is supported!");     if (GLEW_VERSION_4_5)       printf("OpenGL 4.5 is supported!");     init();     while (!glfwWindowShouldClose(window))    {       display();       glfwSwapBuffers(window);       glfwPollEvents();    }     glfwDestroyWindow(window);    glfwTerminate();     return EXIT_SUCCESS; } 

I find that the program segfaults/"access violation" at the call to glCreateBuffers(), line 84. One thing I noticed during my investigation is that this is a new function to OpenGL 4.5. I decided to test compatibility by adding the GLEW version checks that are in main(). The only output from this application before segfaulting is:

OpenGL 4.3 is supported! 

If I replace glCreateBuffers() with glGenBuffers(), the program does not segfault at the same line; instead it dies at the next function call, to glNamedBufferStorage(). From the Khronos documentation, I notice that the two segfaulting functions are OpenGL 4.5, whereas glGenBuffers() has existed for a while.

Library details:

  • GLFW v3.2.1 (latest) binaries from website
  • GLEW comes from the Nsight tool (C:\Program Files (x86)\NVIDIA Corporation\Nsight Visual Studio Edition 5.2\Monitor\glew), recently downloaded

I am at a loss as to what I am (probably) doing wrong; please let me know if I am missing anything!

(And if there's any other information I can give to help diagnose, please leave a comment.)

Similar articles

How to use Logstash with log4j in Windows 7 32-bit VM

4 months ago - Reply: 1 - Views: 116

Running webrick server in background for Windows

4 months ago - Reply: 0 - Views: 40

Task Scheduler doesn't start my exe for the next day

4 months ago - Reply: 0 - Views: 32

Unable to get Windows Updates in Windows 7 [on hold]

4 months ago - Reply: 0 - Views: 21

Connect Spark-Shell using Eclipse in Windows 7

4 months ago - Reply: 0 - Views: 21

Can't use custom windows themes

4 months ago - Reply: 1 - Views: 108

“npm install <module>” doesn't work

4 months ago - Reply: 0 - Views: 31

OpenGL 4.5 functions segfaulting

4 months ago - Reply: 0 - Views: 6

Executing a batch file during installation

4 months ago - Reply: 0 - Views: 2

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re