user18190011
user18190011

Reputation:

glGenBuffer causes segmentation fault

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();
}

Upvotes: 2

Views: 291

Answers (1)

httpdigest
httpdigest

Reputation: 5806

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.

Upvotes: 1

Related Questions