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.

Introduction

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.

Acknowledgement

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
{ 
    public: 
    CStackWalker(LPCTSTR symPath); 
    ~CStackWalker(); 
    // 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); 
    private: 
    ... 
}

Now you can reuse the class like this:

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

History

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

License

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

Share

About the Author

cdp_anhui_china
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 
QuestionAny chance of a version with English messages and comments? Pinmember-273C10-Apr-08 10:56 
Please provide a version with English resources. I am getting an error message and can't understand the symbols (they may be Chinese but are not displayed as such). Also, useful if the comments in the code were in English too.
 
Thank you.
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
Web01 | 2.8.1411023.1 | Last Updated 5 May 2014
Article Copyright 2007 by cdp_anhui_china
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid