简体   繁体   English

了解valgrind输出

[英]Understanding valgrind output

I am new to linux. 我是linux的新手。 How can I interpret the following output from 如何解释以下输出

valgrind --tool=memcheck --leak-check=yes ./main

It says some blocks are lost. 它说有些街区丢失了。 How can I nail down memory leaks. 我怎样才能确定内存泄漏。

==2599== HEAP SUMMARY:
==2599==     in use at exit: 17,327 bytes in 55 blocks
==2599==   total heap usage: 180,597 allocs, 180,542 frees, 15,787,989,675 bytes allocated
==2599== 
==2599== 9 bytes in 1 blocks are definitely lost in loss record 5 of 19
==2599==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2599==    by 0x41E546F: strdup (strdup.c:43)
==2599==    by 0x804BA2A: Schema::Schema(char*, char*) (Schema.cc:86)
==2599==    by 0x804AD78: CNF::GrowFromParseTree(AndList*, Schema*, Record&) (Comparison.cc:606)
==2599==    by 0x804EE52: main (main.cc:28)
==2599== 
==2599== 10 bytes in 2 blocks are definitely lost in loss record 6 of 19
==2599==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2599==    by 0x41E546F: strdup (strdup.c:43)
==2599==    by 0x804BB84: Schema::Schema(char*, char*) (Schema.cc:115)
==2599==    by 0x804AD78: CNF::GrowFromParseTree(AndList*, Schema*, Record&) (Comparison.cc:606)
==2599==    by 0x804EE52: main (main.cc:28)
==2599== 
==2599== 13 bytes in 1 blocks are definitely lost in loss record 9 of 19
==2599==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2599==    by 0x41E546F: strdup (strdup.c:43)
==2599==    by 0x804BA2A: Schema::Schema(char*, char*) (Schema.cc:86)
==2599==    by 0x804EDF4: main (main.cc:23)
==2599== 
==2599== 13 bytes in 1 blocks are definitely lost in loss record 10 of 19
==2599==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2599==    by 0x41E546F: strdup (strdup.c:43)
==2599==    by 0x804BA2A: Schema::Schema(char*, char*) (Schema.cc:86)
==2599==    by 0x804EEA4: main (main.cc:37)
==2599== 
==2599== 188 bytes in 16 blocks are definitely lost in loss record 16 of 19
==2599==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2599==    by 0x41E546F: strdup (strdup.c:43)
==2599==    by 0x804BB84: Schema::Schema(char*, char*) (Schema.cc:115)
==2599==    by 0x804EDF4: main (main.cc:23)
==2599== 
==2599== 188 bytes in 16 blocks are definitely lost in loss record 17 of 19
==2599==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2599==    by 0x41E546F: strdup (strdup.c:43)
==2599==    by 0x804BB84: Schema::Schema(char*, char*) (Schema.cc:115)
==2599==    by 0x804EEA4: main (main.cc:37)
==2599== 
==2599== LEAK SUMMARY:
==2599==    definitely lost: 421 bytes in 37 blocks
==2599==    indirectly lost: 0 bytes in 0 blocks
==2599==      possibly lost: 0 bytes in 0 blocks
==2599==    still reachable: 16,906 bytes in 18 blocks
==2599==         suppressed: 0 bytes in 0 blocks
==2599== Reachable blocks (those to which a pointer was found) are not shown.
==2599== To see them, rerun with: --leak-check=full --show-reachable=yes
==2599== 
==2599== For counts of detected and suppressed errors, rerun with: -v
==2599== ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 19 from 8)

输出显示了在Schema.cc的第86行和第115行( strdup调用)中泄漏(并且丢失,即没有剩余指针)内存的堆栈。

From opengroup's strdup() description: 来自opengroup的strdup()描述:

The strdup() function shall return a pointer to a new string, which is a duplicate of the string pointed to by s1. strdup()函数应返回一个指向新字符串的指针,该字符串是s1指向的字符串的副本。 The returned pointer can be passed to free(). 返回的指针可以传递给free()。 A null pointer is returned if the new string cannot be created. 如果无法创建新字符串,则返回空指针。

In short, strdup() is calling malloc() . 简而言之, strdup()调用malloc() You will need to free() that resultant string when you are done with it. 完成后,您需要free()结果字符串。 Although, as you're using C++, I'd highly recommend std::string instead if you can. 虽然,当你使用C ++时,我强烈建议你使用std::string Remember, if you really need the C equivalent string.c_str() will get it for you. 请记住,如果你真的需要C等效的string.c_str()将为你得到它。

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

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