Thread: How can detect memory leakage?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    7

    How can detect memory leakage?

    Hi,

    I wrote an application that use pthread. It has memory management problems.
    It runs well without any problem but after some minutes, hours or day, is created Segmentation fault.

    For detecting reason of segmentation fault, i use memwatch, a memory leakage detection tool. But when log file is created, it shows only function that cause problem. Beacuse of using some own functions(wroted by myself) for free specialized structures, the function cause problem, is always same function. I need to get stack trace so i can find where that it call the special function(that it free memory).

    How can i do it?

    I thank you for any other suggestions for memory leakage detection.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by pronetin View Post
    Hi,

    I wrote an application that use pthread. It has memory management problems.
    It runs well without any problem but after some minutes, hours or day, is created Segmentation fault.

    For detecting reason of segmentation fault, i use memwatch, a memory leakage detection tool. But when log file is created, it shows only function that cause problem. Beacuse of using some own functions(wroted by myself) for free specialized structures, the function cause problem, is always same function. I need to get stack trace so i can find where that it call the special function(that it free memory).

    How can i do it?

    I thank you for any other suggestions for memory leakage detection.
    Are you sure it's a memory leak and not a race condition? I consider that a lot more likely...

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    7
    Quote Originally Posted by EVOEx View Post
    Are you sure it's a memory leak and not a race condition? I consider that a lot more likely...
    Even if be race condition(for example access to memory before initializing it), should be found by memory leakage detection tools, shouldn't?

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I think you can give Valgrind a try:

    Valgrind Home
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    7
    Quote Originally Posted by claudiu View Post
    I think you can give Valgrind a try:

    Valgrind Home
    I tried it before. It needs glibc 2.2 to 2.10 but in my system(ubuntu 10.04) installed 2.10-1. So it cannt be used.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by pronetin View Post
    I tried it before. It needs glibc 2.2 to 2.10 but in my system(ubuntu 10.04) installed 2.10-1. So it cannt be used.
    Just use "apt-get install valgrind". Never had trouble with that.

    Whether it should be detected by a memory leak... well, maybe, maybe not. That really depends on how advanced the application is ;-). But most proper ones probably will, yeah. Never used memwatch, though, but valgrind does it.
    But it makes the way you should look at the function very different.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by EVOEx View Post
    Just use "apt-get install valgrind". Never had trouble with that.

    Whether it should be detected by a memory leak... well, maybe, maybe not. That really depends on how advanced the application is ;-). But most proper ones probably will, yeah. Never used memwatch, though, but valgrind does it.
    But it makes the way you should look at the function very different.
    Yes, I never had a problem with valgrind on Ubuntu either.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by pronetin View Post
    Even if be race condition(for example access to memory before initializing it), should be found by memory leakage detection tools, shouldn't?
    Possibly... probably not. Accessing memory that is in your arena, but not allocated, isn't a memory leak. I believe he means something more along the lines of semaphores not being placed correctly, causing something to "go" with an invalid value as because there was no semaphore on the thread setting the value. It's very easy to get caught up with large multi-threaded applications.

    Run it in gdb, when you get the SEGV signal, print out all your values, find out what's bad, exactly why it crashed, memory leakage typically won't cause a seg fault, a bad pointer will. Just run it, when it crashes, it'll tell you where, then start printing different values, find out what could possibly make that pointer (typically a pointer) invalid to make it crash there.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    7

    Wink

    Just use "apt-get install valgrind". Never had trouble with that.
    Ok. In Ubuntu's repository exist Valgrind. I tried to install Valgrind from source that showed me glibc compatibility error. When Valgrind is install from repositories, there isn't any problem.

  10. #10
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Probably because it installs dependencies for you. E.g. it installed whatever version of glibc was required for it to work.

    (Don't take my word for it, I'm a Windows person.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bug in Best-Fit Memory Allocation program (Simulation)
    By RommelTJ in forum C Programming
    Replies: 6
    Last Post: 12-13-2009, 04:43 PM
  2. Memory Fragmentation with Dynamic FIFO Queue
    By fguy817817 in forum Linux Programming
    Replies: 17
    Last Post: 10-31-2009, 04:17 AM
  3. Memory allocation/reallocation
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-04-2008, 03:27 PM
  4. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  5. Question about the Basics of Memory Leakage
    By Shamino in forum C++ Programming
    Replies: 12
    Last Post: 09-26-2005, 09:42 AM

Tags for this Thread