Files
Misaki.HighPerformance/Misaki.HighPerformance.LowLevel/MemoryLeakException.cs
Misaki fbe72e33f7 Refactor AllocationManager and enhance debug tracking
Refactored `AllocationManager` to introduce intrusive allocation tracking with `AllocationHeader` structs for debug mode. Added lightweight allocation counters for non-debug mode. Enhanced memory leak detection with detailed stack traces and `MemoryLeakException`.

Simplified `AllocationInfo` by removing the `Allocator` property. Updated `AllocationOption` enum to remove `UnTracked` and clarified documentation.

Improved unsafe collections (`UnsafeArray`, `UnsafeStack`, etc.) with strongly-typed enumerators and better compatibility with `IEnumerable<T>`. Enhanced `UnsafeStack` with a dedicated `Enumerator` struct and consistent constructor parameters.

Refactored `MemoryLeakException` to support detailed allocation info and improved stack trace formatting. Simplified `MemoryUtility` by removing redundant null checks.

Added unit tests for `AllocationManager`, `UnsafeArray`, and `UnsafeStack` to validate memory management and functionality. Updated `Program.cs` with new examples.

Cleaned up namespaces, removed redundant `using` directives, and improved XML documentation. Applied `MethodImplOptions.AggressiveInlining` to performance-critical methods.
2025-11-06 01:28:43 +09:00

68 lines
1.8 KiB
C#

using Misaki.HighPerformance.LowLevel.Buffer;
using System.Diagnostics;
using System.Text;
namespace Misaki.HighPerformance.LowLevel;
/// <summary>
/// An exception that is thrown when a memory leak is detected.
/// </summary>
/// <param name="Infos">An array of AllocationInfo containing details about the memory leaks.</param>
public class MemoryLeakException : Exception
{
private readonly IEnumerable<AllocationInfo>? _infos;
private readonly string _message = string.Empty;
public MemoryLeakException(IEnumerable<AllocationInfo> infos)
{
_infos = infos;
}
public MemoryLeakException(string message)
{
_message = message;
}
private static string GetMessage(StackTrace? stackTrace)
{
if (stackTrace == null)
{
return "No stack trace available.";
}
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Memory leak detected at: ");
for (var i = 0; i < stackTrace.FrameCount; i++)
{
var frame = stackTrace.GetFrame(i);
if (frame != null)
{
stringBuilder.AppendLine($"File: {frame.GetFileName()}, Method: {DiagnosticMethodInfo.Create(frame)?.Name}, Line: {frame.GetFileLineNumber()}");
}
}
return stringBuilder.ToString();
}
public override string Message
{
get
{
if (_infos == null)
{
return _message;
}
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"Found {_infos.Count()} memory lakes!");
foreach (var info in _infos)
{
stringBuilder.AppendLine(GetMessage(info.StackTrace));
}
return stringBuilder.ToString();
}
}
}