CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2007
    Posts
    117

    How to trace memory leaks

    Hi,

    I'm having some problems with detecting the cause of memory leaks in my code.
    The problem here is that at the end of a debug I get a memory dump of all detected leaks, but line in the code where the memory was assigned is not provided.

    So for example a dump will look like this:

    Detected memory leaks!
    Dumping objects ->
    {78} normal block at 0x001910D0, 32 bytes long.
    Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    Object dump complete.
    The thread 0x1158 has exited with code 0 (0x0).
    The program 'C:\SOFTWARE\memoryleaks\Debug\memoryleaks.exe' has exited with code 0 (0x0).


    But not the location in the code.
    Is there some way of getting this information?

    Thanks!

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to trace memory leaks

    Add this at the beginning of source (.cpp) file
    Code:
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Sep 2007
    Posts
    117

    Re: How to trace memory leaks

    Got that one already.

    But that only works for things such as:

    double * d;
    d=new double;

    but not for

    double * d;
    d=(double *)calloc(4,sizeof(double));

    It's the second one where I suspect my memory leaks are, and the ifdef DEBUG line doesn't work here.

  4. #4
    Join Date
    May 2007
    Posts
    811

    Re: How to trace memory leaks

    Not a help with memory leak, but why are you mixing C and C++ memory allocations?

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to trace memory leaks

    Quote Originally Posted by rmirani View Post
    Got that one already.

    But that only works for things such as:

    double * d;
    d=new double;

    but not for

    double * d;
    d=(double *)calloc(4,sizeof(double));

    It's the second one where I suspect my memory leaks are, and the ifdef DEBUG line doesn't work here.
    Then replace that calloc() line with operator "new".

    Heck, even better -- replace that whole thing with std::vector<double>. Then you don't worry about memory leaks at all.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured