我正在阅读如何查找与Win32应用程序上的C++ new
运算符和标准CRT malloc
函数的内存泄漏。调试Win32 API应用程序内存泄漏
我添加了一些与Windows套接字一起工作的东西,我想包括crtdbg.h
只有在调试模式下运行,以及其他一些定义。所以我结束了一起拼凑到这一点我stdafx.cpp
作为我的标准的预编译的头:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
// Windows Sockets
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
// If running in debug mode we need to use this library to check for memory leaks, output will be in DEBGUG -> WINDOWS -> OUTPUT
// http://msdn.microsoft.com/en-us/library/x98tx3cf.aspx
#ifdef _DEBUG
//http://stackoverflow.com/questions/8718758/using-crtdumpmemoryleaks-to-display-data-to-console
#ifndef DBG_NEW
#define DBG_NEW new (_NORMAL_BLOCK , __FILE__ , __LINE__)
#define new DBG_NEW
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#endif // _DEBUG
我也打算把这些线只是我计划的退出点之前:
#ifdef _DEBUG
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
_CrtDumpMemoryLeaks();
#endif
不管怎么说,它似乎编译,但我得到一些奇怪的警告:
警告C4005: '_malloca':宏的重新定义
从crtdbg.h
。这是由于stdafx.h
中标题的顺序错误造成的,或者这是正常的吗?另外,我在正确的轨道上检测Win32应用程序中的内存泄漏,还是在Visual Studio中应该使用其他内容?
也许你应该在包含第一个头文件之前(或者至少在Windows.h之前)定义'_CRTDBG_MAP_ALLOC'。 – Medinoc
您可能感兴趣的[VLD](http://vld.codeplex.com/)(视觉检漏仪) –