ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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.
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.
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++)
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.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.