XNA - should I manually dispose VertexBuffers?

Started by
0 comments, last by george7378 4 years, 6 months ago

Hey everyone,

I've got a memory leak in my XNA/MonoGame app, and I'm pretty sure it's due to the fact that I create a lot of VertexBuffers manually and never dispose of them.

I'm dealing with a dynamic quadtree terrain where every possible tile, at all levels of detail (down to a limit I impose), is created at loading time, each with its own vertex buffer which is sent straight to the GPU when I want to draw that particular tile. This means that, each time I load a level, I have something like 24,000 small VertexBuffers comprising my terrain, and each frame I pick and choose which ones to draw based on the camera position.

The problem is that when I exit to the main menu and reload, I create another NEW quadtree which generates another 24,000 VertexBuffers. I can see that the memory allocated to the previous VertexBuffers is NOT being reclaimed, even though the quadtree objects which own them are no longer referenced and have been garbage collected.

So, my question is - do I have to manually dispose of a VertexBuffer every time I'm done with it in my program? I've seen a load of tutorials for XNA which create VertexBuffers and IndexBuffers manually and allocate data into them (here and here, to name two of the most famous) and they never dispose of them. Are they doing it wrong, or are they correct since the buffers are in use throughout the whole program and so will be disposed cleanly when it exits anyway?

Appreciate the help :)

This topic is closed to new replies.

Advertisement