Use mtrace () to capture memory leaks

Source: Internet
Author: User

Original article: http://blog.yam.com/ddy1280/article/3064451

Recently, we found that memory leaks exists in the program used by YACC & Lex.
It took some time to complete the division and finally found the problem in YACC & Lex.
Mtrace is a good tool to help me catch these problems. Mtrace is a function provided by gnu c/C ++ (Linux platform ).
Using a ticket with GCC-g in addition to metadata, you can easily find the problem

Example:

 1 #include <stdio.h> 2 #include <stdlib.h> 3  4 int main(int argc, char *argv[]) 5 { 6     int *p; 7     int i; 8  9     #ifdef DEBUG10         setenv("MALLOC_TRACE", "./memleak.log", 1);11         mtrace();12     #endif13 14     p=(int *)malloc(1000);15     if (p!=NULL)16     {17         for (i=0;i<1000;i++)18         {19           p[i]=i;20         }21     }22     else23         printf("malloc fail");24 25     return 0;26 }
Malloc has an empty space in this program, and the memory has not been free. We have added the following code 9 for the Division worker.# Ifdef debug10 setenv ("malloc_trace", "./memleak. log", 1); 11 mtrace (); 12# EndifThen restart gcc-g-o AA-ddebug AA. C.


Rows./AAProgram. After the operation is completed, the./memleak. log file is displayed in the project to view its content,
When the scalability is relatively low, it cannot be seen any problems.

$ Cat memleak. Log
= Start
@./AA :( mtrace + 0xf9) [0x8048401] + 0x8049ab0 0x3e8
@/Lib/tls/libc. so.6 :( clearenv + 0x7c) [0x42029acc]-0x8049690
@/Lib/tls/libc. so.6 :( tdestroy + 0x39) [0x420dc9e9]-0x8049700
@/Lib/tls/libc. so.6 :( tdestroy + 0x41) [0x420dc9f1]-0x8049728
@/Lib/tls/libc. so.6: [0x42113a22]-0x8049648
@/Lib/tls/libc. so.6: [0x42113a52]-0x8049660
@/Lib/tls/libc. so.6: [0x42113a96]-0x8049678

In this case, you can use the/usr/bin/mtrace tool compiled by Perl to parse the above information.

$ Mtrace AA memleak. Log
-0x08049690 free 3 was never alloc 'd 0x42029acc
-0x08049700 free 4 was never alloc 'd 0x420dc9e9
-0x08049728 free 5 was never alloc 'd 0x420dc9f1
-0x08049648 free 6 was never alloc 'd 0x42113a22
-0x08049660 free 7 was never alloc 'd 0x42113a52
-0x08049678 free 8 was never alloc 'd 0x42113a96

Memory not freed:
-----------------
Address size caller
0x08049ab0 0x3e8 at/home/ddy/test/AA. C: 14

In this way, we can know that there is no corresponding free () for the malloc of the AA. c 14th line ()
By making good use of such tools, you can make the program better stable ^__ ^

<The above programs are not available in GCC 3.2.2 on Linux platform without authorization>

Additional information:
The gnu c library's allocation debugging
Identifying memory leaks in Linux for C ++ programs


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.