Skip to content

Commit

Permalink
[Graphics] Added function to help track down resource leaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tape-Worm committed Jan 12, 2022
1 parent cf83e36 commit 7ef3cc8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Examples/Gorgon.Renderers.2D/Balls/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,9 @@ static void Main()
{
try
{
GorgonGraphics.IsObjectTrackingEnabled = true;
GorgonGraphics.IsDebugEnabled = true;

Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Expand Down
38 changes: 38 additions & 0 deletions Gorgon/Gorgon.Graphics.Core/GorgonGraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using Gorgon.Core;
Expand Down Expand Up @@ -590,6 +591,16 @@ private void SetDrawStates(D3DState state, in GorgonColor factor, int blendSampl
_stateApplicator.BindResourceState(resourceChanges, state);
}

/// <summary>
/// Function to report any objects that are left alive at the end of a session.
/// </summary>
private void ReportLiveObjectsInternal(D3D11.Device device)
{
using D3D11.DeviceDebug debugDevice = new(device);
debugDevice.ReportLiveDeviceObjects(D3D11.ReportingLevel.IgnoreInternal);
}


/// <summary>
/// Function to check for the minimum windows 10 build that Gorgon Graphics supports.
/// </summary>
Expand Down Expand Up @@ -1223,6 +1234,24 @@ public void SubmitIndirect(GorgonInstancedCall drawCall, GorgonBuffer indirectAr
}
}

/// <summary>
/// Function to report internal objects that are still alive to the debug output.
/// </summary>
/// <remarks>
/// <para>
/// This method is used to track any object that is still alive even when they should be deactivated. Typically one would call this at shut down due to resource leaks noted by the debug runtime.
/// </para>
/// </remarks>
public void ReportLiveObjects()
{
if ((!IsObjectTrackingEnabled) || (!IsDebugEnabled) || (D3DDevice is null))
{
return;
}

ReportLiveObjectsInternal(D3DDevice);
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
Expand Down Expand Up @@ -1261,6 +1290,15 @@ public void Dispose()
// Reset the state for the context. This will ensure we don't have anything bound to the pipeline when we shut down.
context?.ClearState();
context?.Dispose();

#if DEBUG
if ((IsObjectTrackingEnabled) && (IsDebugEnabled))
{
Debug.WriteLine("GORGON: A live device object is normal, and will be destroyed after this report.");
ReportLiveObjectsInternal(device);
}
#endif

device?.Dispose();
adapter?.Dispose();
factory?.Dispose();
Expand Down

0 comments on commit 7ef3cc8

Please sign in to comment.