Click here to Skip to main content
15,867,686 members
Articles / Multimedia / GDI
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
2.79/5 (20 votes)
5 May 2014CPOL1 min read 223.1K   1.5K   31   13
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.

C++
class StackWalker API

I have rewritten the class to meet my needs:

C++
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:

C++
//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)


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

Comments and Discussions

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