Click here to Skip to main content
13,795,978 members
Click here to Skip to main content
Add your own
alternative version


51 bookmarked
Posted 10 Sep 2003

GDI+ and MFC memory leak detection

, 10 Sep 2003
Rate this:
Please Sign up or sign in to vote.
GDI+ and MFC memory leak detection


When I started to use GDI+ with MFC in VC++ 6.0 I ran into some annoying problems:

  1. Couldn't compile my GDI+ code with MFC without removing the
  2. Couldn't compile GDI+ code with STL without tweaking my code
  3. Couldn't detect memory leaks

Here is the solution!

How to use

Include GdiplusH.h in stdafx.h:

<PRE lang=c++>// GDI+ helper file #include "GdiplusH.h"


  • GDI+ will be initialized when program starts
  • You can use
    functions to detect memory leaks, set an allocation breakpoint, save and compare memory states, etc.
  • Memory leak information will be dumped to output window (MSVC IDE)
  • No more compilation problem with DEBUG_NEW
  • No more compilation problem with STL

Memory leak detection


GDI+ is using

and GdipFree for allocating memory for GDI+ objects. Probably they have their own memory allocation lists in gdiplus.dll, but unfortunately there is no exported API to get any memory leak information. But GDI+ objects can be located on stack (for example: creating a local variable in a function), so there is no special initialization for these memory regions, so we do not have to use GdipAlloc or GdipFree. If we call the CRT debug version of memory allocation and deletion functions instead of GdipAlloc/Free, we can detect memory leaks very easily using the well known _CrtXXX functions.


Miscellaneous defines


    GDI+ won't be initialized at program startup. You have to create a GdiPlus::GdiPlusInitialize variable to initialize GDI+ (GDI+ will be uninitialized, when destructor is called for this variable).
    GdipAlloc and GdipFree is used for memory operations. In this case _Crt functions cannot be used to detect memory leaks
    Gdiplus namespace won't be defined as a used namespace. In this case you have to use Gdiplus:: prefix.



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Zoltan Csizmadia
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionGdiPlusH.h Pin
thelvaci22-May-17 3:57
memberthelvaci22-May-17 3:57 
QuestionTool for searh memory and GDI leaks. Pin
Mast Avalons10-Jan-12 9:02
memberMast Avalons10-Jan-12 9:02 
AnswerRe: Tool for searh memory and GDI leaks. Pin
SuperMegaCoder30-Mar-12 11:38
memberSuperMegaCoder30-Mar-12 11:38 
GeneralRe: Tool for searh memory and GDI leaks. Pin
JohnDepth6-Jun-12 19:08
memberJohnDepth6-Jun-12 19:08 
GeneralRe: Tool for searh memory and GDI leaks. Pin
Mast Avalons7-Jun-12 12:18
memberMast Avalons7-Jun-12 12:18 
GeneralAnother solution (risky) Pin
__stas__14-Jul-10 1:53
member__stas__14-Jul-10 1:53 
GeneralThanks alot! Pin
Lars [Large] Werner27-Aug-08 21:03
professionalLars [Large] Werner27-Aug-08 21:03 
GeneralA problem Pin
qqz25-Jan-08 6:33
memberqqz25-Jan-08 6:33 
GeneralThak you! Pin
skitten9-Mar-07 12:00
memberskitten9-Mar-07 12:00 
I'm just starting with GDI+. I've tried to make it work with my MFC application. Some GDI+ methods were working, some not until I used your file. It solved all the problems I got so far. Thanks a lot.

GeneralGood Pin
leibe15-Sep-05 17:34
memberleibe15-Sep-05 17:34 
GeneralLink errors in release mode Pin
Salvador Dali20-Apr-04 23:58
memberSalvador Dali20-Apr-04 23:58 
GeneralUse __declspec(selectany) Pin
__stas__14-Jul-10 6:32
member__stas__14-Jul-10 6:32 
GeneralUseful stuff Pin
Nish Nishant17-Sep-03 21:51
sitebuilderNish Nishant17-Sep-03 21:51 
GeneralGood Job! Pin
NormDroid11-Sep-03 21:47
professionalNormDroid11-Sep-03 21:47 

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
Web06 | 2.8.181207.3 | Last Updated 11 Sep 2003
Article Copyright 2003 by Zoltan Csizmadia
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid