Detecting memory leaks using _CrtDumpMemoryLeaks

Discussion in 'C Programming' started by lancer6238@yahoo.com, Apr 4, 2012.

  1. Guest

    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
     
    , Apr 4, 2012
    #1
    1. Advertising

  2. gwowen Guest

    On Apr 4, 3:28 am, "" <>
    wrote:
    > 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)
     
    gwowen, Apr 4, 2012
    #2
    1. Advertising

  3. Guest

    On Apr 4, 6:47 am, gwowen <> wrote:
    > On Apr 4, 3:28 am, "" <>
    > wrote:
    >
    > > 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)


    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.
     
    , Apr 5, 2012
    #3
  4. <> wrote:
    > On Apr 4, 6:47 am, gwowen <> wrote:
    > > On Apr 4, 3:28 am, "" <>
    > > wrote:
    > >
    > > > 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)


    > 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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Apr 5, 2012
    #4
  5. Geoff Guest

    On Wed, 4 Apr 2012 18:41:07 -0700 (PDT), ""
    <> wrote:

    >On Apr 4, 6:47 am, gwowen <> wrote:
    >> On Apr 4, 3:28 am, "" <>
    >> wrote:
    >>
    >> > 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)

    >
    >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.
     
    Geoff, Apr 6, 2012
    #5
  6. Guest

    On Apr 6, 7:21 am, Geoff <> wrote:
    > On Wed, 4 Apr 2012 18:41:07 -0700 (PDT), ""
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > <> wrote:
    > >On Apr 4, 6:47 am, gwowen <> wrote:
    > >> On Apr 4, 3:28 am, "" <>
    > >> wrote:

    >
    > >> > 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)

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


    I do have _DEBUG in the preprocessor directives and /MDd for the Code
    Generation | Runtime Library category.
     
    , Apr 9, 2012
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ASP.Confused

    ASP.NET - Detecting memory leaks

    ASP.Confused, Jul 16, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,800
    Marina
    Jul 16, 2004
  2. Jive

    Detecting memory leaks

    Jive, Nov 21, 2004, in forum: Python
    Replies:
    2
    Views:
    398
    Eddie Parker
    Nov 21, 2004
  3. Diego Martins
    Replies:
    4
    Views:
    340
    Diego Martins
    Oct 9, 2006
  4. Ilias Lazaridis

    Detecting memory leaks on apache, mod_python

    Ilias Lazaridis, Dec 17, 2007, in forum: Python
    Replies:
    14
    Views:
    611
    Ilias Lazaridis
    Dec 23, 2007
  5. Jerome Sui

    detecting memory leaks in Perl

    Jerome Sui, Jul 13, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    141
    Jerome Sui
    Jul 13, 2003
Loading...

Share This Page