$ cat main.cpp
/* author :hjjdebug
* date: 2018年 01月 26日 星期五 09:24:02 CST
*/
#include
#include
void *g_p1;
int *g_p2;
int ** fun1(void)
{
//内存分配指针付给了局部变量, 函数结束而不释放,为肯定丢失.
//把函数尾部语句return p; 改为return 0;更能说明这个问题.
int **p=(int **)malloc(16);
p[1]=(int *)malloc(40); //如果p丢失了,则p[1]为间接丢失.
g_p1=malloc(110); //付给了全局变量, 内存可以访问
g_p2=(int *)malloc(120);
g_p2++; //付给了全局变量, 内存可以访问,但是指针被移动过,为可能丢失
return p;
}
int main()
{
int **p;
// for(int i=0;i<5;i++) // 可以研究一下5次循环循环所报的结果,全局指针也能引起肯定丢失.
{
p=fun1();
printf("p is %p\n",p);
}
// free(g_p1); //如果不free, 将会有 still reachable 内存泄露
// free(--g_p2);//如果不free, 将会有 possibly lost 内存泄露
// free(p[1]); //如果不free, 将会有 indirectly lost 内存泄露
// free(p); //如果不free, 将会有 definitely lost内存泄露
return 0;
}
/*
总结:
由局部变量指向的内存,如果不释放为肯定丢失,
由此指针而引起的后续内存泄露,为间接丢失.
由全局变量指向的内存如果不被释放,为still reachable,
如果该变量改动过, 为可能丢失.
附上一段英文解释,帮助理解。
是啊,局部变量是栈变量,如果你不能把这个栈变量处理好,出了这个函数,指针地址就丢失了,这就是肯定丢失了.
如果你付给的地址是全局变量,倒是可以访问,叫still reachable
但是如果你这个全局变量的值改动过, 那只有你知道怎样正确访问这块内存,别人可能就访问不到了,这叫可能丢失.
由肯定丢失而引起的进一步的内存丢失为间接丢失.
所以碰到问题你首先要解决什么问题? 肯定丢失, 然后是可能丢失,然后间接丢失,然后still reachable!!!
*/
附上不free时valgrind 的内存错误报告.
$ valgrind --leak-check=full ./test
==18612== Memcheck, a memory error detector
==18612== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==18612== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==18612== Command: ./test
==18612==
p is 0x522f040
==18612==
==18612== HEAP SUMMARY:
==18612== in use at exit: 286 bytes in 4 blocks
==18612== total heap usage: 5 allocs, 1 frees, 1,310 bytes allocated
==18612==
==18612== 56 (16 direct, 40 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 4
==18612== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18612== by 0x1086BC: fun1() (main.cpp:13)
==18612== by 0x108733: main (main.cpp:26)
==18612==
==18612== 120 bytes in 1 blocks are possibly lost in loss record 4 of 4
==18612== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18612== by 0x1086F6: fun1() (main.cpp:16)
==18612== by 0x108733: main (main.cpp:26)
==18612==
==18612== LEAK SUMMARY:
==18612== definitely lost: 16 bytes in 1 blocks
==18612== indirectly lost: 40 bytes in 1 blocks
==18612== possibly lost: 120 bytes in 1 blocks
==18612== still reachable: 110 bytes in 1 blocks
==18612== suppressed: 0 bytes in 0 blocks
==18612== Reachable blocks (those to which a pointer was found) are not shown.
==18612== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==18612==
==18612== For counts of detected and suppressed errors, rerun with: -v
==18612== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
可以放开循环, 进一步理解blocks, record 概念及肯定丢失等概念.