Will glBufferData leak memory?

Hey guys, I have a decade’s experience in D3D, but I’m a novice on OpenGL. My project requires me to use OpenGL as it’s cross-platform.

Will glBufferData leak memory?

My program looks like this

glGenBuffers(…)
glBindBuffer()
main_loop
{
glBufferData( GL_ARRAY_BUFFER , size , data , GL_DYNAMIC_DRAW );
draw
}
glDestroyBuffers(…)

As you see, the VBO will be a dynamic one. And the buffer size is always changing.
If the glBufferData will cause memory leak. Will it appear in video memory? Because I have tested it, while memory leak didn’t take place in my system memory.

Thank you!

Good luck! :slight_smile:

HI,
You should call glBufferData only once when u create the buffer object for the first time. For modifying the data in the buffer object, use glBufferSubData or glMapBufferRange functions. The wiki entry here (http://www.opengl.org/wiki/Buffer_Objects#Mapping) seems to answer your query quite well worth having a look.

Note that it is perfectly legal to reallocate buffers with glBufferData using different sizes and usage hints. OpenGL will accommodate you, and if you do, there should not be a memory leak. If there is, that’s a bug in the driver.

However, as you’re experienced in D3D and inexperienced in OpenGL, a word of warning: Just because you can do something in OpenGL does not mean you should. OpenGL will cheerfully allow you to do all kinds of things that are ultimately antithetical to performance (speed or memory).

For example, buffer reallocation will almost certainly fragment graphics memory. If that can’t be helped for your application, then so be it. But most applications can and therefore should avoid allocating buffers like this.

Note that there is a difference between reallocating the buffer and invalidating it. Reallocation means to call glBufferData with a different size and/or usage hint. Invalidation means that you call it with the same size and usage as before. That last part is important, because it tells the driver that you don’t care about what is currently in the buffer. It says that you want to wipe it out and make the buffer anew.

Thank you guys!
Especially Alfonse answered me perfectly.

Yes, actually reallocation is not allowed in D3D. When reallocating, I have to copy the entire buffer data from system memory to video memory. so I will reserve a proper size of video memory to store the buffer, and use glBufferSubData to update it when new primitives are added. Just like std::vector does.