Click here to Skip to main content
Click here to Skip to main content

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

, 5 May 2014 CPOL
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.

Comments and Discussions

QuestionIs your app supports for Mixed Platform builds. Pinmembercvmohan25-Sep-14 23:02 
QuestionVery Good! PinmemberDuduscs1-Jul-13 10:24 
QuestionMy mind PinmemberMast Avalons24-Feb-12 9:37 
QuestionVery good Pinmemberq1q2q3q4q5q6ln31-Jul-11 18:24 
GeneralReally good idea, but need some explanations [modified] PinmemberLaurent Regnier9-Nov-09 4:42 
GeneralRe: Really good idea, but need some explanations Pinmembercdp_anhui_china13-Nov-09 2:38 
QuestionReally great idea, but i can't get it running - could you translate it into english? PinmemberRedFraggle30-Oct-08 2:35 
at first, thanks for your contribution here.
But i have trouble to get it working. Main problem might be , that i do not understand the error messages nor the GUI ... an english translation would be really great Smile | :)
QuestionAny chance of a version with English messages and comments? Pinmember-273C10-Apr-08 10:56 
GeneralClever, but..... Pinmemberjuggler29-Aug-07 4:43 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 5 May 2014
Article Copyright 2007 by cdp_anhui_china
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid