Skip to content

Commit

Permalink
feat: Added allocation stacktraces to memory manager
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoTenPvP committed Sep 10, 2019
1 parent abd5ccc commit 1e48aa3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
17 changes: 15 additions & 2 deletions Ruffles/Memory/HeapMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public byte[] Buffer
private byte[] _buffer;
internal bool isDead;

#if DEBUG
internal string allocStacktrace;
#endif

public HeapMemory(uint size)
{
_buffer = new byte[size];
Expand Down Expand Up @@ -53,11 +57,20 @@ public void EnsureSize(uint size)
{
if (!isDead)
{
if (Logging.CurrentLogLevel <= LogLevel.Warning) Logging.LogWarning("Memory was just leaked from the MemoryManager [Size=" + Buffer.Length + "]");
#if DEBUG
if (Logging.CurrentLogLevel <= LogLevel.Warning) Logging.LogWarning("Memory was just leaked from the MemoryManager [Size=" + Buffer.Length + "] AllocStack: " + allocStacktrace);
#else
if (Logging.CurrentLogLevel <= LogLevel.Warning) Logging.LogWarning("Memory was just leaked from the MemoryManager [Size=" + Buffer.Length + "]");

#endif
}
else
{
if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogWarning("Dead memory was just leaked from the MemoryManager [Size=" + _buffer.Length + "]");
#if DEBUG
if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogWarning("Dead memory was just leaked from the MemoryManager [Size=" + _buffer.Length + "] AllocStack: " + allocStacktrace);
#else
if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogWarning("Dead memory was just leaked from the MemoryManager [Size=" + _buffer.Length + "]");
#endif
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions Ruffles/Memory/MemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ internal class MemoryManager : IDisposable
private const uint minPointerArraySize = 64;
private const uint pointerArraySizeMultiple = 64;

private readonly SocketConfig _configuration;

private readonly SocketConfig _configuration;

internal static uint CalculateMultiple(uint minSize, uint multiple)
{
uint remainder = minSize % multiple;
Expand All @@ -50,7 +50,7 @@ internal MemoryManager(SocketConfig config)

internal object[] AllocPointers(uint size)
{
uint allocSize = Math.Max(minPointerArraySize, CalculateMultiple(size, pointerArraySizeMultiple));
uint allocSize = Math.Max(minPointerArraySize, CalculateMultiple(size, pointerArraySizeMultiple));

if (!_configuration.PoolPointerArrays)
{
Expand Down Expand Up @@ -117,7 +117,7 @@ internal HeapMemory AllocHeapMemory(uint size)
_hasWarnedAboutHeapMemoryLeaks = true;
}

memory = new HeapMemory(allocSize);
memory = new HeapMemory(allocSize);
}

memory.EnsureSize(allocSize);
Expand All @@ -126,6 +126,11 @@ internal HeapMemory AllocHeapMemory(uint size)
memory.VirtualCount = size;
memory.VirtualOffset = 0;

#if DEBUG
// The allocation stacktrace allows us to see where the alloc occured that caused the leak
memory.allocStacktrace = Environment.StackTrace;
#endif

return memory;
}

Expand Down

0 comments on commit 1e48aa3

Please sign in to comment.