Search code examples
c++openglgame-engineglfwglew

Access Violation in calling glGenBuffers()


I'm trying to develop a game engine and was adding a batched rendering feature. I have 2 problems here,

  1. Have some unusual access violation errors.

    I initialized the Buffer classes as a member variable of the Renderer class

    Scope<VertexBuffer> vertex = CreateScope<OpenGLVertexBuffer>(ME_MAX_VERTEX_BUFFER_SIZE, GL_DYNAMIC_DRAW);
    Scope<IndexBuffer> index = CreateScope<OpenGLIndexBuffer>(ME_MAX_INDEX_BUFFER_SIZE, GL_DYNAMIC_DRAW);
    

    The Scope meantioned above refers unique_ptr and CreateScope refers make_unique
    The code file:
    Header: https://github.com/VikramSGIT/MarsEngine/blob/master/MarsEngine/src/RenderAPI/OpenGL/OpenGLRenderer.cpp
    CPP: https://github.com/VikramSGIT/MarsEngine/blob/master/MarsEngine/src/RenderAPI/OpenGL/OpenGLRenderer.cpp
    The constructor of the Vertex Buffer classes (Index Buffer Class Have similar constructor)

    OpenGLVertexBuffer::OpenGLVertexBuffer(const unsigned int& size, const unsigned int& mode)
        :Emptybuffer(true)
    {
        ME_PROFILE_TRACE_CALL();
    
        GLLogCall(glGenBuffers(1, &m_RendererID));
        GLLogCall(glBindBuffer(GL_ARRAY_BUFFER, m_RendererID));
        GLLogCall(glBufferData(GL_ARRAY_BUFFER, size, nullptr, mode));
    }
    

    Had Access Violation excactly at glGenBuffers(1 ,&m_RendererID). Even doudle checked that i had called glewInit() after glfwMakeCurrentContext(window).

  2. When I tried to add glewInit() inside the above OpenGLVertexBuffer class constructor. There came another unusual problem where glDeleteBuffers(1, &m_RendererID) is stuck unterminated!!

    OpenGLVertexBuffer::~OpenGLVertexBuffer()
    {
        ME_PROFILE_TRACE_CALL();
    
        GLLogCall(glDeleteBuffers(1, &m_RendererID));
    }
    

    Then I ran the same model of code in VS Code, Ran Smooth. So this concludes that I had no driver problems.

    And to be noted I didn't get an output (was not included as I'm not sure with logic) on my screen and ImGui ran smoothly. But I ran breakpoints over every part of the logic, buffers filled up as expected ba can fain waz goin wroonng :(

Link to my Github repo: https://github.com/VikramSGIT/MarsEngine


Solution

  • template<typename T>
    using Ref = std::shared_ptr<T>;
    
    template<typename T>
    using Scope = std::shared_ptr<T>;
    
    template<typename T, typename... Args>
    Ref<T> CreateRef(Args&& ... args) { return std::make_shared<T>(std::forward<Args (args)...); }
    
    template<typename T, typename ... Args>
    Scope<T> CreateScope(Args&& ...args) { return std::make_unique<T>(std::forward<Args> (args)...); }
    

    Your CreateScope function creates a unique_ptr, but your Scope object stores a shared_ptr. I suspect the problem is this:

    template<typename T>
    using Scope = std::shared_ptr<T>;
    

    Which was supposed to be:

    template<typename T>
    using Scope = std::unique_ptr<T>;