使用TCMalloc检测内存泄漏

测试代码

#include 

void leaky() {
    int* p = new int(0);
    p = NULL;
}

int main() {
    leaky();
    int* b = new int(1);
    delete b;
    int* dd = new int(0);
    dd = NULL;
    getchar();
    return 0;
}

MakeFile

加入-ltcmalloc

CC=g++
SRC = $(wildcard *.cpp)
OBJS = $(patsubst %.cpp, %.o, $(SRC))
FLAG = -g  -Werror -I. -pthread -ltcmalloc
TARGET = a.out

$(TARGET):$(OBJS)
        $(CC) -o $@ $^ $(FLAG)

%.o:%.cpp
        $(CC) -o $@ -c $(FLAG) $< -g -MD -MF [email protected]

clean:
        rm -rf $(TARGET) $(OBJS)


运行命令行

HEAPCHECK=normal ./a.out

输出:

WARNING: Perftools heap leak checker is active -- Performance may suffer

Have memory regions w/o callers: might report false leaks
Leak check _main_ detected leaks of 8 bytes in 2 objects
The 2 largest leaks:
*** WARNING: Cannot convert addresses to symbols in output below.
*** Reason: Cannot run 'pprof' (is PPROF_PATH set correctly?)
*** If you cannot fix this, try running pprof directly.
Leak of 4 bytes in 1 objects allocated from:
        @ 4006df
        @ 400700
        @ 7f6644a79555
        @ 400609
Leak of 4 bytes in 1 objects allocated from:
        @ 40072a
        @ 7f6644a79555
        @ 400609


If the preceding stack traces are not enough to find the leaks, try running THIS shell command:

pprof ./a.out "/tmp/a.out.4781._main_-end.heap" --inuse_objects --lines --heapcheck  --edgefraction=1e-10 --nodefraction=1e-10 --gv

If you are still puzzled about why the leaks are there, try rerunning this program with HEAP_CHECK_TEST_POINTER_ALIGNMENT=1 and/or with HEAP_CHECK_MAX_POINTER_OFFSET=-1
If the leak report occurs in a small fraction of runs, try running with TCMALLOC_MAX_FREE_QUEUE_SIZE of few hundred MB or with TCMALLOC_RECLAIM_MEMORY=false, it might help find leaks more repeatabl
Exiting with error code (instead of crashing) because of whole-program memory leaks

运行命令行

pprof ./a.out "/tmp/a.out.4781._main_-end.heap" --inuse_objects --lines --heapcheck  --edgefraction=1e-10 --nodefraction=1e-10 --text --stack

输出:

[root@localhost leak]# pprof ./a.out "/tmp/a.out.4781._main_-end.heap" --inuse_objects --lines --heapcheck  --edgefraction=1e-10 --nodefraction=1e-10 --text --stack
Using local file ./a.out.
Using local file /tmp/a.out.4781._main_-end.heap.
Total: 2 objects
Stacks:

1        (000000000040072a) /root/code/leak/main.cpp:14:main
         (00007f6644a79554) ??:0:__libc_start_main
         (0000000000400608) ??:0:_start

1        (00000000004006df) /root/code/leak/main.cpp:4:leaky
         (00000000004006ff) /root/code/leak/main.cpp:11:main
         (00007f6644a79554) ??:0:__libc_start_main
         (0000000000400608) ??:0:_start

Leak of 4 bytes in 1 objects allocated from:
        @ 004006df unknown
        @ 00000000004006ff main /root/code/leak/main.cpp:11
        @ 00007f6644a79554 __libc_start_main ??:0
        @ 0000000000400608 _start ??:0
Leak of 4 bytes in 1 objects allocated from:
        @ 0040072a unknown
        @ 00007f6644a79554 __libc_start_main ??:0
        @ 0000000000400608 _start ??:0

       1  50.0%  50.0%        1  50.0% leaky /root/code/leak/main.cpp:4
       1  50.0% 100.0%        1  50.0% main /root/code/leak/main.cpp:14
       0   0.0% 100.0%        2 100.0% __libc_start_main ??:0
       0   0.0% 100.0%        2 100.0% _start ??:0
       0   0.0% 100.0%        1  50.0% /root/code/leak/main.cpp:11

结果:

(000000000040072a) /root/code/leak/main.cpp:14:main

(00000000004006df) /root/code/leak/main.cpp:4:leaky

发生内存泄漏

你可能感兴趣的:(使用TCMalloc检测内存泄漏)