 ___                __    _______ __          __
|   |  .-----.---.-.  |--.   _   |__|-----.--|  |-----.----.
|.  |  |  -__|  _  |    <|.  1___|  |     |  _  |  -__|   _|
|.  |__|_____|___._|__|__|.  __) |__|__|__|_____|_____|__|
|:  1   |                |:  |
|::.. . |                |::.|
`-------'                `---'

another bug-ridden module by RabidCow

Much thanks to grd, carluchi, and John Robbins, who wrote or passed on the most functional parts.


This module attempts to track GDI resources allocated by modules.  Load it before the module you want to test and unload it afterwards.  At this point it does not track bitmaps loaded with LoadLSBitmap.


Usage:
!reloadmodule $Path$leakfinder.dll
!reloadmodule $ModuleToTest$
!dostuff
!unloadmodule $ModuleToTest$
!unloadmodule $Path$leakfinder.dll

(you can test more than one module at a time)


A log file will be written to $LiteStepDir$logs\<modulename>.lf.txt, which will list each allocation and deallocation of GDI resources, and a list of resources that were not freed.

Example bit from systray2.ls.txt:

+ @01E25A67: REGION 71041481
- @01E25A67: REGION 71041481
+ @01E233AC: MEMDC 3B011551 (bm:0185000F,br:01900010,f:018A0021,p:01B00017)
+ @01E233BD: BITMAP 1F05154F (24x24)
+ @01E23596: MEMDC 170114CD (bm:0185000F,br:01900010,f:018A0021,p:01B00017)
- @01E23596: MEMDC 170114CD (bm:00000000,br:00000000,f:00000000,p:00000000)
- @01E233AC: MEMDC 3B011551 (bm:00000000,br:00000000,f:00000000,p:00000000)
- @01E233BD: BITMAP 1F05154F (24x24)

As you can see, some information about each resource is listed alongside when possible.

The first char indicates whether the resource is being allocated or freed.
The address after the @ is the location where the resource was allocated.  The idea is that you can hopefully look up this address in a debugger and find where it is in the source code.
After this is the resource type (I'm not entirely confident that it's correct), the handle, and then any extra data available.

Some fonts being allocated in popup2.lf.txt:

+ @01DF4F41: FONT 040A1639 (Arial Unicode MS 14)
+ @01DF4FA4: FONT 060A1640 (Verdana 14)
+ @01DF5007: FONT 0F0A1636 (Verdana 14)

and a leak, also from popup2:

REGION 1204165A created at 01DF944A never deleted.
REGION 17041635 created at 01DF944A never deleted.
REGION 4804165A created at 01DF944A never deleted.

Notice that these regions were all created in the same place in the code.  Looking in a debugger (or disassembler), we can see that address 1000944A (high word can be different) immediately follows a call to CreateRectRgn:
  1000943C     push 0
  1000943E     push 0
  10009440     push 0
  10009442     push 0
  10009444     call ds:CreateRectRgn
->1000944A     mov ebx, eax
Unfortunately I'm not using a debug build of this module, so I can't see what part of the source this corresponds to.


----

How does it work?  Well, if you're familiar with the way grdMagick and lsTransparent work, LeakFinder uses the same technique (the same code, in fact) but on a much larger scale.  Where grdMagick replaced BeginPaint and EndPaint, LeakFinder replaces nearly all of the API calls to create and destroy resources.  It then keeps track of these, storing a little information attached to each handle (such as the module handle and address where it was allocated) and reports anything left in its list when it exists.  (or tries to exit, it might crash instead)

