Home > Net >  glGenBuffer causes segmentation fault
glGenBuffer causes segmentation fault

Time:03-20

when using glGenBuffers with almost any other gl function the program crashes in startup

#define GL_GLEXT_PROTOTYPES
#include </usr/include/GLFW/glfw3.h>
#include <iostream>

int main()
{
    glfwInit();
    GLFWwindow *wd = glfwCreateWindow(900, 800, "main window", NULL, NULL);
    glfwMakeContextCurrent(wd);
    GLuint *buffer;
    glGenBuffers(1, buffer);
    glBindBuffer(GL_ARRAY_BUFFER, *buffer);

    while (!glfwWindowShouldClose(wd))
    {
        glfwPollEvents();
    }
    glfwTerminate();
}

CodePudding user response:

Change:

    GLuint *buffer;
    glGenBuffers(1, buffer);
    glBindBuffer(GL_ARRAY_BUFFER, *buffer);

to:

    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);

The problem is: You are giving OpenGL the value of an uninitialized variable, which it will treat as a memory location to store the buffer id into. Instead, you should declare a stack/local variable and use a pointer to that (which is a valid address location) to give OpenGL.

  • Related