Fixed memory leak in RPF Explorer

This commit is contained in:
dexy 2022-07-30 21:54:21 +10:00
parent cf81aebbb7
commit ba16eab570
7 changed files with 41 additions and 18 deletions

View File

@ -898,19 +898,21 @@ namespace CodeWalker.GameFiles
{ {
using (DeflateStream ds = new DeflateStream(new MemoryStream(bytes), CompressionMode.Decompress)) using (DeflateStream ds = new DeflateStream(new MemoryStream(bytes), CompressionMode.Decompress))
{ {
MemoryStream outstr = new MemoryStream(); using (var outstr = new MemoryStream())
ds.CopyTo(outstr);
byte[] deflated = outstr.GetBuffer();
byte[] outbuf = new byte[outstr.Length]; //need to copy to the right size buffer for output.
Array.Copy(deflated, outbuf, outbuf.Length);
if (outbuf.Length <= bytes.Length)
{ {
LastError = "Warning: Decompressed data was smaller than compressed data..."; ds.CopyTo(outstr);
//return null; //could still be OK for tiny things! byte[] deflated = outstr.GetBuffer();
} byte[] outbuf = new byte[outstr.Length]; //need to copy to the right size buffer for output.
Buffer.BlockCopy(deflated, 0, outbuf, 0, outbuf.Length);
return outbuf; if (outbuf.Length <= bytes.Length)
{
LastError = "Warning: Decompressed data was smaller than compressed data...";
//return null; //could still be OK for tiny things!
}
return outbuf;
}
} }
} }
catch (Exception ex) catch (Exception ex)
@ -924,13 +926,15 @@ namespace CodeWalker.GameFiles
{ {
using (MemoryStream ms = new MemoryStream()) using (MemoryStream ms = new MemoryStream())
{ {
DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress, true); using (var ds = new DeflateStream(ms, CompressionMode.Compress, true))
ds.Write(data, 0, data.Length); {
ds.Close(); ds.Write(data, 0, data.Length);
byte[] deflated = ms.GetBuffer(); ds.Close();
byte[] outbuf = new byte[ms.Length]; //need to copy to the right size buffer... byte[] deflated = ms.GetBuffer();
Array.Copy(deflated, outbuf, outbuf.Length); byte[] outbuf = new byte[ms.Length]; //need to copy to the right size buffer...
return outbuf; Buffer.BlockCopy(deflated, 0, outbuf, 0, outbuf.Length);
return outbuf;
}
} }
} }

View File

@ -46,6 +46,7 @@ namespace CodeWalker.Rendering
try try
{ {
//SharpDX.Configuration.EnableObjectTracking = true;
SwapChainDescription scd = new SwapChainDescription() SwapChainDescription scd = new SwapChainDescription()
{ {
@ -154,12 +155,18 @@ namespace CodeWalker.Rendering
dxform.CleanupScene(); dxform.CleanupScene();
if (context != null) context.ClearState();
//dipose of all objects //dipose of all objects
if (depthview != null) depthview.Dispose(); if (depthview != null) depthview.Dispose();
if (depthbuffer != null) depthbuffer.Dispose(); if (depthbuffer != null) depthbuffer.Dispose();
if (targetview != null) targetview.Dispose(); if (targetview != null) targetview.Dispose();
if (backbuffer != null) backbuffer.Dispose(); if (backbuffer != null) backbuffer.Dispose();
if (swapchain != null) swapchain.Dispose(); if (swapchain != null) swapchain.Dispose();
if (context != null) context.Dispose();
//var objs = SharpDX.Diagnostics.ObjectTracker.FindActiveObjects();
if (device != null) device.Dispose(); if (device != null) device.Dispose();
GC.Collect(); GC.Collect();

View File

@ -323,6 +323,7 @@ namespace CodeWalker.Rendering
cacheitems.Clear(); cacheitems.Clear();
itemsToUnload = new ConcurrentQueue<TVal>(); itemsToUnload = new ConcurrentQueue<TVal>();
keysToInvalidate = new ConcurrentQueue<TKey>(); keysToInvalidate = new ConcurrentQueue<TKey>();
CacheUse = 0;
} }

View File

@ -235,6 +235,8 @@ namespace CodeWalker.Rendering
bsAdd.Dispose(); bsAdd.Dispose();
rsSolid.Dispose(); rsSolid.Dispose();
rsWireframe.Dispose(); rsWireframe.Dispose();
rsSolidDblSided.Dispose();
rsWireframeDblSided.Dispose();
Widgets.Dispose(); Widgets.Dispose();
Paths.Dispose(); Paths.Dispose();
@ -284,6 +286,8 @@ namespace CodeWalker.Rendering
public void BeginFrame(DeviceContext context, double currentRealTime, float elapsedTime) public void BeginFrame(DeviceContext context, double currentRealTime, float elapsedTime)
{ {
if (disposed) return;
CurrentRealTime = currentRealTime; CurrentRealTime = currentRealTime;
CurrentElapsedTime = elapsedTime; CurrentElapsedTime = elapsedTime;

View File

@ -1025,6 +1025,7 @@ namespace CodeWalker.Rendering
texsampler.Dispose(); texsampler.Dispose();
texsampleranis.Dispose(); texsampleranis.Dispose();
texsamplertnt.Dispose(); texsamplertnt.Dispose();
texsamplertntyft.Dispose();
foreach (InputLayout layout in layouts.Values) foreach (InputLayout layout in layouts.Values)
{ {

View File

@ -340,6 +340,7 @@ namespace CodeWalker.Rendering
texsampler.Dispose(); texsampler.Dispose();
texsampleranis.Dispose();
layout.Dispose(); layout.Dispose();

View File

@ -415,6 +415,11 @@ namespace CodeWalker.Rendering
texsampler.Dispose(); texsampler.Dispose();
texsampler = null; texsampler = null;
} }
if (texsamplerc != null)
{
texsamplerc.Dispose();
texsamplerc = null;
}
foreach (InputLayout layout in layouts.Values) foreach (InputLayout layout in layouts.Values)
{ {