Vortice.Windows
Vortice.Windows copied to clipboard
Possible Memory Leak?
Hello, while following the https://github.com/walbourn/directx-vs-templates/blob/main/d3d11game_win32_dr/DeviceResources.cpp UpdateColorSpace method implementation, I noticed a possible memory leak.
If I do
IDXGIOutput bestOutput = null;
int bestIntersectArea = -1;
while(true)
{
for (var adapterIndex = 0;
_dxgiFactory.EnumAdapters(adapterIndex, out IDXGIAdapter adapter).Success;
++adapterIndex)
{
for (var outputIndex = 0;
adapter.EnumOutputs(outputIndex, out IDXGIOutput output).Success;
++outputIndex)
{
// Get the rectangle bounds of current output.
OutputDescription desc = output.Description;
var r = desc.DesktopCoordinates;
// Compute the intersection
int intersectArea = ComputeIntersectionArea(ax1, ay1, ax2, ay2, r.Left, r.Top, r.Right, r.Bottom);
if (intersectArea > bestIntersectArea)
{
var tempOutput = bestOutput;
bestOutput = output;
bestIntersectArea = intersectArea;
tempOutput?.Dispose();
}
}
adapter.Dispose();
}
}
I'll have a lot of memory used after a few seconds, because somehow I'm not able to dispose properly the adapter and output variables.
Although of course I would never do a while(true), this can still happen because UpdateColorSpace is called every time the window moves, so if I keep moving the window, the memory usage increases as if it's on a loop.
Is there something I can do or it needs to be fixed in Vortice.Windows? I noticed that this is similar to an issue that was reported before, https://github.com/amerkoleci/Vortice.Windows/issues/58
Not sure what is causing you an issue, does it happen in real application? (instead of while(true))?
With this commit and new beta I've changed and now I cache adapters and outputs:
https://github.com/amerkoleci/Vortice.Windows/commit/6654e951105bd14a0466bae0b4ce504709179d9e
Yeah, I have that code inside the UpdateColorSpace method which is called every time the window moves, which prompted me to open this issue. The while true was only a simple scenario to reproduce.
Thanks for that, I'll give it a try tonight and give back some feedback.
It improved quite a lot, but there's still other issues. I guess that the QueryInterfaceOrNull itself leaks easily, so if it's called multiple times, the memory will increase. I could cache it, but then the output.Description also leaks.
The code I have currently is this one: https://github.com/BMarques/LearnDirectx/blob/d35910ecb48423141b5057425130cf04f3e071f8/BasicGameLoop/DX/DeviceResources.cs#L474