Memory leakage detection

Source: Internet
Author: User

This section describes how to use the library injection method to detect memory leaks.

Library injection is the function used to load the library.ProgramInstead of calling the functions in the library.

Memory leakage mainly involves the heavy-load malloc and free functions. After the heavy-load, when the program calls malloc and free to allocate and release the memory, it uses the self-compiled malloc and free functions.

1. How to complete library injection?

In Linux, you can use ld_preload to specify the compiled overload library.

The specific method is as follows:

1) g ++-shared-FPICLeakFinder. Cpp-oLeakFinder. So-LDL // compile to obtain the dynamic library that reloads the malloc and free functions.
2) Export ld_preload = ./LeakFinder. So // configure a pre-loaded dynamic library
3)./c_example // execute the example Program
When the malloc and free functions are reloaded, use extern "c" to identify the malloc and free functions. this is because C ++ and C do not name functions. c ++ contains the corresponding parameters, but C does not.

2. How to Implement the overloaded malloc and free functions, and what work needs to be done?

1) Basic memory allocation and release must be completed. You can call the system's own malloc and free functions.

ExploitationVoid * dlsym (void * handle, const char * symbol)The function obtains the system's malloc and free function pointers, and uses the system's malloc and free function pointers to complete the basic work of memory allocation and release.

2) Save the memory allocation information to check whether memory leakage exists at the end, and use vector to save the corresponding memory allocation information.

When each allocation is made, a record is added to the vector. When each release is made, the corresponding record information is searched for and deleted.

In the final program result, the system checks whether the vector has recorded information to determine whether memory leakage has occurred.

3) records the memory allocation information, including the allocated address, and uses the function call sequence (to facilitate Memory leakage, a prompt is provided)

In Linux, function call stack information is obtained through backtrace and backtrace_symbols.
4) when the program ends, check whether there is memory leakage. If yes, output the corresponding information. use _ attribute _ (destructor) to specify the memory leakage statistics executed after the program ends.

Static VoidCompile_allocation () _ attribute _ (destructor ));
5) when saving the memory allocation information, you need to allocate and release the memory. If it is not distinguished from the external memory allocation and release, there will be infinite loop recursive calls.

Use the global tag volume to identify whether the call is from the outside or from the inside.

6) The Global tag volume may be modified by multiple threads, resulting in thread insecurity. Therefore, you need to use mutex lock to control memory information storage.

 

references: http://www.codeproject.com/Articles/393957/Cplusplus-Memory-Leak-Finder

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.