Reputation: 3161
I was reading up on how to go about finding memory leaks with the C++ new
operator and the standard CRT malloc
function on a Win32 application.
I added some stuff for working with windows sockets and I wanted to include crtdbg.h
only if running in debug mode, along with some other defines. So I ended up piecing this together into my stdafx.cpp
as my standard pre-compiled header:
// 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
I also intended to put these lines just before the exit point of my program:
#ifdef _DEBUG
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_DEBUG );
_CrtDumpMemoryLeaks();
#endif
Anyways, it appears to compile but I get some strange warning:
warning C4005: '_malloca': macro redefinition
From the crtdbg.h
. Is this due to the wrong ordering of the headers in the stdafx.h
, or is this normal? Also, am I on the right track for detecting memory leaks in Win32 apps or is there something else I should be using in Visual Studio?
Upvotes: 2
Views: 2096
Reputation: 3031
From the crtdbg.h. Is this due to the wrong ordering of the headers in the stdafx.h, or is this normal?
Perhaps this article can help you. Notice this part :
For the CRT functions to work correctly, the #include statements must follow the order shown here.
You could use Visual Leak Detector for finding leaks involved with missing delete
for new
operator as others mentioned. I had good experience with it, but I usually tend to triple-check if in my code every new
has corresponding delete
call.
Also, am I on the right track for detecting memory leaks in Win32 apps or is there something else I should be using in Visual Studio?
The other type of memory leaks are GDI leaks
. These usually happen when you do not restore the device context
into original state or you forget to delete the GDI object
.
For those leaks I use GDIView. Its usage is well described but if you have any questions leave a comment.
You might find this article useful.
Of course, there are lots of good tools out there, this is just my suggestion.
Hopefully this helps.
Best regards.
Upvotes: 2