Reputation: 6515
Suppose I call glGenBuffers (or createBuffer in WebGL), and later I lose the buffer name. E.g. it goes out of scope, is garbage collected, whatever the case may be. Making an analogy to C programming, that seems like a memory leak. Effectively, there's a block of allocated memory (on the GPU) with no pointer to it. I'm guessing the GPU can't garbage collect that memory, because it can't automatically infer that the client application is done with it.
First, is this indeed a memory leak? Second, if I call glDeleteBuffers (or deleteBuffer in WebGL) before losing my buffer name, does that free the memory and avoid a leak?
Upvotes: 3
Views: 3072
Reputation: 5137
Yes, that will cause a memory leak. You have to call glDeleteBuffers
for every allocated buffer. If you call it, it frees the data on the GPU and reverts the binding to 0. If you wouldn't call it, GPU would eventually run out of memory and your driver would probably crash.
Also note, that you don't need to call glDeleteBuffers
after every call of glBufferData
, it destroys any data that was previously bound to it. Call glDeleteBuffers
only once, when you won't use the buffer anymore.
Upvotes: 6