Memory Leak detection and analysis

Source: Internet
Author: User
Tags valgrind

I. Introduction to memory leaks:
    • Memory leaks are categorized in the way they occur, and memory leaks can be divided into 4 categories:

    • 1. Frequent memory leaks. The code that occurs in memory leaks is executed multiple times, causing a memory leak each time it is executed.

    • 2. Accidental memory leaks. Code that occurs with a memory leak occurs only under certain circumstances or during operation. The occurrence and the incidental sex are opposite. For a given environment, the occasional may become a frequent occurrence. So test environments and test methods are critical to detecting memory leaks.

    • 3. Disposable memory leaks. The code that occurs with a memory leak is only executed once, or because of an algorithmic flaw, there is always a piece of memory that leaks.

Two. C Program memory leaks and other memory common errors and examples
    • 1. No memory leaks caused by free after testing malloc

    • 2. Pointer access is out of bounds

      • ############# #Explame1 ###########################

        • p = (char *) malloc (10);

          • P[24] = ' 0 '; Free (p);

        #################################################

    3. Two times for the same address use free ()

      • ############# #Explame1 ###########################

        • char *p = NULL;

          • char *q = NULL; p = (char *) malloc (2); Free (p); Free (p); It can be detected that this line of free (p+2) can also detect free (q); Can not detect the

        #################################################

    4. Make uninitialized data the amount of the assignment

      • ############# #Explame1 ###########################

        • int n,m;

          • m = n;

        #################################################

    5. The uninitialized value as a condition of judgement

      • ############# #Explame1 ###########################

        • int n; if (n) {

          • n++;

          }

        #################################################

    Memory overlap due to 6.memcpy

      • ############# #Explame1 ###########################

        • Char szstr[10];

          • memcpy (szstr,szstr+3,6);

        #################################################

    7. Stack Overflow

      • ############# #Explame1 ###########################

        • Fun () {

          • Fun ();

          • } main () {

            • Fun ();

            }

        #################################################

    8.new, delete, malloc free collocation error

      • ############# #Explame1 ###########################

        • Char *p = (char *) malloc (2);

        Delete p;

      • #################################################

Three. Introduction of memory detection method under Linux
    • 1. Determine the scope to be checked.

      • Determine which memory errors to detect. This is basically the memory error mentioned above.

      2. Detecting a program using the memory Detection tool

      • At present we use the Open Source Tool under Linux Valgrind for memory leaks and detection of various tools, similar tools are required to re-program at GCC compile time to add the-G parameter, and then can dynamically analyze the program's memory allocation, and various memory errors. Use generally get the result as. (1) Error type (2) The error is located in the file (3) row.

      3. Go deep into the source code for error analysis and guessing.

      • Get the wrong hints from the tool to drill down into the source code for error analysis. Find the bottom-level invocation of the program's own code.

Four. Valgrind Tool Introduction
  • 1. Introduction

      • Valgrind is in Linux under the analysis of the program and debugging a tool-level, you can automatically detect memory management vulnerabilities, avoid system crashes, make the program more stable, can also be detailed analysis of the program, optimize the code, reduce the use of memory resources.

    2. Download

      • The latest version of Valgrind is available for free on http://www.valgrind.org/downloads/current.html

    3. Installation

      • (1) Copy the Valgrind package to its own directory valgrind-3.1.1.tar.bz2 (2) Unzip TAR-JXVF valgrind-3.1.1.tar.bz2 get directory valgrind-3.1.0 (3) Enter the valgrind-3.1.0 directory, and then click Execute

        • ./configure Make make Install

        (4) Installation complete

    4. Using Valgrind

    • (1) Requirements for the program under test

      • Require use of the-G option in GCC compilation

      • For example, compiling the TEST.C program: Gcc-o test-g test.c

      • (2) Basic usage:

        • Valgrind [option] exefile arg ...

        • [option]: Parameter options

        • Exefile: Executable

        • Arg ... : Program parameters

        (3) Use memory detection

        • Option parameter must be added--leak-check=yes

        • Example: Program tes T for example

          • Check memory error for test program: Valgrind--leak-check=yes./test

        (4) Common parameter description

        ul>
      • --show-reachable=no Prompt when the pointer is completely missing =yes prompt

      • -V to get more trusted error statistics as long as the pointer is not released

      • --log-f Ile-exactly=[logfile] Valgrind information into the logfile file, if none of the entries are displayed on the screen

      (5) Example: Test the memory of the program and save the results to the log file

      • Valgrind--leak-check=yes--show-reachable=yes-v--log-file-exactly=log./test

      (6) detailed parameter description see Valgrind online Help manual   http://www.valgrind.org/docs/manual/mc-manual.html

    5.Vallog recognizable errors and error hints

      • 1.malloc/free:in use in Exit is not released before exiting

      • 2.Invalid write of size illegal write memory: general array out of bounds

      • 3.Invalid read of size illegal read memory: General array out of bounds

      • 4.definitely lost/possibly Lost/still reachable in loss record memory not released

      • 5.Invalid free ()/delete/delete[] The same pointer is released multiple times

      • 6.Source and destination overlap memory overlap (typically caused by use of strncpy,memcpy)

      • 7.Syscall param contains uninitialised byte passed in uninitialized variable when calling system function

      • 8.Conditional jump or move depends on uninitialised value condition judged using uninitialized variable

      • 9. Stack overflow access not within mapped region/stack overflow stack Overflow

      • 10.Mismatched free ()/Delete/delete [] new--delete malloc--free collocation error

Memory Leak detection and analysis

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.