forked from MoonsideGames/MoonWorks
fix graphics resources disposing out of order
parent
7f6236cb49
commit
d10f018f14
|
@ -12,6 +12,8 @@ namespace MoonWorks.Graphics
|
|||
|
||||
private readonly Queue<CommandBuffer> commandBufferPool;
|
||||
|
||||
private readonly List<WeakReference> resources = new List<WeakReference>();
|
||||
|
||||
public GraphicsDevice(
|
||||
IntPtr deviceWindowHandle,
|
||||
Refresh.PresentMode presentMode,
|
||||
|
@ -78,16 +80,44 @@ namespace MoonWorks.Graphics
|
|||
Refresh.Refresh_Wait(Handle);
|
||||
}
|
||||
|
||||
internal void AddResourceReference(WeakReference resourceReference)
|
||||
{
|
||||
lock (resources)
|
||||
{
|
||||
resources.Add(resourceReference);
|
||||
}
|
||||
}
|
||||
|
||||
internal void RemoveResourceReference(WeakReference resourceReference)
|
||||
{
|
||||
lock (resources)
|
||||
{
|
||||
resources.Remove(resourceReference);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!IsDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
// TODO: dispose managed state (managed objects)
|
||||
lock (resources)
|
||||
{
|
||||
foreach (var resource in resources)
|
||||
{
|
||||
var target = resource.Target;
|
||||
if (target != null)
|
||||
{
|
||||
(target as IDisposable).Dispose();
|
||||
}
|
||||
}
|
||||
resources.Clear();
|
||||
}
|
||||
|
||||
Refresh.Refresh_DestroyDevice(Handle);
|
||||
}
|
||||
|
||||
IsDisposed = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace MoonWorks.Graphics
|
||||
{
|
||||
|
@ -12,9 +10,14 @@ namespace MoonWorks.Graphics
|
|||
public bool IsDisposed { get; private set; }
|
||||
protected abstract Action<IntPtr, IntPtr> QueueDestroyFunction { get; }
|
||||
|
||||
private WeakReference selfReference;
|
||||
|
||||
public GraphicsResource(GraphicsDevice device)
|
||||
{
|
||||
Device = device;
|
||||
|
||||
selfReference = new WeakReference(this);
|
||||
Device.AddResourceReference(selfReference);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
|
@ -22,6 +25,13 @@ namespace MoonWorks.Graphics
|
|||
if (!IsDisposed)
|
||||
{
|
||||
QueueDestroyFunction(Device.Handle, Handle);
|
||||
|
||||
if (selfReference != null)
|
||||
{
|
||||
Device.RemoveResourceReference(selfReference);
|
||||
selfReference = null;
|
||||
}
|
||||
|
||||
IsDisposed = true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue