Skip to content

Commit 1e48aa3

Browse files
committed
feat: Added allocation stacktraces to memory manager
1 parent abd5ccc commit 1e48aa3

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Ruffles/Memory/HeapMemory.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public byte[] Buffer
2525
private byte[] _buffer;
2626
internal bool isDead;
2727

28+
#if DEBUG
29+
internal string allocStacktrace;
30+
#endif
31+
2832
public HeapMemory(uint size)
2933
{
3034
_buffer = new byte[size];
@@ -53,11 +57,20 @@ public void EnsureSize(uint size)
5357
{
5458
if (!isDead)
5559
{
56-
if (Logging.CurrentLogLevel <= LogLevel.Warning) Logging.LogWarning("Memory was just leaked from the MemoryManager [Size=" + Buffer.Length + "]");
60+
#if DEBUG
61+
if (Logging.CurrentLogLevel <= LogLevel.Warning) Logging.LogWarning("Memory was just leaked from the MemoryManager [Size=" + Buffer.Length + "] AllocStack: " + allocStacktrace);
62+
#else
63+
if (Logging.CurrentLogLevel <= LogLevel.Warning) Logging.LogWarning("Memory was just leaked from the MemoryManager [Size=" + Buffer.Length + "]");
64+
65+
#endif
5766
}
5867
else
5968
{
60-
if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogWarning("Dead memory was just leaked from the MemoryManager [Size=" + _buffer.Length + "]");
69+
#if DEBUG
70+
if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogWarning("Dead memory was just leaked from the MemoryManager [Size=" + _buffer.Length + "] AllocStack: " + allocStacktrace);
71+
#else
72+
if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogWarning("Dead memory was just leaked from the MemoryManager [Size=" + _buffer.Length + "]");
73+
#endif
6174
}
6275
}
6376
}

Ruffles/Memory/MemoryManager.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ internal class MemoryManager : IDisposable
2525
private const uint minPointerArraySize = 64;
2626
private const uint pointerArraySizeMultiple = 64;
2727

28-
private readonly SocketConfig _configuration;
29-
28+
private readonly SocketConfig _configuration;
29+
3030
internal static uint CalculateMultiple(uint minSize, uint multiple)
3131
{
3232
uint remainder = minSize % multiple;
@@ -50,7 +50,7 @@ internal MemoryManager(SocketConfig config)
5050

5151
internal object[] AllocPointers(uint size)
5252
{
53-
uint allocSize = Math.Max(minPointerArraySize, CalculateMultiple(size, pointerArraySizeMultiple));
53+
uint allocSize = Math.Max(minPointerArraySize, CalculateMultiple(size, pointerArraySizeMultiple));
5454

5555
if (!_configuration.PoolPointerArrays)
5656
{
@@ -117,7 +117,7 @@ internal HeapMemory AllocHeapMemory(uint size)
117117
_hasWarnedAboutHeapMemoryLeaks = true;
118118
}
119119

120-
memory = new HeapMemory(allocSize);
120+
memory = new HeapMemory(allocSize);
121121
}
122122

123123
memory.EnsureSize(allocSize);
@@ -126,6 +126,11 @@ internal HeapMemory AllocHeapMemory(uint size)
126126
memory.VirtualCount = size;
127127
memory.VirtualOffset = 0;
128128

129+
#if DEBUG
130+
// The allocation stacktrace allows us to see where the alloc occured that caused the leak
131+
memory.allocStacktrace = Environment.StackTrace;
132+
#endif
133+
129134
return memory;
130135
}
131136

0 commit comments

Comments
 (0)