简体   繁体   English

即使释放了所有堆块,Valgrind也会出错

[英]Valgrind errors even though all heap blocks were freed

I have recently developed a habit of running all of my programs through valgrind to check for memory leaks, but most of its results have been a bit cryptic for me. 我最近养成了通过valgrind运行所有程序来检查内存泄漏的习惯,但是大部分结果对我来说都有点神秘。

For my latest run, valgrind -v gave me: 对于我最近的跑步, valgrind -v给了我:

All heap blocks were freed -- no leaks are possible

That means my program's covered for memory leaks , right? 这意味着我的程序被内存泄漏所覆盖,对吗?

So what does this error mean? 那么这个错误意味着什么? Is my program not reading certain memory blocks correctly? 我的程序没有正确读取某些内存块吗?

ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 14 from 9)

1 errors in context 1 of 1:
Invalid read of size 4
   at 0x804885B: findPos (in /home/a.out)
   by 0xADD918: start_thread (pthread_create.c:301)
   by 0xA26CCD: clone (clone.S:133)
 Address 0x4a27108 is 0 bytes after a block of size 40 alloc'd
   at 0x4005BDC: malloc (vg_replace_malloc.c:195)
   by 0x804892F: readInput (in /home/a.out)
   by 0xADD918: start_thread (pthread_create.c:301)
   by 0xA26CCD: clone (clone.S:133)

used_suppression:     14 dl-hack3-cond-1

Also, what are the so-called "suppressed" errors here? 此外,这里所谓的“抑制”错误是什么?

This seems obvious ... but it might be worth pointing out that the "no leaks are possible" message does not mean that your program cannot leak; 这似乎很明显......但值得指出的是"no leaks are possible"消息并不意味着你的程序不能泄漏; it just means that it did not leak in the configuration under which it was tested. 它只是意味着它在测试的配置中没有泄漏。

If I run the following with valgrind with no command line parameters, it informs me that no leaks are possible. 如果我使用没有命令行参数的valgrind运行以下命令,它会通知我没有泄漏是可能的。 But it does leak if I provide a command line parameter. 但是如果我提供命令行参数,它确实会泄漏。

int main( int argc, char* argv[] )
{
   if ( argc > 1 )
      malloc( 5 );
   printf( "Enter any command line arg to cause a leak\n" );
}
  1. Yes, you are greatly covered, don't think that valgrind easily can miss a leak in user code 是的,你被大大覆盖,不要以为valgrind很容易错过用户代码泄漏
  2. your error means that you probably have a +1 error in indexing an array variable. 您的错误意味着索引数组变量时可能会出现+1错误。 the lines that valgrind tell you should be accurate, so you should easily find that, provided you compile all your code with -g valgrind告诉你的那些行应该准确,所以如果用-g编译所有代码,你应该很容易找到它
  3. suppressed errors are usually from system libraries, which sometimes have small leaks or undectable things like the state variables of threads. 抑制错误通常来自系统库,系统库有时会出现小漏洞或线程状态变量等不可检测的错误。 your manual page should list the suppression file that is used by default 您的手册页应列出默认使用的抑制文件

Checking for memory leaks is one reason to use valgrind, but I'd say a better reason is to find more serious errors in your code, such as using an invalid array subscript or dereferencing an uninitialized pointer or a pointer to freed memory. 检查内存泄漏是使用valgrind的一个原因,但我想说更好的理由是在代码中发现更严重的错误,例如使用无效的数组下标或取消引用未初始化的指针或指向释放内存的指针。

It's good if valgrind tells you that the code paths you exercised while running valgrind didn't result in memory leaks, but don't let that cause you to ignore reports of more serious errors, such as the one you're seeing here. 如果valgrind告诉你在运行valgrind时运行的代码路径没有导致内存泄漏,这是很好的,但是不要让它忽略更严重错误的报告,例如你在这里看到的错误。

As other have suggested, rerunning valgrind after compiling with debug information (-g) would be a good next step. 正如其他人所建议的那样,在使用调试信息(-g)进行编译后重新运行valgrind将是一个很好的下一步。

If you are getting below error:- "Invalid read of size 4" 如果您收到以下错误: - “读取大小4无效”

Are you freeing memory before and then go to next argument? 你之前是否释放记忆然后再进入下一个论点? I am also getting error because in linked list I am freeing memory first and then go to next element. 我也得到错误,因为在链表中我首先释放内存然后转到下一个元素。 Below is my code snippet where I am getting error - 下面是我的代码片段,我收到错误 -

void free_memory(Llist **head_ref)
{
    Llist *current=NULL;

    current=*head_ref;
    while(*head_ref != NULL)
    {
        current=*head_ref;
        free(current);
        current=NULL;
        (*head_ref)=(*head_ref)->next;
    }
}

After changes below is my code snippet - 以下更改后是我的代码段 -

void free_memory(Llist **head_ref)
{
    Llist *current=NULL;
    current=*head_ref;
    while(*head_ref != NULL)
    {
        current=*head_ref;
        (*head_ref)=(*head_ref)->next;
        free(current);
        current=NULL;
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM