当前位置:网站首页>QT using Valgrind to analyze memory leaks

QT using Valgrind to analyze memory leaks

2022-06-27 05:18:00 Frame phase

Valgrind Installation and use

Valgrind yes linux The next one is for memory debugging 、 A software development kit for memory leak detection and performance analysis , Contains seven tools .
We use Memcheck To detect memory problems in the program .

sudo apt-get install valgrind# install 
valgrind --version# View version 

QT Integrated Valgrind Tools , After the project is compiled , stay QT menu bar –Analyze–Valgrind Memory Analyzer , Click to analyze memory (Memcheck) Mode startup program , The program runs for a period of time and closes ,Memcheck The memory exception list will be recorded , Contains error message and simple call stack .
 Insert picture description here

The memory is released in a mismatched way

Code example ,new What comes out should be used delete Release ,malloc What comes out should be used free Release ,new The resulting array should use delete [] Release .

// Example a
    int lena = 10;
    int* pa = new int[lena];
    delete pa;//Mismatched free() / delete / delete [], You should use  delete [] pa;

// Example b
    int* pb = new int;
    free(pb);//Mismatched free() / delete / delete [], You should use delete pc;

// Example c
    char *pc;
    int lenc = 10;
    pc = (char *) malloc(lenc);
    delete pc;//Mismatched free() / delete / delete [], You should use free(pd);

Memcheck error message , Mismatched release mode is used , The source file where the error occurred , How many lines, etc

Mismatched free() / delete / delete []
   stay  MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:14 in 
  1: operator delete(void*) in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so
  2: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:14
  3: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9
Address 0x10b225e0 is 0 bytes inside a block of size 40 alloc'd  1: operator new[](unsigned long) in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so
  2: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:13
  3: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9

Repeat release

    char *pd;
    int lend = 10;
    pd = (char *) malloc(lend);
    free(pd);
    free(pd);//Invalid free() / delete / delete[] / realloc(), Repeat release 

Memcheck error message , Invalid release

Invalid free() / delete / delete[] / realloc()
   stay  MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:28 in 
  1: free in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so
  2: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:28
  3: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9
Address 0x10b23650 is 0 bytes inside a block of size 10 free'd  1: free in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so
  2: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:27
  3: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9
Block was alloc'd at  1: malloc in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so
  2: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:26
  3: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9

Memory not released

    int lene = 20;
    //80 bytes in 1 blocks are definitely lost in loss record 6,814 of 8,736
    int* pe = new int[lene];// There is no release , You should use  delete [] pb;

Memcheck error message , There is... In a block 80 Bytes (20 individual int) It must be lost ,new or malloc Memory , But no delete or free.

80 bytes in 1 blocks are definitely lost in loss record 6,820 of 8,741
   stay  MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:31 in 
  1: operator new[](unsigned long) in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so
  2: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:31
  3: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9

Use and release wild pointers to unallocated memory

    char* pf;
    printf("pf=%s\n",pf);//Use of uninitialised value of size 8, Used a wild pointer to unallocated memory 
    free(pf);//Conditional jump or move depends on uninitialised value(s),Invalid free() / delete / delete[] / realloc(), Free wild pointer of unallocated memory 

1、 Used a wild pointer to unallocated memory

Use of uninitialised value of size 8, Uninitialized value used 
   stay  MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:34 in 
  1: strlen in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so
  2: __vfprintf_internal in ./stdio-common/vfprintf-internal.c:1647
  3: buffered_vfprintf in ./stdio-common/vfprintf-internal.c:2295
  4: printf in ./stdio-common/printf.c:33
  5: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:34
  6: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9
Uninitialised value was created by a stack allocation  1: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:6

2、 Free wild pointer of unallocated memory
Conditional jump or move depends on uninitialised value(s), Conditional jumps or moves depend on uninitialized values

Conditional jump or move depends on uninitialised value(s)
   stay  MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:35 in 
  1: free in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so
  2: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:35
  3: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9
Uninitialised value was created by a stack allocation  1: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:6

Invalid free() / delete / delete[] / realloc(), Invalid release

Invalid free() / delete / delete[] / realloc()
   stay  MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:35 in 
Address 0x65cd6c0 is 0 bytes inside data symbol "_IO_2_1_stdout_"  1: free in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so
  2: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:35
  3: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9

Use the released wild pointer

    int* pg;
    pg = new int;
    *pg=5;
    delete pg;
    printf("pg=%d\n",*pg);//Invalid read of size 4,pg Released into wild pointer , Read the field pointer here 

Memcheck error message , Invalid read ,4 Bytes

Invalid read of size 4
   stay  MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:41 in 
  1: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:41
  2: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9
Address 0x10b1b020 is 0 bytes inside a block of size 4 free'd  1: operator delete(void*) in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so
  2: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:40
  3: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9
Block was alloc'd at  1: operator new(unsigned long) in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so
  2: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:38
  3: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9

Array access beyond bounds

    int *ph = (int*)malloc(5*sizeof(int));
    memset(ph,0,5*sizeof(int));
    printf("ph[5] = %d\n",ph[5]);//Invalid read of size 4, Array access beyond bounds 
    free(ph);

Memcheck error message , Invalid read ,4 Bytes

Invalid read of size 4
   stay  MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:45 in 
  1: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:45
  2: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9
Address 0x10b1b084 is 0 bytes after a block of size 20 alloc'd  1: malloc in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so
  2: MainWindow::MainWindow(QWidget*) in /home/chw/testCode/testValgring/testValgring/mainwindow.cpp:43
  3: main in /home/chw/testCode/testValgring/testValgring/main.cpp:9

原网站

版权声明
本文为[Frame phase]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270510494522.html