Refactor to use MHP_ENABLE_SAFETY_CHECKS, MHP_ENABLE_STACKTRACE, and MHP_ENABLE_MIMALLOC for feature toggling. Remove FreeList allocator in JobSchedular and debug-layer code, simplifying memory management. Improve memory leak detection and reporting, update memory allocation API, and guard all safety/debug features with new defines. Update csproj files, README, and code samples to match new API and toggles. Fix and improve collection types for correct behavior with and without safety checks. The codebase is now more modular and easier to configure for different build environments. BREAKING CHANGE: Old defines (ENABLE_SAFETY_CHECKS, ENABLE_DEBUG_LAYER, ENABLE_MIMALLOC) are replaced with MHP_* equivalents. FreeList allocator and related debug features are removed. Some APIs and behaviors have changed for safety/debug configuration.
63 lines
1.9 KiB
C#
63 lines
1.9 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 string _message;
|
|
|
|
public override string Message => _message;
|
|
|
|
public MemoryLeakException(IEnumerable<AllocationInfo> infos)
|
|
{
|
|
var stringBuilder = new StringBuilder();
|
|
stringBuilder.AppendLine($"Found {infos.Count()} memory lakes!");
|
|
|
|
#if MHP_ENABLE_STACKTRACE
|
|
stringBuilder.AppendLine();
|
|
|
|
foreach (var info in infos)
|
|
{
|
|
GetMessage(stringBuilder, info.StackTrace);
|
|
}
|
|
#else
|
|
stringBuilder.AppendLine("No stack trace information available. Please enable MHP_ENABLE_STACKTRACE for detailed leak information.");
|
|
#endif
|
|
|
|
_message = stringBuilder.ToString();
|
|
}
|
|
|
|
public MemoryLeakException(string message)
|
|
{
|
|
_message = message;
|
|
}
|
|
|
|
private static void GetMessage(StringBuilder stringBuilder, StackTrace? stackTrace)
|
|
{
|
|
if (stackTrace == null)
|
|
{
|
|
stringBuilder.AppendLine("No stack trace available.");
|
|
return;
|
|
}
|
|
|
|
stringBuilder.AppendLine("Memory leak detected at: ");
|
|
|
|
for (var i = 0; i < stackTrace.FrameCount; i++)
|
|
{
|
|
var frame = stackTrace.GetFrame(i);
|
|
var fileName = frame?.GetFileName();
|
|
|
|
if (frame != null)
|
|
{
|
|
var methodInfo = DiagnosticMethodInfo.Create(frame);
|
|
stringBuilder.AppendLine($"File: {fileName}, Type: {methodInfo?.DeclaringTypeName}, Method: {methodInfo?.Name}, Line: {frame.GetFileLineNumber()}");
|
|
}
|
|
}
|
|
}
|
|
} |