Access violation exception when calling Index Buffer destructor at the end of function scope [duplicate]

So, i’m currently trying to make some OpenGL tests using ImGui on VS2019.
My ideia it’s to make a class TestTexture that has a OnRender() function, this class inherit from a Test class and than overrides it’s OnRender function.

TestTexture’s OnRender() implementation:

void test::TestTexture::OnRender()

VertexArray va;

VertexBuffer vb(/*args*/);

VertexBufferLayout layout;

layout.Push<float>(2);

layout.Push<float>(2);

va.AddBuffer(vb, layout);

IndexBuffer ib(indices, 6);

glm::mat4 proj = glm::ortho(0.0f, 640.0f, 0.0f, 480.0f, -1.0f, 1.0f);
glm::mat4 view = glm::translate(glm::mat4(1.0f), glm::vec3(0,0,0));

Shader shader("res/shaders/FirstShader.shader");
shader.Bind();

Texture texture("res/textures/texture.png");
texture.Bind();
shader.SetUniform1i("u_Texture", 0);

Renderer renderer;
renderer.Clear();

glm::mat4 model = glm::translate(glm::mat4(1.0f), translation);
glm::mat4 mvp = proj * view * model;
shader.Bind();
shader.SetUniformMat4f("u_MVP", mvp);

renderer.Draw(va, ib, shader);

However, the moment this function’s scopes ends and the destructors are called i get a Unhandled exception at 0x78E2A730 (ig9icd32.dll) in testeGL.exe: 0xC0000005: Access violation reading location 0x01164000 error message in Index Buffer destructor

This memory address location 0x01164000 (at least i think it’s a memory address), changes every time and it’s not the same as the one my Index Buffer id is located at.

~IndexBuffer():

IndexBuffer::~IndexBuffer()
{
    GLCall(glDeleteBuffers(GL_ARRAY_BUFFER, &m_RendererID));
}

Debugging &m_RendererID i can see that it’s memory address it’s different than the one in the error message.
I did some research and, as far as i was able understood, what is happening is that it’s trying to delete an already deleted buffer, but i just can’t see where this might be happening, even if a do something like

IndexBuffer ib(indices, 6);
ib.~IndexBuffer();

It still throws the error regardless.

The same code that is in TestTexture::OnRender() was also written directly on main() before i put it in the function and everything was working just fine.

(For reference, everything here was done following The Cherno’s guides for OpenGl)

Source: Windows Questions C++

LEAVE A COMMENT