Click here to Skip to main content
13,802,058 members
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 29 Aug 2007
Licenced CPOL

GDI Leak Detector - A special debugger to detect and locate GDI leaks

, 5 May 2014
Rate this:
Please Sign up or sign in to vote.
A special debugger to detect and locate GDI leaks, with source code.


This is an easy-to-use, powerful, and efficient tool to detect and locate GDI leaks. It can be a good tool for use with Visual Studio.

Mechanism of the tool

The tool has two parts: GdiLeakDetector.exe and GdiSpy.dll.

GdiLeakDetector.exe is a special debugger. It will launch a process in debug mode and will inject GdiSpy.dll to the debuggee. GdiSpy.dll will intercept the GDI get/create call to log the call stack and intercept the release/delete call to remove the corresponding log. If there is any leak when the debuggee exits, there may be GDI leaks. I say 'may' because the spy DLL may not be the last DLL unloaded, and some DLLs may be unloaded after it and then release the GDI resources they hold. So in theory, the wrong leak report can occur. But I don't think it's a big problem because developers can easily ignore the wrong reports.


Thanks to StackWalker from CodeProject. I learned a lot from the code.

class StackWalker API

I have rewritten the class to meet my needs:

class CStackWalker
    CStackWalker(LPCTSTR symPath); 
    // skipFrameCount: don't get the last skipFrameCount callstack entries. 
    // maxFrameCount: get maxFrameCount callstack entries at most. 
    // if maxFrameCount == -1, 
    // get as many callstack entries as possible. 
    void GetCallStack(/*out*/vector<string>& callStacker, 
        int skipFrameCount, int maxFrameCount=10); 

Now you can reuse the class like this:

//some .cpp file 
CStackWalker g_StackWalker; 
void somefunc() 
    vector<string> callStacks; 
    g_StackWalker.GetCallstack(callStacks, 1); 
//other .cpp file 
extern CStackWalker g_StackWalker; 
void otherfunc() 
    vector<string> callStacks; 
    g_StackWalker.GetCallstack(callStacks, 1); 


  • Uploaded on 2007-08-29
  • Translated comments and resource from Chinese to English on 2009-11-13.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
China China
I am a student from Shanghai Tongji University.

You may also be interested in...

Comments and Discussions

Questioncrash when run other exe Pin
鹏 李28-Aug-18 0:05
member鹏 李28-Aug-18 0:05 
AnswerRe: crash when run other exe Pin
cdp_anhui_china22-Sep-18 1:00
membercdp_anhui_china22-Sep-18 1:00 
QuestionUnable to Debug, Exception Occurred Pin
Ayush Baunthiyal27-Oct-15 1:08
memberAyush Baunthiyal27-Oct-15 1:08 
AnswerRe: Unable to Debug, Exception Occurred Pin
cdp_anhui_china30-Nov-15 16:48
membercdp_anhui_china30-Nov-15 16:48 
QuestionIs your app supports for Mixed Platform builds. Pin
cvmohan25-Sep-14 23:02
membercvmohan25-Sep-14 23:02 
QuestionVery Good! Pin
Duduscs1-Jul-13 10:24
memberDuduscs1-Jul-13 10:24 
QuestionMy mind Pin
Mast Avalons24-Feb-12 9:37
memberMast Avalons24-Feb-12 9:37 
QuestionVery good Pin
q1q2q3q4q5q6ln31-Jul-11 18:24
memberq1q2q3q4q5q6ln31-Jul-11 18:24 
GeneralReally good idea, but need some explanations [modified] Pin
Laurent Regnier9-Nov-09 4:42
professionalLaurent Regnier9-Nov-09 4:42 
GeneralRe: Really good idea, but need some explanations Pin
cdp_anhui_china13-Nov-09 2:38
membercdp_anhui_china13-Nov-09 2:38 
QuestionReally great idea, but i can't get it running - could you translate it into english? Pin
RedFraggle30-Oct-08 2:35
memberRedFraggle30-Oct-08 2:35 
QuestionAny chance of a version with English messages and comments? PinPopular
-273C10-Apr-08 10:56
member-273C10-Apr-08 10:56 
GeneralClever, but..... PinPopular
juggler29-Aug-07 4:43
memberjuggler29-Aug-07 4:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.181215.1 | Last Updated 5 May 2014
Article Copyright 2007 by cdp_anhui_china
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid