Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-11-2012, 01:34 AM   #1
LQ Newbie
Registered: May 2012
Posts: 13

Rep: Reputation: Disabled
Please help me find the memory leak with thread program

I have written this program, I ran it tonight, all night, with 100 clients connecting then being terminated. There is a memory leak, since after this night htop tells me that the program is using 616M of memory, while it started with 5300kb.
Valgrind tells me that the memory leak is in the malloc for the parameter c1 and c2 of type (connection*), but the cleanup_handler function is executed...


Ps. The output of the program is:

Program running with default value 600
server localhost:7890 connected to client localhost:59653
client localhost:59653 connected to server localhost:7890
connection between SERVER localhost:7890 and localhost:59653 closed
connection between CLIENT localhost:59653 and localhost:7890 closed

so I think the cleanup_handler are run.
Still valgrind tells me that there is that mem leak

Last edited by wollowizard; 06-11-2012 at 02:00 AM.
Old 06-11-2012, 03:11 AM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,391

Rep: Reputation: 3073Reputation: 3073Reputation: 3073Reputation: 3073Reputation: 3073Reputation: 3073Reputation: 3073Reputation: 3073Reputation: 3073Reputation: 3073Reputation: 3073
I would probably try to print the value of the connection pointer also (when entering sender and cleanup)
Old 06-11-2012, 09:36 AM   #3
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,981
Blog Entries: 4

Rep: Reputation: 3137Reputation: 3137Reputation: 3137Reputation: 3137Reputation: 3137Reputation: 3137Reputation: 3137Reputation: 3137Reputation: 3137Reputation: 3137Reputation: 3137
Pastebin didn't find the file, but never mind ... it's just a matter of looking for it, and you are lucky that Valgrind pretty much told you where it was.

Grab a piece of paper and just start writing down ... by hand, with a number-two pencil and no eraser ... all of the possibilities. For instance, you might know that the cleanup function is being executed, but do you in fact know what parameters were being passed to it? (What if the memory-deallocator was being handed "null?")

Consider modifying the program to write a "running log" output to STDERR. Every time it allocates something, a log message includes its binary address. Each subroutine dumps its parameters. Now, run the program again with these debugging messages turned on. You ought to be able to find the problem with just a handful of connections in a test case.

I am also a big fan of adding things to a program so that it will detect its own errors. Add debugging facilities to your program, and leave them in until the program is completely debugged. Then, and only then, leave them in. Add "eyecatchers" to memory blocks, check to see that the eyecatchers are still there, then change the eyecatchers to something else just before you deallocate the block. Add sequence-numbers to them, too. If at any point the program detects an internal inconsistency, it dies.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Find memory leak for a process sks76 Linux - Newbie 1 03-06-2010 11:31 AM
Memory Leak in a program or two? richcoosa19 Linux - General 1 07-11-2006 06:04 PM
Weird memory leak with find 1337_penguin Linux - General 3 12-22-2005 06:34 AM
How do you find a memory leak? BrianK Linux - General 10 06-10-2004 10:34 PM
Memory Leak when using memory debugging C program on SuSE SLES8 babalina Linux - Distributions 0 10-06-2003 09:39 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:55 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration