Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions UnitTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace Microsoft.IO.UnitTests
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -2481,6 +2483,33 @@ public void AdvanceOverReplacedTempBufferDoesNotMakeWritesVisible()
#endregion

#region Dispose and Pooling Tests
[TestCase(false, false)]
[TestCase(true, false)]
[TestCase(false, true)]
public void Dispose_PartiallyConstructedStream_DoesNotThrow(bool assignMemoryManager, bool assignBlocks)
{
var stream = (RecyclableMemoryStream)RuntimeHelpers.GetUninitializedObject(typeof(RecyclableMemoryStream));
try
{
if (assignMemoryManager)
{
typeof(RecyclableMemoryStream).GetField("memoryManager", BindingFlags.Instance | BindingFlags.NonPublic)!.SetValue(stream, this.GetMemoryManager());
}

if (assignBlocks)
{
typeof(RecyclableMemoryStream).GetField("blocks", BindingFlags.Instance | BindingFlags.NonPublic)!.SetValue(stream, new List<byte[]>());
}

var dispose = typeof(RecyclableMemoryStream).GetMethod("Dispose", BindingFlags.Instance | BindingFlags.NonPublic, null, [typeof(bool)], null)!;
Assert.DoesNotThrow(() => dispose.Invoke(stream, [false]));
}
finally
{
GC.SuppressFinalize(stream);
}
}

[Test]
public void Pooling_NewMemoryManagerHasZeroFreeAndInUseBytes()
{
Expand Down
5 changes: 5 additions & 0 deletions src/RecyclableMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ internal RecyclableMemoryStream(RecyclableMemoryStreamManager memoryManager, Gui
/// <param name="disposing">Whether we're disposing (true), or being called by the finalizer (false).</param>
protected override void Dispose(bool disposing)
{
if (this.memoryManager == null || this.blocks == null)
{
return;
}

if (this.disposed)
{
string? doubleDisposeStack = null;
Expand Down
Loading