c++openglglad

OpenGL Glad, glDeleteVertexArrays(), glGetError() causes segfault


In the destructor of a renderer class, I want to delete the buffers/vertex array object associated with the shape. I save both (VAO/VBO) as member variables after initialization. Tracked down the issue to this, this causes the seg-fault.

Error from lldb:

stop reason = signal SIGSEGV: invalid address (fault address: 0x7ffff778efc0)

* thread #1, name = 'gulua', stop reason = signal SIGSEGV: invalid address (fault address: 0x7ffff778efc0)
  * frame #0: 0x00007ffff778efc0
    frame #1: 0x00005555555ee595 gulua`GuluaResources::ShapeRenderer::~ShapeRenderer(this=0x0000555555f06f10) at ShapeRenderer.cpp:10:22

Note: only occurs on Linux machine, Mac machine does not encounter this error. Compiling with clang.

The buffer/vertex array is generated here which is called:

ShapeRenderer::~ShapeRenderer() {
    glDeleteVertexArrays(1, &mVAO);
    glDeleteBuffers(GL_ARRAY_BUFFER, &mVBO);
}

void TriangleRenderer::initShape() {
    glGenVertexArrays(1, &mVAO);
    glGenBuffers(1, &mVBO);


    glBindBuffer(GL_ARRAY_BUFFER, mVBO);
    glBufferData(GL_ARRAY_BUFFER, mVertices.size() * sizeof(mVertices), &mVertices[0], GL_STREAM_DRAW);

    glBindVertexArray(mVAO);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);

    glBindBuffer(GL_ARRAY_BUFFER, 0); 
    glBindVertexArray(0); 
}

Tried searching around about this. Suspecting that I'm confused on how array/buffers are stored post-generation / andor confused about C++ class destruction.

Edit:

Tried to get error set with glGetError() which causes its own segfault. Can I not call this in a destructor?


Solution

  • glfwterminate() prior to deleting vector arrays/buffers. Had to make sure to deallocate objects that use opengl functions prior to exiting. All is well :)