了解 Valgrind 输出
我是 Linux 新手。如何解释 Valgrind 的以下输出?
valgrind --tool=memcheck --leak-check=yes ./main
它说有些块丢失了。如何解决内存泄漏问题?
==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)
I am new to Linux. How can I interpret the following output from Valgrind?
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
输出显示了在 Schema.cc 的第 86 行和第 115 行(
strdup
调用)中分配泄漏(和丢失,即没有指向剩余内存的指针)的堆栈。The output shows the stack for where the leaked (and lost, i.e., no pointers to which remain) memory was allocated, in lines 86 and 115 of Schema.cc (an
strdup
call).来自 opengroup 的 strdup() 描述:
简而言之,
strdup()
正在调用malloc()
。使用完后,您需要free()
该结果字符串。不过,当您使用 C++ 时,如果可以的话,我强烈推荐使用std::string
。请记住,如果您确实需要 C 等效的string.c_str()
将为您提供。From opengroup's strdup() description:
In short,
strdup()
is callingmalloc()
. You will need tofree()
that resultant string when you are done with it. Although, as you're using C++, I'd highly recommendstd::string
instead if you can. Remember, if you really need the C equivalentstring.c_str()
will get it for you.