Các kỹ thuật debug memory leak

      Comments Off on Các kỹ thuật debug memory leak

Có thể tham khảo thêm tài liệu này: https://www.cs.cmu.edu/~guna/15-123S11/Lectures/Lecture06.pdf

https://en.wikipedia.org/wiki/Memory_debugger

Muốn debug thì lúc biên dịch phải enable debug lên bằng cách thêm option khi compile: ví dụ: g++ -g, gcc -g. hoặc có thể thêm -ggdb. -g vs -ggdb không khác nhau nhiều gần như giống nhau nên cứ dùng -g cũng ổn rồi. Xem thêm debug option GNU https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html

Trong C/C++ có 1 tool rất hay để debug memory leak đó là valgrind ( dynamic tool, kiểm tra lúc runtime).

1.valgrind

install: sudo apt-get install valgrind

Debug:  valgrind –leak-check=full ./example1

Ví dụ: example1.cpp


#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(){

        char* ptr = new char[10];
        memset(ptr,0,10);
        strncpy(ptr,"Linux", strlen("Linux"));
        printf("str=%s\n",ptr);
        delete ptr;
        ptr[0]='a';
        return 0;
}

Compile: g++ -Wall -g example1.cpp -o example1
Debug: valgrind –leak-check=full ./example1

Result:

==9247== Memcheck, a memory error detector
==9247== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==9247== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==9247== Command: ./example1
==9247==
str=Linux
==9247== Mismatched free() / delete / delete []
==9247==    at 0x4C2F24B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9247==    by 0x400731: main (<span style="color: #000000;">example1.cpp:11</span>)
==9247==  Address 0x5ab6c80 is 0 bytes inside a block of size 10 alloc'd
==9247==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9247==    by 0x4006E7: main (example1.cpp:7)
==9247==
==9247== <strong><span style="color: #ff0000;">Invalid write of size</span> 1</strong>
==9247==    at 0x400736: main (<span style="color: #ff0000;">example1.cpp:12</span>)
==9247==  Address 0x5ab6c80 is 0 bytes inside a block of size 10 free'd
==9247==    at 0x4C2F24B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9247==    by 0x400731: main (<span style="color: #ff0000;">example1.cpp:11</span>)
==9247==  Block was alloc'd at
==9247==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9247==    by 0x4006E7: main (<span style="color: #ff0000;">example1.cpp:7</span>)
==9247==
==9247==
==9247== HEAP SUMMARY:
==9247==     in use at exit: 72,704 bytes in 1 blocks
==9247==   total heap usage: 3 allocs, 2 frees, 73,738 bytes allocated
==9247==
==9247== LEAK SUMMARY:
==9247==    definitely lost: 0 bytes in 0 blocks
==9247==    indirectly lost: 0 bytes in 0 blocks
==9247==      possibly lost: 0 bytes in 0 blocks
==9247==    still reachable: 72,704 bytes in 1 blocks
==9247==         suppressed: 0 bytes in 0 blocks
==9247== Reachable blocks (those to which a pointer was found) are not shown.
==9247== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==9247==
==9247== For counts of detected and suppressed errors, rerun with: -v
==9247== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

2. Cppcheck ( static check, chek lúc  chưa biên dịch)

http://cppcheck.sourceforge.net/

install:  sudo apt-get install cppcheck

check: cppcheck file

3. Viết lại hàm new/delete or malloc/free

debug-new for c++: redefine new và delete.

malloc debug: Hồi samsung có làm.

Android có sẵn malloc debug: https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md

Dmalloc:

Dmalloc is a C memory debugger library written by Gray Watson to assist programmers in finding a variety of dynamic memory allocation mistakes. It replaces parts (such as malloc) of the C standard library provided by the operating system or compiler with its own versions, which produce information intended to help the programmer detect problematic code.

Dmalloc can find memory leaks, off-by-one errors, and usage of invalid addresses in some library functions calls.

4. Fix bằng cơm

Tìm kiếm tất cả new or malloc và đảm bảo tất cả đã được delete or free 1 cách đúng cách.