Find your memory leaks

Share your advanced PureBasic knowledge/code with the community.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Find your memory leaks

Post by Trond »

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. :D

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
kinglestat
Enthusiast
Enthusiast
Posts: 746
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Re: Find your memory leaks

Post by kinglestat »

Now thats a nice tool
But tings like strings which are allocated internally with PB are not tracked this way I assume?
I may not help with your coding
Just ask about mental issues!

http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Find your memory leaks

Post by Trond »

No, they aren't.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Find your memory leaks

Post by Little John »

Simple, clean, and very useful. Thanks for sharing, Trond!
Why didn't I think of something like that myself? :-)

Regards, Little John
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Find your memory leaks

Post by rsts »

Very nice.

Will probably save me hours.

Many thanks Trond :D

cheers
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Find your memory leaks

Post by idle »

could save a lot of time, thanks
Post Reply