Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

OpenGL modifies variable without being explicitly told to

Hi, I’m trying to write a program with OpenGL using c++, glad and glfw, but I’ve encountered a problem I cannot wrap my head around.

main.cpp

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>

#define SCR_WIDTH 1280
#define SCR_Height 720

int main() {
    GLFWwindow* window = init();

    unsigned int VAO, VBO, EBO;

    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(N_EBOS, &EBO);

    //load vertices etc..
    
    while (!glfwWindowShouldClose(window)) {
        glfwSwapBuffers(window);

        // Render stuff...

        glfwPollEvents();
    }

    glfwTerminate();

    return 0;
}

Basically if I remove glGenBuffers(N_EBOS, &EBO); it all works fine, but when I add that line, and N_EBOS is 15 or more, the program segfaults at the glfwWindowShouldClose(window) line. After using a debugger I found that after the glGenbuffers(N_EBOS, &EBO); is run, the window variable changes value, and I can’t figure out how or why it does that. I also made a gif to show the problem better:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

gif

For more contex, this is the init(); function:

GLFWwindow* init() {
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif


    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Thing", nullptr, nullptr);
    if (window == nullptr) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        exit(1);
    }
    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        exit(1);
    }
    glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
    return window;
}

So what could be cause, and what could be a solution? I tried searching online but to no avail.

I thought this could be some sort of buffer overflow, but I cannot see where this could happen.
Also if it helps I’m using Clion with CMake, CPP 23, OpenGL 3.3 and Arch Linux.

>Solution :

If N_EBOS is 15, more than 1 that is, then the second argument of glGenBuffers must point to the first item of the array of at least this size, otherwise you’ll get a buffer overflow.

::std::array<GLuint, N_EBOS> ebos{};
::glGenBuffers(ebos.size(), ebos.data());

And don’t forget to call ::glGetError to figure out whether call succeeded.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading