LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 04-16-2006, 03:52 PM   #1
leosgb
Member
 
Registered: Nov 2004
Location: Brazil
Distribution: Gentoo
Posts: 363

Rep: Reputation: 31
how to monitor memory leak in C


Hi folks,

I am writing an application that does a lot of dynamic memory allocation (strings and linked lists). I try to free all used pointers once my application is done running but I am not sure if I did it correctly for all allocated pointers. Is there any way to hardcode a "memory leak monitor" in your C application? Or should I use a debugger for this purpose? If so, what debugger is a good option? Thanks!
 
Old 04-16-2006, 04:20 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Take a look at Valgrind:
http://valgrind.org/
 
Old 04-16-2006, 06:22 PM   #3
leosgb
Member
 
Registered: Nov 2004
Location: Brazil
Distribution: Gentoo
Posts: 363

Original Poster
Rep: Reputation: 31
Checking it now. Thanks! Seems to be very handy! Will be testing it now.
 
Old 04-16-2006, 10:28 PM   #4
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
There is also another simple way. Since you are using C, you can write three cover functions. One for malloc(), another for free() and if necessary, a third for realloc()

You would call your cover function to set a bit in a byte to indicate that you allocated a pointer. Another function resets the bit in the byte with a free.
Realloc normally works on an allocated array, and in some environments, works like malloc() when the array has not been initialized.
At then times, you can check that the byte(s) associated with the pointers are all zero. (You could also reset the pointers to zero after a free() and that would help you as well. Just test your pointers for non-zero values, and you could find your memory leaks.
 
Old 04-16-2006, 10:38 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Just to interject:
There is no such thing (to my knowledge) as a memory leak when the program is ended. Memory leaks occur when the program allocates memory and "forgets" about it during normal operation. Regardless, when the program ends, the operating system frees all the memory used by the application. It's not possible for a dead application to hold on to allocated memory.

You only need be concerned about memory consumed while it's running--which is why a memory leak in a server application is such a huge deal.
 
Old 04-17-2006, 09:13 AM   #6
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
Memory leaks

When a program exits, and it is a mainliner, normal cleanup of the stack, and all outstanding memory requests take place. However, when a module is loaded as a plug-in, or as a service, and then released when no longer needed, the memory obtained by the sub-module from the operating system may not be returned. Why? The operating system does not know if the sub-module did the allocation on behalf of the parent. Thus, the best way to handle memory leaks is in your own code.

I suggested a way in my earlier response. I follow what I suggested. I still program (after 40 years) and make use of malloc(), free(), realloc() and by following my practice I stopped having those leak problems.

Leslie
By the way, if you did not write the sub-module, then you have to relie on that author to do it right.

Perhaps it is better to write in other than C (say C++)
 
Old 04-17-2006, 11:26 AM   #7
leosgb
Member
 
Registered: Nov 2004
Location: Brazil
Distribution: Gentoo
Posts: 363

Original Poster
Rep: Reputation: 31
Lsatenstein, all the code is mine. I have the privilege to know everything that happens in the program nothing was copied (not yet hehehe) from any source. All I have are subroutines in my program so I guess there is no real memory leak (according to what you guys said). Hence I dont need to worry about it, for now. But my goal is to have this code run as a daemon so I have to fix the leaks detected by valgrind. I have 6 unallocated pointers in my code but valgrind's message wasnt very helpful to me. I am still looking for them. I will try the "auxiliary function" approach later today. Let's see if I will be able to handle this. Thanks for all suggestions! I will post back w/ my findings as soon as I have something.
 
Old 04-17-2006, 01:47 PM   #8
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by paulsm4
Hi -

Take a look at Valgrind:
http://valgrind.org/
yes a very handy app..

there is also fluid studios memory manager (MMGR).. i have used it for several years and it works very well also. i generally use valgrind now though as it is less work to use.. but MMGR has never missed anything that I can tell, though valgrind will give more detailed information usually (always).
 
  


Reply


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
Memory Leak?! RoaCh Of DisCor Linux - General 9 05-07-2005 12:26 AM
Memory leak?! nyk Programming 10 01-24-2005 07:08 PM
memory leak mfitzpat Linux - Newbie 1 09-24-2004 02:58 PM
possible memory leak?? matt80 Linux - General 2 07-01-2004 11:07 PM
Memory Leak when using memory debugging C program on SuSE SLES8 babalina Linux - Distributions 0 10-06-2003 09:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:51 AM.

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