C++内存泄露检测

内存检测相关命令:
valgrind –tool=memcheck –leak-check=full –undef-value-errors=yes ./ql_circle_svr2 -id 20000 -tcp 192.168.1.55:8000 -svr 192.168.1.55:8015

valgrind –tool=memcheck –leak-check=full –undef-value-errors=yes ./ql_connect_svr2 -id 30000 -tcp 192.168.1.55:8100 -svr 192.168.1.55:8015

valgrind –tool=memcheck –leak-check=full –undef-value-errors=yes ./ql_data_center2 -id 10000 -tcp 192.168.1.55:8015

valgrind –tool=memcheck –leak-check=full –undef-value-errors=yes ./ql_storage_svr -id 40000 -tcp 192.168.1.55:8300 -svr 192.168.1.55:8015

1、常见内存泄露情况:
(1)动态内存泄露:调用了malloc/new等内存申请的操作,但缺少了对应的free/delete;
(2)资源泄露:文件打开后未关闭;
(3)动态内存越界:
static void mem_overrun1(void)
{
char *p = malloc(1);
(short)p = 2;

free(p);

}

(4)数组内存越界;
static void mem_overrun2(void)
{
char array[5];
strcpy(array, “hello”);
}
(5)动态内存两次 free;
(6)使用野指针,即未初始化的指针;
(7)释放野指针,即未初始化的指针;

2、常见内存操作问题:
(1)使用未初始化的内存 (Use of uninitialised memory)
(2)使用已经释放了的内存 (Reading/writingmemory after it has been free’d)
(3)使用超过 malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)
(4)对堆栈的非法访问 (Reading/writinginappropriate areas on the stack)
(5)申请的空间是否有释放 (Memory leaks –where pointers to malloc’d blocks are lost forever)
(6)malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
(7)src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)

你可能感兴趣的:(内存,C语言)