C++ operator new[]和Debug Heap

如果在VS2005下面想用CRT Debug Heap来调试Memory Leak,最后可以用_CrtDumpMemoryLeaks 把所有的leak打印出来。尝试下面的代码,会怎样:

#include "stdafx.h"

#ifdef _DEBUG

#define _CRTDBG_MAP_ALLOC

#include<stdlib.h>

#include<crtdbg.h>

#endif

#define MY_NEW[s] new(s,_NORMAL_BLOCK,
__FILE__, __LINE__)

#if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)

#define new MY_NEW

#endif

int _tmain(int argc, _TCHAR* argv[])

{

char
*p=new char[10];

void
*p2=malloc(10);

#ifdef
_DEBUG

_CrtDumpMemoryLeaks();

#endif

return
0;

}

运行后会看到:

Detected memory leaks!

Dumping objects ->

c:\documents and settings\lixiong\my
documents\mycode\detectleak\detectleak\detectleak.cpp(17) : {87} normal block
at 0x003A8130, 10
bytes long.

Data: < > CD CD CD CD CD CD CD CD CD CD

c:\program files\microsoft visual studio 8\vc\include\crtdbg.h(1150) :
{86} normal block at 0x003A3240,
10 bytes long.

Data: < > CD CD CD CD CD CD CD CD CD CD

Object dump complete.

The
program ‘[808] DetectLeak.exe: Native’ has exited with code 0 (0x0).

注意这里打印出的第一个leak,出现在detectleak.cpp17行,对应的是malloc语句,没问题

可是第二个leak,出现在crtdbg.h1150行,而不是new char[10]那里,怎么回事?如果不能定位到正确的源代码,还有什么用呢?

为了搞清楚这个问题,可以看看mallocdebug heap下的的定义:

#ifdef _CRTDBG_MAP_ALLOC

#define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK,
__FILE__, __LINE__)

注意看,这里用了__FILE__,
__LINE__
两个预处理器Directive:

The #line Directive
http://msdn2.microsoft.com/en-us/library/b5w2czay.aspx

由于与处理器自动把文件名和行号传递给了_malloc_dbg函数,最后的output窗口才可以打印出源代码行

那好,看看debug heapnew的定义。由于newC++的关键字,而且是一个操作符,所以debug heap下定义为:

inline __bcount(_Size) void*
__CRTDECL operator new[](size_t
_Size)

{ return
::operator new[](_Size,
_NORMAL_BLOCK, __FILE__, __LINE__); }

注意这里没有用#define,而是inline。同时该定义是在crtdbg.h文件中的。所以最后得到的是文件名是crtdbg.h。你可能有如下疑问:

1.
为什么不用#define而要用inline呢,改成#define可以吗?
你试试看吧,看能不能该成#define。由于这里有一个中括号,麻烦来了吧。谁让你是C++呢?bs一下C++

2.
为什么预处理器看到inline函数,不把inline后的行号和文件名字作为解释呢?
这我就不确定了啦。不过根据C++标准,标示为inline函数不是一定就要inline的,人家标准就定义得模棱两可,你何必强求预处理器呢?

所以,解决方法就是,所有的内存分配,就用:

#ifdef _CRTDBG_MAP_ALLOC

char
*test=(char*)::operator
new[](20, _NORMAL_BLOCK, __FILE__, __LINE__);

#else

char
*test=new char[20];

#endif

你有其它好方法吗?

http://eparg.spaces.live.com/blog/cns!59BFC22C0E7E1A76!1490.entry


此条目发表在Uncategorized分类目录。将固定链接加入收藏夹。

10 Responses to C++ operator new[]和Debug Heap

  1. Unknown说道:

    wow gold wow gold replica rolex powerleveling powerleveling powerleveling powerleveling power leveling power leveling powerleveling powerleveling power leveling power leveling power leveling power leveling power leveling power leveling power leveling power leveling rolex wow gold wow gold Warcraft Gold Warcraft Gold wow power leveling wow power leveling wow gold@@@@ World of Warcraft Gold wow power leveling wow power leveling wow power leveling wow power leveling wow power leveling wow power leveling wow power leveling wow power leveling wow power leveling wow powerleveling wow powerleveling wow powerleveling rolex replica wow powerleveling wow powerleveling wow powerleveling wow powerleveling wow powerleveling wow powerleveling wow powerleveling wow powerlevelinghfytyy6r

  2. Unknown说道:


    信是個富有挑戰的工作,忙碌、刺激、激情。最后的結局總是有人哭,有人笑。在徵信社工作的幾年時間里,我感悟到很多生活的無奈與殘酷。閑暇時我去中古车市
    场,感覺工作太緊張,放松一下而已,不是純粹的為了淘二手车。私家偵探也就是徵信工作人員似乎是個很酷的詞,但是如果誰需要私家偵探來幫忙的話,他(她)
    的婚姻也許已經亮起了紅燈。每天陪著星際客戶喝著咖啡,似乎是一件很爽的事,可是她們的眼淚比咖啡要苦的多。常常聽他們說嘈雜的燈光音響麻痹著他們的神
    經,讓他們過著醉生夢死的生活。其實,誰不渴望幸福美滿的愛情。沒有團體制服不了那些玩弄生活、玩弄感情的人,于是,徵信社應運而生。這不知道是人們的福
    音還是不幸,希望天下的每一個人都歡樂著、幸福著。坐月子是女人一生的大事,女人可以为了襁褓中的婴儿放弃一直引用的红酒而吃月子餐,这时候的女人是幸福
    而伟大的。有人歡喜有人優,有人狂笑有人大哭,我不想看到女人太多的眼淚,辭職后,我做了一位安靜、平和的室內設計師。虽然上下班没有了车接车送,但我租
    车怡然自得。偶爾和Fake
    Rolex Watch, Breitling
    Replica Watch, Best
    replica watches, Omega
    Replica Watches ecosway的朋友小聚,很愜意

  3. Unknown说道:

    Amberdigital Branch,Southern Stars Enterprises Co is specializing in the development and manufacturing of advertising displays, advertising player and LCD displays. Established in 1996, we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

    amberdigital Contact Us
    Southern Stars Enterprises Co (Hong Kong Office)
    Add:3 Fl, No.2, Lane 2, Kam Tsin Tsuen, Sheung Shui, Hong Kong
    Tel:+852 2681 4099
    Fax:+852 2681 4586
    Southern Stars Enterprises Co (Shenzhen Office)
    Add:DE, 16/F, Building 2, Nanguo Tower, Sungang Road, Shenzhen, China
    Tel:+86 755 2592 9100
    Fax:+86 755 2592 7171
    E-mail:sstar@netvigator.com
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[c

  4. Unknown说道:

    FXレーシック 保険レーシック 失敗コスメ ランキング東京 家庭教師大阪 家庭教師受験 家庭教師無料 レンタルサーバーレンタルサーバー 比較生命保険 比較生命保険 見直しフェイシャル エステエステティシャンネットオークションオークション 相場サプリメント 通販健康 サプリメント

留下评论