Use mtrace to check memory leakage

Source: Internet
Author: User
As we all know, memory leakage is a top concern for every programmer and company. This problem can be said to be a time bomb and is not easy to detect. Some famous software companies such as Huawei pay close attention to and care about this issue. He consciously mentioned this issue many times when he listened to a lecture from the head of a Huawei driver group, it proves again that the more experienced old developers are more concerned about the minefield of memory, but the most common place we go is the minefield. Recently, our supervisor may be involved in translating foreign technical books. (I don't know if he wants to write a book. Alas, you'll really have to pick a book later ). I will ask some strange technical questions if I'm okay. However, I am still very interested in this question today. It is a kind of stuff that checks for Memory leakage. I have studied it myself, I think it is quite easy to use. I recommend it to you. I hope you will write safe and beautiful code in the future. Body: [overview in code]
# Include <mcheck. h>/* to include it */mtrace ();...
/* Check whether there is any code segment with Memory leakage ;*/...
Muntrace (); [compile our code in the command line after the code is compiled] [Step1]
# GCC main. C-o main [step2]/* the environment variable malloc_trace defines a file where mtrace () writes its output.
* Use the environment variable malloc_trace to define a file log for output;
*/
# Export malloc_trace =./log [Step3] #./main [step4] # below cat log is a system command to view memory leakage information [more understandable format]
# Mtrace -- help/* Help yourself. This command is very simple */p.s. in fact, you can only compile the code, instead of generating an elf file, you can check the memory leakage information. E. g: # gcc-C main. C-o main.omy commands are the actual commands. After the code is compiled, check whether malloc and free are paired from the .txt segment. I was dumbly writing a small function that checked whether my code had a memory leak. At that time, I was impressed with whether it was paired. It is only now known that tools have been available for such common problems that everyone is concerned about. You can't build a car behind closed doors in the future. (If you still don't understand how a piece of code turns into a binary program that a computer can recognize, I suggest you read my original article, the name seems to be "a deep understanding of the Program Generation Process". Let's look for it and I'm a little lazy.) [here is a small instance code written by myself. You can copy it, mtrace]/** // ******************************** **************************************** **********************************
* Filename: Main. c
*
* Author: Version: Date (y/M/d ):
* Li Yang Alpha 1.0
*
* Description: The routine is a simple example, which check the memory lack with "mtrace ".
*
**************************************** **************************************** **************************/
# Include <stdio. h>
# Include <stdlib. h>
# Include <mcheck. h>


Void Foo (char * P)
...{
Printf ("INVOKE Foo function to free memory pointer-p2 ");
Free (P );
}

Int main ()
...{
Int * P1;
Char * P2;

Mtrace ();

P1 = (int *) malloc (4 );
P2 = (char *) malloc (1 );

Printf ("*** before free ***");
Printf ("P1: [% P]", P1 );
Printf ("p2: [% P]", P2 );

Free (P1 );
// Foo (P2);/* after this code is injected, memory leakage will occur */

Printf ("*** after free ***");
Printf ("P1: [% P]", P1 );
Printf ("p2: [% P]", P2 );

Muntrace ();

Return 0;
}

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.