clean up graphics resource code

pull/53/head
cosmonaut 2023-11-20 17:09:22 -08:00
parent b2a0ca3515
commit a736ed031d
2 changed files with 12 additions and 11 deletions

View File

@ -380,8 +380,7 @@ namespace MoonWorks.Graphics
{ {
foreach (var resource in resources) foreach (var resource in resources)
{ {
var target = resource.Target; if (resource.Target is IDisposable disposable)
if (target is IDisposable disposable)
{ {
disposable.Dispose(); disposable.Dispose();
} }

View File

@ -11,18 +11,18 @@ namespace MoonWorks.Graphics
public bool IsDisposed { get; private set; } public bool IsDisposed { get; private set; }
protected abstract Action<IntPtr, IntPtr> QueueDestroyFunction { get; } protected abstract Action<IntPtr, IntPtr> QueueDestroyFunction { get; }
internal GCHandle selfReference; private GCHandle SelfReference;
public GraphicsResource(GraphicsDevice device, bool trackResource = true) protected GraphicsResource(GraphicsDevice device)
{ {
Device = device; Device = device;
selfReference = GCHandle.Alloc(this, GCHandleType.Weak); SelfReference = GCHandle.Alloc(this, GCHandleType.Weak);
Device.AddResourceReference(selfReference); Device.AddResourceReference(SelfReference);
} }
internal GraphicsResourceDisposalHandle CreateDisposalHandle() private GraphicsResourceDisposalHandle CreateDisposalHandle()
{ {
return new GraphicsResourceDisposalHandle return new GraphicsResourceDisposalHandle
{ {
QueueDestroyAction = QueueDestroyFunction, QueueDestroyAction = QueueDestroyFunction,
@ -30,15 +30,15 @@ namespace MoonWorks.Graphics
}; };
} }
protected virtual void Dispose(bool disposing) protected void Dispose(bool disposing)
{ {
if (!IsDisposed) if (!IsDisposed)
{ {
if (Handle != IntPtr.Zero) if (Handle != IntPtr.Zero)
{ {
QueueDestroyFunction(Device.Handle, Handle); QueueDestroyFunction(Device.Handle, Handle);
Device.RemoveResourceReference(selfReference); Device.RemoveResourceReference(SelfReference);
selfReference.Free(); SelfReference.Free();
Handle = IntPtr.Zero; Handle = IntPtr.Zero;
} }
@ -50,6 +50,8 @@ namespace MoonWorks.Graphics
~GraphicsResource() ~GraphicsResource()
{ {
#if DEBUG #if DEBUG
// If the graphics device associated with this resource was already disposed, we assume
// that your game is in the middle of shutting down.
if (!IsDisposed && Device != null && !Device.IsDisposed) if (!IsDisposed && Device != null && !Device.IsDisposed)
{ {
// If you see this log message, you leaked a graphics resource without disposing it! // If you see this log message, you leaked a graphics resource without disposing it!