Thread: Detecting memory leak in complicating source code

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    Detecting memory leak in complicating source code

    Hello everyone,

    I'am trying to run a code which performs continuously min-cuts to a graph until it detects dense subgraphs.
    Unfortunately, it seems that there is a memory leak and the program exits. As the program is constituted of many files, it is really difficult to detect the memory leak error.
    I was wondering if there is a way to print the size of the allocated memory after each min-cut in order to test my code and find the memory leak.

    Thank you!!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What makes you think it's a memory leak? (Do memory leaks crash programs as such? I though they would either go completely unnoticed - leading me to believe I don't have any memory leaks in my programs -, or if they are large, starve the program from resources, so it gets slower over time.)

    Have you tried a debugger?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, a memory leak can of course cause a crash by the application expecting memory to be there and not getting any. It may or may not run slower.

    In C, the typical out of memory reaction is to return NULL from malloc - if the app isn't checking for NULL pointers, then it will crash in such an event.

    But yes, memory leaks are only fatal if the application can't cope with "out of memory" [and it may not always make sense to make an application COMPLETELY resilient t OOM].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I had this article in mind which seems to indicate it is pretty hard to get a memory allocation failure.

    I don't know - by not doing any manual memory management my programs just don't leak - or I haven't noticed that. When I try to cause a memory allocation failure by leaking memory intentionally the system just gets slow but I have never seen a std::bad_alloc thrown or a NULL pointer returned. Perhaps it is more likely with memory corruption?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If you're using Visual Studio, look at the documention of _CrtDumpMemLeaks.

    If you're using Linux, run your program through valgrind. It's the ultimate tool for discovering memory leaks, memory corruptions, double frees, and uninitialized memory access.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    I had this article in mind which seems to indicate it is pretty hard to get a memory allocation failure.
    Let me first say that it's quite likely that you are right, and the term memory leak is incorrect, and should be replaced with memory trouble.

    But I do think that "program crashing" may also be "program aborts with an error message", which is the default mechanism for handling throws with no try block. And if the code was written like most code, it doesn't expect to see bad_alloc or any other exception, so it doesn't contain a try-block in main() or such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  2. Huge memory leak
    By durban in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2005, 12:10 PM
  3. Memory leak trackers
    By Darkness in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-30-2004, 02:03 PM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  5. C source code for int25 or code help
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-26-2001, 02:04 AM