LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to monitor memory leak in C (https://www.linuxquestions.org/questions/programming-9/how-to-monitor-memory-leak-in-c-435741/)

leosgb 04-16-2006 03:52 PM

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!

paulsm4 04-16-2006 04:20 PM

Hi -

Take a look at Valgrind:
http://valgrind.org/

leosgb 04-16-2006 06:22 PM

Checking it now. Thanks! Seems to be very handy! Will be testing it now.

Lsatenstein 04-16-2006 10:28 PM

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.

Dark_Helmet 04-16-2006 10:38 PM

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.

Lsatenstein 04-17-2006 09:13 AM

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++)

leosgb 04-17-2006 11:26 AM

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.

xhi 04-17-2006 01:47 PM

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).


All times are GMT -5. The time now is 10:18 PM.