我正在阅读如何使用C++ new操作符和Win32应用程序上的标准CRT malloc函数查找内存泄漏。
我添加了一些用于使用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中使用其他东西?
发布于 2014-03-07 17:10:52
从crtdbg.h。这是由于stdafx.h中的标头排序错误,还是这是正常的?
也许这篇文章可以帮你。请注意本部:
为了使CRT函数正确工作,#语句必须遵循这里显示的顺序。
您可以使用http://vld.codeplex.com/查找与缺少delete for new操作符有关的泄漏,其他人已经提到了这一点。我在这方面有很好的经验,但我通常会反复检查代码中是否每个new都有相应的delete调用。
另外,我是否在正确的轨道上检测Win32应用程序中的内存泄漏,还是我应该在Visual中使用其他东西?
另一种类型的内存泄漏是GDI leaks。当您不将device context还原到原始状态或忘记删除GDI object时,通常会发生这种情况。
对于这些泄漏,我使用http://www.nirsoft.net/utils/gdi_handles.html。它的用法是很好的描述,但如果你有任何问题,请留下评论。
您可能会发现这篇文章很有用。
当然,有很多很好的工具,这只是我的建议。
希望这能帮上忙。
诚挚的问候。
https://stackoverflow.com/questions/22229518
复制相似问题