How to trace memory leaks
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: How to trace memory leaks

  1. #1
    Join Date
    Sep 2007
    Posts
    78

    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
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: How to trace memory leaks

    Add this at the beginning of source (.cpp) file
    Code:
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  3. #3
    Join Date
    Sep 2007
    Posts
    78

    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
    800

    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,423

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center