Jump to content
  • Advertisement
Sign in to follow this  

3D XNA - should I manually dispose VertexBuffers?

Recommended Posts

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 :)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!