Detection of memory leaks in Linux (ii) customized Malloc/free

Source: Internet
Author: User
Tags wrapper

The simplest method of detecting memory leaks in Linux introduces the simplest method of memory leak detection, which is simple, but has many realistic problems, which can not be used in actual production.

Direct use of this method is certainly unrealistic, because:

(1) The entire project calls Malloc/free all the places are changed to My_malloc/my_free, code changes very large.

(2) Usually the code of the dynamic library and the static library has no permission to modify.

Solve this problem today and dynamically decide whether to let the program use its own or the system's memory management interface.

Wrap options

If you do not want to modify the product code, then the interface used to request/release memory is Malloc/free.
Also want to add the function of the count in the interface, it is necessary to implement a set of interface for requesting/releasing memory. The new interface cannot be duplicate with Malloc/free. This is too contradictory.

It would be nice if I could customize a malloc/free for myself.

Fortunately, GCC also thought of this and gave us the wrap option.
This is the description that man LD got:

–wrap=symbol

Use a wrapper function for symbol. Any undefined reference to symbol would be resolved to __wrap_symbol . Any undefined reference to'll be __real_symbol resolved to symbol.

This can is used to provide a wrapper for a system function. The wrapper function should be called __wrap_symbol . If it wishes the system function, it should call __real_symbol .

Here is a trivial example:

>

void * __wrap_malloc (size_t c){    printf ("malloc called with %zu\n", c);    return __real_malloc (c);}

If you link other code with this file Using–wrap malloc, then all calls to would call the malloc function __wrap_malloc instead. The call to in would call the __real_malloc __wrap_malloc real "malloc" function.

wish to provide a __real_malloc function as well, so, links without the–wrap option would succeed. If you don't put the definition __real_malloc __wrap_malloc of the in the same file as; should, the assembler may resolve The call before the linker have a chance to wrap it to malloc .

I explain this big lump of English (English good students can skip):

Wrapper in English is the meaning of packaging, that is, there is no modification of the symbols (usually system symbols) outside with a layer of customized packaging, so that we can reuse the original code, but also add new features.

When you use the Wrap function for a named symbol symbol, any place you want to use symbol is actually a __wrap_symbol symbol

Consider your only to __wrap_symbol symbol add a layer of packaging, it is possible to use the real symbol , only need to __wrap_symbol call you __real_symbol , because any use of the __real_symbol actual use of the realsymbol

That is, when you use the Wrap feature for a named symbol symbol, you get the effect:

(1) When you invoke symbol the actual call is__wrap_symbol

(2) When you invoke __real_symbol the actual call issymbol

(3) The operation of the packaging can be symbol in __wrap_symbol , and then let the __wrap_symbol call __real_wrap , it is equivalent to the use symbol of their own custom-made additional functions.

Customize your own Malloc/free

It looks like this wrap function is exactly what we need, so let's look at how it's used.

(1) Wrap can be used for both variable and function symbols, but what we're going to use now is a function symbol, exactly, the two symbols, malloc and free.

(2) This is an option that works in the link process, plus -Wl,--wrap,malloc -Wl,--wrap,free the link option
(3) __wrap_malloc/__wrap_free function implementation

void * __wrap_malloc(int size){    malloc_count++;    return __real_malloc(size);}void __wrap_free(void *ptr){    free_count++;    __real_free(ptr);}

(4) test

int main(){    0;    0;    int *p1 = (int *)malloc(sizeof(int));    int *p2 = (int *)malloc(sizeof(int));    free( p1);    if(malloc_count != free_count)        printf("memory leak!\n");    return0;}

(5) Operation

-o test main.-Wl,---Wl,--wrap,free
Analysis
    • Advantages

(1) Easy to use-no need to change the product code, only need to modify the compilation options to complete.

(2) Full range-wrap is a link option that works on all files that are linked by __wrap_malloc and __wrap_free, whether it is a static library or a dynamic library.

    • Disadvantages

(1) This method requires running at the end of the print analysis generated in order to know the results.

(2) only applicable to C language, not for C + +

(3) can only detect leakage, but no specific information, such as how much space leaked

(4) does not indicate which line of code caused the leak

Improved

The detection method has a preliminary improvement, but can not meet with this, predict the next improvement, and see tell

Detection of memory leaks in Linux (ii) customized Malloc/free

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.