5,692,513 members and growing! (17,798 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Intermediate

GDI+ and MFC memory leak detection

By Zoltan Csizmadia

GDI+ and MFC memory leak detection
VC6, C++Windows, Win2K, WinXP, Win2003, Visual Studio, MFC, GDI+, Dev

Posted: 10 Sep 2003
Updated: 10 Sep 2003
Views: 45,206
Bookmarked: 27 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
13 votes for this Article.
Popularity: 4.97 Rating: 4.46 out of 5
3 votes, 23.1%
1
0 votes, 0.0%
2
1 vote, 7.7%
3
1 vote, 7.7%
4
8 votes, 61.5%
5

Introduction

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 DEBUG_NEW macros
  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:

// GDI+ helper file


#include "GdiplusH.h"

Features

  • GDI+ will be initialized when program starts
  • You can use _CrtXXX 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 GdipAlloc 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

 

  • GDIPLUS_NO_AUTO_INIT - 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).
  •  GDIPLUS_USE_GDIPLUS_MEM - GdipAlloc and GdipFree is used for memory operations. In this case _Crt functions cannot be used to detect memory leaks
  •  GDIPLUS_NO_AUTO_NAMESPACE - Gdiplus namespace won't be defined as a used namespace. In this case you have to use Gdiplus:: prefix.

Acknowledgements

License

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



Occupation: Web Developer
Location: United States United States

Other popular GDI+ articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralThanks alot!memberLars [Large] Werner21:03 27 Aug '08  
GeneralA problemmemberqqz6:33 25 Jan '08  
GeneralThak you!memberskitten12:00 9 Mar '07  
GeneralGoodmemberleibe17:34 15 Sep '05  
GeneralLink errors in release modememberSalvador Dali23:58 20 Apr '04  
GeneralUseful stuffeditorNishant S21:51 17 Sep '03  
GeneralGood Job!memberNormski21:47 11 Sep '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Sep 2003
Editor: Nishant Sivakumar
Copyright 2003 by Zoltan Csizmadia
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project