内存泄露输出

vc程序在调试模式下,new分配的内存通过一个结构体CrtMemBlockHeader来形成双链表

typedef struct  CrtMemBlockHeader{	
	struct CrtMemBlockHeader *pBlockHeaderNext;		// Pointer to the block allocated just before this one:
	struct CrtMemBlockHeader *pBlockHeaderPrev;		// Pointer to the block allocated just after this one:
	char *szFileName;				// File name
	int nLine;						// Line number
	size_t nDataSize;				// Size of user block
	int nBlockUse;					// Type of block
	long lRequest;					// Allocation number
	unsigned char gap[4];			// nNoMansLandSize	// Buffer just before (lower than) the user's memory:
} CrtMemBlockHeader;
因而可以写一个在程序退出的时候输出没有释放的内存,即遍历该双链表,将代码行和文件名称打印出来

int* p = new  int;
CrtMemBlockHeader* phead = (CrtMemBlockHeader*)p;
phead--;
if(phead && phead->pBlockHeaderNext){
	phead = phead->pBlockHeaderNext;
	while (phead)
	{
		void* ptr = (void*)(phead+1);
		if(phead->nLine!=0)
		{
			DBGVIEW("dataSize:%6d line:%4d File:%s",
			phead->nDataSize,phead->nLine,phead->szFileName);
		}
		phead = phead->pBlockHeaderNext;
	}
	break;
}else{
	DBGVIEW("invalid header_ %x",phead);
}
delete p;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值