Detecting memory leaks using _CrtDumpMemoryLeaks

L

lancer6238

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
 
G

gwowen

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

Move
#include <crtdbg.h>
to after the other #include <> statements (i.e. make sure its the last
one)
 
L

lancer6238

Move
#include <crtdbg.h>
to after the other #include <> statements (i.e. make sure its the last
one)

Thanks. I tried that, but now I have a different error:

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

#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

#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.
 
J

Jens Thoms Toerring

Thanks. I tried that, but now I have a different error:
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(...)
...

And that aren't compiler errors anymore (so the code seems to
be fine now in the sense that the compiler has no trouble com-
piling it) but now you got linker errors. They tell you that
you rather likely aren't linking against the library that con-
tains the functions mentioned in the error messages. I have no
experience with this CrtDbg library but my guess is that it's
the one with all those missing symbols. So you probably forgot
to tell the linker that it is supposed to link your program
against it.
Regards, Jens
 
G

Geoff

Thanks. I tried that, but now I have a different error:

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

#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

#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.

You must define _DEBUG in the preprocessor directives for the project.
You must link with the debug runtime. Set /MDd for the multithreaded
dynamic library (MSVCRTD.LIB).

You can access this option in the IDE via the Project Properties
dialog and the Code Generation | Runtime Library category. The default
for a DLL is /MDd since it's a VERY bad idea to statically link the
wrong version runtime.
 
L

lancer6238

You must define _DEBUG in the preprocessor directives for the project.
You must link with the debug runtime. Set /MDd for the multithreaded
dynamic library (MSVCRTD.LIB).

You can access this option in the IDE via the Project Properties
dialog and the Code Generation | Runtime Library category. The default
for a DLL is /MDd since it's a VERY bad idea to statically link the
wrong version runtime.

I do have _DEBUG in the preprocessor directives and /MDd for the Code
Generation | Runtime Library category.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top