Detecting memory leaks in C programs?

梦想与她 提交于 2019-12-20 08:14:10

问题


If we would like to check for memory leaks in a C++ program, we can overload the new and delete operators to keep track of the memory that was allocated. What if we would like to check for leaks in a C program? Since there is no operator overloading in C, can we over-write the malloc function pointer to intercept calls to malloc and track memory allocation? Is there an easier way without using any external utilities? Please provide some code as I am not familiar with over-writing method pointers.

Note: I would like to do this without any external utilities for practice.


回答1:


As suggested, there already exist excellent tools like Valgrind to do this.

Further:

I would like to do this without any external utilities for practice
This is interesting and I am sure would be fulfilling,
You can use macro trick to detect such memory usage and leak errors, in fact write your own neat leak detector. You should be able to do this as long as you have a single allocation and deallocation function in your project.

#define malloc(X) my_malloc( X, __FILE__, __LINE__, __FUNCTION__)

void* my_malloc(size_t size, const char *file, int line, const char *func)
{

    void *p = malloc(size);
    printf ("Allocated = %s, %i, %s, %p[%li]\n", file, line, func, p, size);

    /*Link List functionality goes in here*/

    return p;
}

You maintain a Linked List of addresses being allocated with the file and line number from where there allocated. You update the link list with entries in your malloc.

Similar to above you can write an implementation for free, wherein you check the address entries being asked to be freed against your linked list. If there is no matching entry its a usage error and you can flag it so.

At the end of your program you print or write the contents of your linked list to an logfile. If there are no leaks your linked list should have no entries but if there are some leaks then the logfile gives you exact location of where the memory was allocated.

Note that in using this macro trick, you lose the type checking which functions offer but it's a neat little trick I use a lot of times.

Hope this helps and All the Best :)




回答2:


Valgrind is what you need.

I remember reading first chapter of Algorithms in a Nutshell which talked about this although it didn't include code. Just added in case you find it interesting.

since there is no operator overloading in c can we over-write malloc function point to intercept calls to malloc and track memory allocation

Actually, you can. GIve LD_PRELOAD a read.




回答3:


In addition to @Als's answer which will wrap calls in your source code, if you're using gnu ld, you can have the linker wrap all calls (presumably to malloc, realloc, calloc, and free) at link time, irrespective of where they come from. You then write __wrap_malloc etc and can call the original function with, for example, __real_malloc.

See --wrap=symbol in http://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_3.html

I don't know how this works with calls from shared libraries. I'm guessing it doesn't.




回答4:


Here is how you can modify malloc, free hooks : Hooks for Malloc




回答5:


Use mallinfo function it worked for me on Xilinx Zynq baremetal using Xilinx SDK gcc. I tested with intentional memory leak - I have no idea why but google results are were incredibly terrible at finding this solution spread the word to help other devs!



来源:https://stackoverflow.com/questions/9074229/detecting-memory-leaks-in-c-programs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!