Bug fixes for virtual memory/stream leaks

This commit is contained in:
Timothy Baldridge 2020-03-04 22:27:15 -07:00
parent 854bdbcaf3
commit 181a0f6b6a
5 changed files with 14 additions and 6 deletions

View File

@ -172,6 +172,7 @@ namespace Compression.BSA
using var ms = new MemoryStream(); using var ms = new MemoryStream();
using (var ds = new DeflaterOutputStream(ms)) using (var ds = new DeflaterOutputStream(ms))
{ {
ds.IsStreamOwner = false;
src.CopyToLimit(ds, (int)chunk.FullSz); src.CopyToLimit(ds, (int)chunk.FullSz);
} }
@ -204,6 +205,7 @@ namespace Compression.BSA
bw.Write((ulong)pos); bw.Write((ulong)pos);
bw.BaseStream.Position = pos; bw.BaseStream.Position = pos;
_dataSlab.CopyToLimit(bw.BaseStream, (int)_dataSlab.Length); _dataSlab.CopyToLimit(bw.BaseStream, (int)_dataSlab.Length);
_dataSlab.Dispose();
} }
} }
@ -230,6 +232,7 @@ namespace Compression.BSA
{ {
using (var ds = new DeflaterOutputStream(ms)) using (var ds = new DeflaterOutputStream(ms))
{ {
ds.IsStreamOwner = false;
builder._dataSrc.CopyTo(ds); builder._dataSrc.CopyTo(ds);
} }
@ -268,6 +271,7 @@ namespace Compression.BSA
wtr.BaseStream.Position = pos; wtr.BaseStream.Position = pos;
_dataSrc.Position = 0; _dataSrc.Position = 0;
_dataSrc.CopyToLimit(wtr.BaseStream, (int)_dataSrc.Length); _dataSrc.CopyToLimit(wtr.BaseStream, (int)_dataSrc.Length);
_dataSrc.Dispose();
} }
} }
} }

View File

@ -298,6 +298,7 @@ namespace Compression.BSA
var r = new MemoryStream(); var r = new MemoryStream();
using (var w = new DeflaterOutputStream(r)) using (var w = new DeflaterOutputStream(r))
{ {
w.IsStreamOwner = false;
_srcData.CopyTo(w); _srcData.CopyTo(w);
} }
_srcData = _bsa._slab.Allocate(r.Length); _srcData = _bsa._slab.Allocate(r.Length);
@ -341,11 +342,13 @@ namespace Compression.BSA
wtr.Write((uint) _originalSize); wtr.Write((uint) _originalSize);
_srcData.Position = 0; _srcData.Position = 0;
_srcData.CopyToLimit(wtr.BaseStream, (int)_srcData.Length); _srcData.CopyToLimit(wtr.BaseStream, (int)_srcData.Length);
_srcData.Dispose();
} }
else else
{ {
_srcData.Position = 0; _srcData.Position = 0;
_srcData.CopyToLimit(wtr.BaseStream, (int)_srcData.Length); _srcData.CopyToLimit(wtr.BaseStream, (int)_srcData.Length);
_srcData.Dispose();
} }
} }
} }

View File

@ -66,6 +66,7 @@ namespace Compression.BSA
{ {
bw.BaseStream.Position = _state.DataOffset + state.Offset; bw.BaseStream.Position = _state.DataOffset + state.Offset;
data.CopyTo(bw.BaseStream); data.CopyTo(bw.BaseStream);
data.Dispose();
} }
} }

View File

@ -18,7 +18,7 @@ namespace Wabbajack.Common
public DiskSlabAllocator() public DiskSlabAllocator()
{ {
_name = Guid.NewGuid().ToString(); _name = Guid.NewGuid().ToString();
_mmap = MemoryMappedFile.CreateNew(_name, (long)1 << 34); _mmap = MemoryMappedFile.CreateNew(null, (long)1 << 34);
} }
public Stream Allocate(long size) public Stream Allocate(long size)

View File

@ -239,17 +239,17 @@ namespace Wabbajack.Lib
using (var a = bsa.State.MakeBuilder()) using (var a = bsa.State.MakeBuilder())
{ {
await bsa.FileStates.PMap(Queue, state => var streams = await bsa.FileStates.PMap(Queue, state =>
{ {
Status($"Adding {state.Path} to BSA"); Status($"Adding {state.Path} to BSA");
using (var fs = File.OpenRead(Path.Combine(sourceDir, state.Path))) var fs = File.OpenRead(Path.Combine(sourceDir, state.Path));
{ a.AddFile(state, fs);
a.AddFile(state, fs); return fs;
}
}); });
Info($"Writing {bsa.To}"); Info($"Writing {bsa.To}");
a.Build(Path.Combine(OutputFolder, bsa.To)); a.Build(Path.Combine(OutputFolder, bsa.To));
streams.Do(s => s.Dispose());
} }
} }