MonoGame icon indicating copy to clipboard operation
MonoGame copied to clipboard

StructuredBuffer failing to release unmanaged resources, leading to memory leak

Open scatterlogical opened this issue 11 months ago • 1 comments

Prerequisites

  • [x] I have verified this issue is present in the develop branch
  • [x] I have searched open and closed issues to ensure it has not already been reported.

MonoGame Version

MonoGame.Framework.Compute.WindowsDX 3.8.3

Which MonoGame platform are you using?

MonoGame Windows Desktop Application (mgwindowsdx)

Operating System

Windows

Description

StructuredBuffers don't seem to release unmanaged resources when disposed, leading to massive memory leakage in unmanaged memory.

Steps to Reproduce

Using the indirect_draw_instances compute example, I changed the Draw() method as such:

`protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black);

        particleBuffer1 = new StructuredBuffer(GraphicsDevice, typeof(Particle), MaxParticleCount, BufferUsage.None, ShaderAccess.ReadWrite); //added
        particleBuffer2 = new StructuredBuffer(GraphicsDevice, typeof(Particle), MaxParticleCount, BufferUsage.None, ShaderAccess.ReadWrite); //added

        ComputeParticles(gameTime);

        DrawParticles();
        DrawText();
        DrawMousePointer();

        particleBuffer1.Dispose(); //added
        particleBuffer2.Dispose(); //added

        base.Draw(gameTime);   
    }`

Minimal Example Repo

No response

Expected Behavior

The unmanaged resources should be released when disposed is called, freeing unmanaged memory.

Resulting Behavior

Image

Files

No response

scatterlogical avatar Feb 10 '25 00:02 scatterlogical

For the record, yes I'm aware that creating new buffers every frame isn't a great idea, but this is just to demostrate and amplify the problem. Allocating and disposing a Texture2D like this doesn't have this effect.

scatterlogical avatar Feb 10 '25 00:02 scatterlogical