|
-
April 3rd, 2012, 09:21 PM
#1
Detecting memory leaks using _CrtDumpMemoryLeaks
Hi,
This is my first time using the CRT library for detecting memory leaks, and I'm using Visual C++ 2003. As mentioned in this website (http://msdn.microsoft.com/en-us/library/x98tx3cf.aspx), I included the statements in my program
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
followed by another header file that contains all other header files like stdio.h, windows.h and structure/function declarations. I also have
_CrtDumpMemoryLeaks();
right before my function returns.
When I tried to build my program, I get the errors
error C2059: syntax error: 'constant'
error C2059: syntax error: 'string'
error C2733: second C linkage of overloaded function '_aligned_malloc_dbg' not allowed
for the function prototypes in malloc.h, e.g.
_CRTIMP void * __cdecl calloc(size_t, size_t);
_CRTIMP void * __cdecl free(void *);
etc.
How do I run the memory check debugger correctly?
Thank you.
Regards,
Rayne
-
April 4th, 2012, 03:48 AM
#2
Re: Detecting memory leaks using _CrtDumpMemoryLeaks
Just include <crtdbg.h> after all others.
Example
Code:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <string.h>
// other includes here...
#include <crtdbg.h> // keep this the last include!
int main()
{
char* p = (char*)calloc(10, 10);
strcpy(p, "Baba Safta");
_CrtDumpMemoryLeaks();
return 0;
}
Result
Code:
Detected memory leaks!
Dumping objects ->
C:\Projects\Test\Test.cpp(15) : {44} normal block at 0x00332A38, 100 bytes long.
Data: <Baba Safta > 42 61 62 61 20 53 61 66 74 61 00 00 00 00 00 00
Object dump complete.
-
April 4th, 2012, 08:41 PM
#3
Re: Detecting memory leaks using _CrtDumpMemoryLeaks
Thanks. I tried that, but now I have a different error:
Code:
error LNK2001: unresolved external symbol "int __cdecl _CrtDumpMemoryLeaks(void)
error LNK2001: unresolved external symbol "__cdecl calloc_dbg(...)
error LNK2001: unresolved external symbol "__cdecl malloc_dbg(...)
error LNK2001: unresolved external symbol "__cdecl realloc_dbg(...)
error LNK2001: unresolved external symbol "__cdecl realloc_dbg(...)
error LNK2001: unresolved external symbol "__cdecl operator_new(...)
...
I don't just have one source code file. My main function is in main.c, and I'm trying to call CrtDumpMemoryLeaks() in FunctionA(), which is in FunctionA.c. This FunctionA.c has
Code:
#include FunctionA.h
And this header file is #include'd in FunctionB.c. But FunctionB() does not call CrtDumpMemoryLeaks().
So what I have in FunctionA.h is
Code:
#ifndef FUNCTIONA_H
#define FUNCTIONA_H
#define _CRTDBG_MAP_ALLOC
typedef unsigned __int8 uint8_t;
typedef unsigned char u_char;
typedef unsigned short u_short;
#include <WinSock2.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#incldue <pcap-int.h>
#include <string.h>
#include <strsafe.h>
#include <signal.h>
#include <time.h>
#include "FunctionC.h"
#include "FunctionD.h"
#include <crtdbg.h>
// Other #define
// Other extern variables
// Structure declarations
// Function declarations
#endif
I'm also trying to build this as a dll.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|