我的节目是一种非常令人费解的方式。它发生在std :: string库代码中,所以我假设我的程序在其他地方覆盖了std :: string代码。当我尝试将char *分配给自动变量中的std:string时,会发生段错误:
struct MyStruct
{
std::string name;
int winch_ndx;
...
};
void MyFunction(const char * nodeName)
{
MyStruct dataL;
dataL.name = nodeName; <-- segfault
...
}
由于dataL是一个自动变量,代码的其他部分无法覆盖它占用的内存,所以我猜库库代码本身被 0x6C2FD8 覆盖(见下文)。令人费解的是,Valgrind根本没有检测到原始的无效写入。我不知道如何将这个0x6C2FD8引入到std :: string代码中。任何见解都将不胜感激。
Valgrid输出和我的valgrind命令如下。
== 17112 ==使用信号11(SIGSEGV)的默认操作终止进程
== 17112 ==地址 0x6C2FD8
的映射区域的权限不佳== 17112 ==在0x9B07D87:__ exchange_and_add(atomicity.h:47)
== 17112 == by 0x9B07D87:__ exchange_and_add_dispatch(atomicity.h:80)
== 17112 == by 0x9B07D87:std :: string :: _ Rep :: _ M_dispose(std :: allocator const&amp;)[clone .part.12](basic_string.h:246)
== 17112 == by 0x9B07F78:_M_dispose(char_traits.h:243)
== 17112 == by 0x9B07F78:std :: string :: _ M_mutate(unsigned long,unsigned long,unsigned long)(basic_string.tcc:487)
== 17112 == by 0x9B083ED:std :: string :: _ M_replace_safe(unsigned long,unsigned long,char const *,unsigned long)(basic_string.tcc:684)
== 17112 == by 0x613ADA:MyFunction(comm.cpp:1108)
valgrind --gen-suppressions=all --error-markers=BEGIN_MARK,END_MARK --free-fill=FF --malloc-fill=FF --trace-malloc=yes --trace-children=yes --track-origins=yes --read-var-info=yes --partial-loads-ok=no --log-file=vg2.out my_program
答案 0 :(得分:3)
检查nodeName是否为NULL,同时检查nodeName是否实际指向&#39; \ 0&#39;终止字符串......