glDeleteBuffers called implicitly immideatly after buffer creation (Modern OpenGL).

Started by
5 comments, last by Jezper 10 months, 1 week ago

Hello folks! Recently I stumbled upon this very weird issue where I try to create a buffer but then it's immediately after. I definitely do not call it. What could be causing this weird behavior. Thanks in advance!

RELEVANT PART OF APITRACE DUMP

4420 glCreateBuffers(n = 1, buffers = &1)
4421 glDeleteBuffers(n = 1, buffers = &1)

These next 2 code blocks are from 2 separate code blocks but they are executed back-to-back with no code executed in between. It's also worth noting that I double checked and the pointer is valid throughout.

if(isInstanced){
	instanceVBO.reset(new GLuint);
	instanceCount.reset(new uint32_t);
	glCreateBuffers(1, instanceVBO.get());
	printf("%i\n", glIsBuffer(instanceVBO.get()[0])); // THE BUFFER IS VALID HERE
}


void Sprite::UpdateInstancedArray(std::vector<glm::vec2> instances){
glBindVertexArray(0);
std::vector<float> instancesRaw;


for(glm::vec2 instance : instances){
instancesRaw.push_back(instance.x);
instancesRaw.push_back(instance.y);
}


glBindBuffer(GL_ARRAY_BUFFER, instanceVBO.get()[0]);

printf("%i\n", glIsBuffer(instanceVBO.get()[0])); // BUT NOT HERE??
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*instancesRaw.size(), instancesRaw.data(), GL_DYNAMIC_DRAW);


Also if I just remove the glCreateBuffers part entirely OpenGL still tries to delete something…

4420 glDeleteBuffers(n = 1, buffers = NULL) // incomplete

Advertisement

jakob_tadej8 said:
glCreateBuffers(1, instanceVBO.get());

This seems suspicious. Does instanceVBO.get() return a valid pointer to a GLuint? I'm guessing that you are passing a bad pointer into glCreateBuffers(), which overwrites some garbage memory location with the buffer ID, and makes the first call to glIsBuffer() succeed because nothing else has had time to overwrite that location. By the second call to glIsBuffer(), the garbage memory has been overwritten by something else (e.g. your instancesRaw vector), and so it fails because the buffer ID has been overwritten and isn't one of the ones that was previously created.

Edit: I re-read your code and see that you do assign a new GLuint to the instanceVBO, assuming it is std::unique_ptr. Does the behavior change if you use glGenBuffers() instead of glCreateBuffers()?

@aressera as per last part of my post, OpenGL still calls glDeleteBuffers even though I havent created any. Which of course results in a segfault. Also for clarification its a shared_ptr. I appreciate the help!

I would start commenting out OpenGL calls and see which one is causing it to delete buffers. You are almost certainly using the API incorrectly in some way or have a bug in your code resulting in undefined behavior.

jakob_tadej8 said:
if(isInstanced){ instanceVBO.reset(new GLuint); instanceCount.reset(new uint32_t); glCreateBuffers(1, instanceVBO.get()); printf(

What is this instanceVBO ? I have the impression that it is a weird structure / class, similar to shared pointers. If that's the case, when this variable gets out of scope, a destructor is then called, and might call DeleteBuffers ?

With simple debugging, you can easily figure out what's happening.

Do you call glDeleteBuffers from your Sprite class destructor?

This topic is closed to new replies.

Advertisement