如何阅读这个valgrind信息

时间:2015-07-07 13:27:26

标签: c debugging gdb malloc valgrind

我正在运行一个多线程程序,每次都会遇到像

这样的错误
  

malloc():内存损坏

或分段错误。

我决定用valgrind来调查我的程序显示哪种问题。首先,我得到了以下输出。有人可以帮我理解如何阅读输出消息吗?

 ==17413== Conditional jump or move depends on uninitialised value(s)
==17413==    at 0x47A2349: ns_name_ntop (ns_name.c:147)
==17413==    by 0x47A3271: ns_name_uncompress (ns_name.c:585)
==17413==    by 0x479B3EF: dn_expand (res_comp.c:93)
==17413==    by 0x479FD2B: __res_queriesmatch (res_send.c:327)
==17413==    by 0x47A0D19: __libc_res_nsend (res_send.c:1327)
==17413==    by 0x479DDC7: __libc_res_nquery (res_query.c:226)
==17413==    by 0x479E417: __libc_res_nquerydomain (res_query.c:582)
==17413==    by 0x479E8FB: __libc_res_nsearch (res_query.c:416)
==17413==    by 0x404B1D9: _nss_dns_gethostbyname3_r (dns-host.c:192)
==17413==    by 0x404B540: _nss_dns_gethostbyname_r (dns-host.c:273)
==17413==    by 0x42C53FA: gethostbyname_r@@GLIBC_2.1.2 (getXXbyYY_r.c:266)
==17413==    by 0x42C4B7B: gethostbyname (getXXbyYY.c:116)
==17413==  Uninitialised value was created by a stack allocation
==17413==    at 0x804ADA3: UDP_Server_Open(int&, unsigned short) (UDP_Server.cpp:16)
==17413== 
==17413== Use of uninitialised value of size 4
==17413==    at 0x47A2382: ns_name_ntop (ns_name.c:153)
==17413==    by 0x47A3271: ns_name_uncompress (ns_name.c:585)
==17413==    by 0x479B3EF: dn_expand (res_comp.c:93)
==17413==    by 0x479FD2B: __res_queriesmatch (res_send.c:327)
==17413==    by 0x47A0D19: __libc_res_nsend (res_send.c:1327)
==17413==    by 0x479DDC7: __libc_res_nquery (res_query.c:226)
==17413==    by 0x479E417: __libc_res_nquerydomain (res_query.c:582)
==17413==    by 0x479E8FB: __libc_res_nsearch (res_query.c:416)
==17413==    by 0x404B1D9: _nss_dns_gethostbyname3_r (dns-host.c:192)
==17413==    by 0x404B540: _nss_dns_gethostbyname_r (dns-host.c:273)
==17413==    by 0x42C53FA: gethostbyname_r@@GLIBC_2.1.2 (getXXbyYY_r.c:266)
==17413==    by 0x42C4B7B: gethostbyname (getXXbyYY.c:116)
==17413==  Uninitialised value was created by a stack allocation
==17413==    at 0x804ADA3: UDP_Server_Open(int&, unsigned short) (UDP_Server.cpp:16)

3 个答案:

答案 0 :(得分:1)

对于最简单的方法,获取有关错误的信息是读取类似

的输出
 ==17413==    at 0x47A2349: ns_name_ntop (ns_name.c:147)

看看

  • 文件名(ns_name.c
  • 行号(147
  • 功能名称(ns_name_ntop

并提供错误消息

  

条件跳转或移动取决于未初始化的值

它基本上说,你正在使用一个条件语句,如ifelse if,其表达式包含一个可以具有未初始化值的变量。未初始化数据的来源往往是:

  1. 尚未初始化的过程中的局部变量。
  2. 在你写东西之前,堆块的内容(用malloc()或类似的函数分配)。
  3. 这样,您就可以开始检查所有报告的消息。

    有关更多相关信息,您可以在Valgrind中查看on-line manual memcheck工具。

答案 1 :(得分:0)

这意味着您在语句和if中使用了一个整数变量。

  

条件跳转或移动取决于未初始化的值

意味着您可以执行以下操作:

[...]
int i;
if (i == 0) ...
...

并且

  

使用大小为4的未初始化值

意味着你可能有一个解除引用的单位指针:

struct *p;
p->toto = 0;

答案 2 :(得分:0)

==17413==  Uninitialised value was created by a stack allocation
==17413==    at 0x804ADA3: UDP_Server_Open(int&, unsigned short) (UDP_Server.cpp:16)

这表示第16行的UDP_Server.cpp是对gethostbyname的调用,它被赋予了未初始化的值。