Find your memory leaks
Posted: Fri Sep 18, 2009 10:16 am
This code is a wrapper for PB's memory allocation commands. They keep track of all allocated memory, including on which line and in which file each allocation was made. Finding the source of memory leaks should be a breeze.
Now obviously, this code will probably slow down your code a lot, especially FreeMemorx(), so it is written within a compilerif. If you compiler without debugger, some macros makes sure your code works at full speed without change. On the other hand, if you need this code, it's a sign you don't actually use FreeMemory() a lot anyways.
To use the code, simply paste it into your program, and replace all the commands that ends with Memory() with Memorx(). I named the new functions like this so they'd show up before the native commands in the autocomplete list (x is before y). When you need to know if your program just "took a leak", open the variable viewer (in the debugger) and view all items in the linked list memories().
Now obviously, this code will probably slow down your code a lot, especially FreeMemorx(), so it is written within a compilerif. If you compiler without debugger, some macros makes sure your code works at full speed without change. On the other hand, if you need this code, it's a sign you don't actually use FreeMemory() a lot anyways.

To use the code, simply paste it into your program, and replace all the commands that ends with Memory() with Memorx(). I named the new functions like this so they'd show up before the native commands in the autocomplete list (x is before y). When you need to know if your program just "took a leak", open the variable viewer (in the debugger) and view all items in the linked list memories().
Code: Select all
CompilerIf #PB_Compiler_Debugger
Structure SAllocation
Size.i
File.s
Line.i
Pointer.i
EndStructure
Global NewList Memories.SAllocation()
Procedure _AllocateMemorx(Size, File.s, Line.i)
AddElement(Memories())
Memories()\Size = Size
Memories()\File = File
Memories()\Line = Line
Memories()\Pointer = AllocateMemory(Size)
ProcedureReturn Memories()\Pointer
EndProcedure
Macro AllocateMemorx(Size)
_AllocateMemorx(Size, #PB_Compiler_File, #PB_Compiler_Line)
EndMacro
Procedure FreeMemorx(Memory)
ForEach Memories()
If Memories()\Pointer = Memory
DeleteElement(Memories())
Break
EndIf
Next
FreeMemory(Memory)
EndProcedure
Macro ReAllocateMemorx(Memory, Size)
_ReAllocateMemorx(Memory, Size, #PB_Compiler_File, #PB_Compiler_Line)
EndMacro
Procedure _ReAllocateMemorx(Memory, Size, File.s, Line.i)
ForEach Memories()
If Memories()\Pointer = Memory
DeleteElement(Memories())
Break
EndIf
Next
AddElement(Memories())
Memories()\Size = Size
Memories()\File = File
Memories()\Line = Line
Memories()\Pointer = ReAllocateMemory(Memory, Size)
ProcedureReturn Memories()\Pointer
EndProcedure
CompilerElse
Macro AllocateMemorx(Size)
AllocateMemory(Size)
EndMacro
Macro ReAllocateMemorx(Memory, Size)
ReAllocateMemory(Memory, Size)
EndMacro
Macro FreeMemorx(Memory)
FreeMemory(Memory)
EndMacro
CompilerEndIf
;- EXAMPLE ---------------------
Pointer = AllocateMemorx(1234)
Pointer = AllocateMemorx(9163)
; Oups, we forgot to free 1234 bytes!
FreeMemorx(Pointer)
Pointer = AllocateMemorx(921)
ReAllocateMemorx(Pointer, 1000081)
; Oups, we forgot to assign the result to Pointer!
; Let's stop the program and open the variable viewer to see what we did
; View all items in the linked lists Memories()
CallDebugger