美文网首页
valgrind内存泄漏跟踪

valgrind内存泄漏跟踪

作者: ebayboy | 来源:发表于2019-04-29 11:05 被阅读0次

#include <stdio.h>

#include <stdlib.h>

void *g_p1;

void *g_p2;

int ** fun1(void)

{

    //内存分配指针付给了局部变量, 函数结束而不释放,为肯定丢失.

    //把函数尾部语句return p; 改为return 0;更能说明这个问题.

    int **p=(int **)malloc(16);

    g_p1=malloc(20);  //付给了全局变量, 内存可以访问

    g_p2=malloc(30);

    g_p2++;            //付给了全局变量, 内存可以访问,但是指针被移动过,为可能丢失

    p[1]=(int *)malloc(40); //如果p丢失了,则p[1]为间接丢失.

    return p;

}

int main(int argc, char *argv[])

{

    int **p=fun1();

//    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 的内存错误报告.

hjj@Inspiron:~/MyTest/test1$ valgrind --leak-check=full ./main

==28209== Memcheck, a memory error detector

==28209== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.

==28209== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info

==28209== Command: ./main

==28209==

==28209==

==28209== HEAP SUMMARY:

==28209==     in use at exit: 106 bytes in 4 blocks

==28209==   total heap usage: 4 allocs, 0 frees, 106 bytes allocated

==28209==

==28209== 30 bytes in 1 blocks are possibly lost in loss record 2 of 4

==28209==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

==28209==    by 0x40055E: fun1 (main.c:15)

==28209==    by 0x4005AB: main (main.c:23)

==28209==

==28209== 56 (16 direct, 40 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4

==28209==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

==28209==    by 0x40053F: fun1 (main.c:13)

==28209==    by 0x4005AB: main (main.c:23)

==28209==

==28209== LEAK SUMMARY:

==28209==    definitely lost: 16 bytes in 1 blocks

==28209==    indirectly lost: 40 bytes in 1 blocks

==28209==      possibly lost: 30 bytes in 1 blocks

==28209==    still reachable: 20 bytes in 1 blocks

==28209==         suppressed: 0 bytes in 0 blocks

==28209== Reachable blocks (those to which a pointer was found) are not shown.

==28209== To see them, rerun with: --leak-check=full --show-leak-kinds=all

==28209==

==28209== For counts of detected and suppressed errors, rerun with: -v

==28209== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

---------------------

作者:hejinjing_tom_com

来源:CSDN

原文:https://blog.csdn.net/hejinjing_tom_com/article/details/79168749

版权声明:本文为博主原创文章,转载请附上博文链接!

相关文章

网友评论

      本文标题:valgrind内存泄漏跟踪

      本文链接:https://www.haomeiwen.com/subject/lwpziqtx.html