您的位置:首页 > 其它

GDI+ 和MFC内存泄露检测

2014-06-06 15:15 423 查看


原文地址:http://www.codeproject.com/KB/GDI-plus/gdiplush.aspx

绪论

当我开始使用VC++6.0进行GDI+和MFC编程后,我遇到了一些麻烦的问题:

不移除DEBUG_NEW宏就不能编译有MFC的GDI代码
不少许修改代码就不能编译有STL的GDI+代码
不能探测到内存泄露

以下是解决方案

怎样使用

在stdafx.h文件中包含GdiplusH.h
//GDI+helperfile


#include"GdiplusH.h"

特征

程序启动时候将初始化GDI+
你可以使用函数
_CrtXXX
来检查内存泄露,设置一个断点,保存并且比较内存状态,等等
内存泄露信息将在(VC)窗口输出
没有
DEBUG_NEW
方面的编译问题
没有STL方面的编译问题

内存泄露检查

GDI+使用
GdipAlloc
GdipFree
为GDI+对象分配内存。大概它们在gdiplus.dll有自己的内存分配表,但不幸的是,那没有输出API去得到内存泄露信息。GDI+对象能在栈上定位(例:在函数中创建一个局部变量),这些内存区域没有设置初始值,所以我们可以不使用
GdipAlloc
或者
GdipFree
函数。如果我们调用CRT调试内存分配和释放函数的版本代替
GdipAlloc
/
Free
我们使用有名的
_CrtXXX
函数可以十分容易地侦测到内存的泄露。

多种定义

GDIPLUS_NO_AUTO_INIT-
GDI+在程序启动时候不会被初始化。你可以创建一个
GdiPlus::GdiPlusInitialize
变量来初始化GDI+(GDI+将在析构函数调用这个变量时候被初始化)。
GDIPLUS_USE_GDIPLUS_MEM-GdipAlloc
GdipFree
用于内存操作。在这种情况下_Crt函数不能用于检查内存的泄露。
GDIPLUS_NO_AUTO_NAMESPACE-
Gdiplus名字空间将不作为一个使用的命名空间定义。所以在这种情况下你必须得使用
Gdiplus::
前缀。

致谢

http://www.codeproject.com/vcpp/gdiplus/vc6gdiplusmacro.asp
http://www.codeproject.com/vcpp/gdiplus/imageexgdi.asp?df=100&forumid=3203&select=465515#xx465515xx



后面附带原文,望大家予以斧正!在下先谢谢了!!




GDI+andMFCmemoryleakdetection

Introduction

WhenIstartedtouseGDI+withMFCinVC++6.0Iranintosomeannoyingproblems:

Couldn'tcompilemyGDI+codewithMFCwithoutremovingtheDEBUG_NEWmacros
Couldn'tcompileGDI+codewithSTLwithouttweakingmycode
Couldn'tdetectmemoryleaks

Hereisthesolution!
Hereisthesolution!

Howtouse

IncludeGdiplusH.hinstdafx.h:
CollapseCopyCode
//GDI+helperfile


#include"GdiplusH.h"

Features

GDI+willbeinitializedwhenprogramstarts
Youcanuse
_CrtXXX
functionstodetectmemoryleaks,setanallocationbreakpoint,saveandcomparememorystates,etc.
Memoryleakinformationwillbedumpedtooutputwindow(MSVCIDE)
Nomorecompilationproblemwith
DEBUG_NEW

NomorecompilationproblemwithSTL

Memoryleakdetection

GDI+isusing
GdipAlloc
and
GdipFree
for
allocatingmemoryforGDI+objects.Probablytheyhavetheirownmemoryallocationlistsingdiplus.dll,butunfortunatelythereisnoexportedAPItogetanymemoryleakinformation.ButGDI+objectscanbelocatedonstack(forexample:creatingalocal
variableinafunction),sothereisnospecialinitializationforthesememoryregions,sowedonothavetouse
GdipAlloc
or
GdipFree
.
IfwecalltheCRTdebugversionofmemoryallocationanddeletionfunctionsinsteadof
GdipAlloc
/
Free
,
wecandetectmemoryleaksveryeasilyusingthewellknown
_CrtXXX
functions.

Miscellaneousdefines

GDIPLUS_NO_AUTO_INIT-
GDI+won'tbeinitializedatprogramstartup.Youhavetocreatea
GdiPlus::GdiPlusInitialize
variabletoinitialize
GDI+(GDI+willbeuninitialized,whendestructoriscalledforthisvariable).
GDIPLUS_USE_GDIPLUS_MEM-GdipAlloc
and
GdipFree
isusedformemoryoperations.Inthiscase_Crtfunctionscannotbeusedto
detectmemoryleaks
GDIPLUS_NO_AUTO_NAMESPACE-
Gdiplusnamespacewon'tbedefinedasausednamespace.Inthiscaseyouhavetouse
Gdiplus::
prefix.

Acknowledgements

http://www.codeproject.com/vcpp/gdiplus/vc6gdiplusmacro.asp
http://www.codeproject.com/vcpp/gdiplus/imageexgdi.asp?df=100&forumid=3203&select=465515#xx465515xx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: