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

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

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

Share

About the Author

Zoltan Csizmadia
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
QuestionTool for searh memory and GDI leaks. PinmemberMast Avalons10-Jan-12 8:02 
AnswerRe: Tool for searh memory and GDI leaks. PinmemberSuperMegaCoder30-Mar-12 10:38 
GeneralRe: Tool for searh memory and GDI leaks. PinmemberJohnDepth6-Jun-12 18:08 
GeneralRe: Tool for searh memory and GDI leaks. PinmemberMast Avalons7-Jun-12 11:18 
GeneralAnother solution (risky) PinmemberStanislas14-Jul-10 0:53 
GeneralThanks alot! PinmemberLars [Large] Werner27-Aug-08 20:03 
This "fixed" the VC6.0 c++ error with the new-parameter and caused things to speed up (since everything doesn't need to be created & deleted all the time).
 
=====================
Lars [Large] Werner
lars@werner.no
http://lars.werner.no
Have you tried the ultimate tool for filling your CD/DVDs? http://lars.werner.no/sizeme/
=====================

GeneralA problem Pinmemberqqz25-Jan-08 5:33 
GeneralThak you! Pinmemberskitten9-Mar-07 11:00 
GeneralGood Pinmemberleibe15-Sep-05 16:34 
GeneralLink errors in release mode PinmemberSalvador Dali20-Apr-04 22:58 
GeneralUse __declspec(selectany) PinmemberStanislas14-Jul-10 5:32 
GeneralUseful stuff PineditorNishant S17-Sep-03 20:51 
GeneralGood Job! PinmemberNormski11-Sep-03 20:47 

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 | Mobile
Web01 | 2.8.141029.1 | Last Updated 11 Sep 2003
Article Copyright 2003 by Zoltan Csizmadia
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid