C++ 集合上的Segfault::insert

C++ 集合上的Segfault::insert,c++,stl,segmentation-fault,C++,Stl,Segmentation Fault,在以下线路之后,我遇到SEG故障: this->my_set.insert(my_string); 该集合已初始化,它包含std::string。 插入字符串不是指一个指针,它以null结尾。 集合中包含的所有其他字符串均不为null,并以“\0”结尾 这是回溯: 标准中的0 0xb7076bdb::_Rb_tree_insert_and_rebalance()来自/usr/lib/libstdc++.so.6 #标准中的1 0xb7480795:_Rb_树::_M_insert_( 在

在以下线路之后,我遇到SEG故障:

this->my_set.insert(my_string);
该集合已初始化,它包含std::string。 插入字符串不是指一个指针,它以null结尾。 集合中包含的所有其他字符串均不为null,并以“\0”结尾

这是回溯:

标准中的0 0xb7076bdb::_Rb_tree_insert_and_rebalance()来自/usr/lib/libstdc++.so.6 #标准中的1 0xb7480795:_Rb_树::_M_insert_( 在/usr/include/c++/4.3/bits/stl_tree.h:854
#std:_Rb_tree::_M_insert_unique(this=0x97bf918,uuu v=@0xaabfc1c4)中的2 0xb7480b96位于/usr/include/c++/4.3/bits/stl_tree.h:1148 #std::set::insert中的3个0xb74fab2d(this=0x97bf918,uux=@0xaabfc1c4) at/usr/include/c++/4.3/bits/stl_set.h:381 有什么想法吗


编辑

更多代码:

set<string> dangerous_messages; //placed in header file

            qpid::client::Message msg;
            msg = (*mylocal_queues)[queue]->get(10*1e9);
            string msgUID = msg.getHeaders().getAsString("UID");
            if(this->dangerous_messages.count(msgUID))
            {
                       warning("(read_local_queue) Duplicate message \""+msgUID+"\" discarded");
           }
           else
           {
                        msg.getHeaders().setString("BID", bid);
                        this->dangerous_messages.insert(msgUID);
            }
设置危险信息//放在头文件中
qpid::client::Message msg;
msg=(*mylocal_队列)[queue]->get(10*1e9);
字符串msgUID=msg.getHeaders().getAsString(“UID”);
如果(此->危险消息计数(msgUID))
{
警告(“(读取本地队列)重复消息\”+msgUID+“\”已丢弃”);
}
其他的
{
msg.getHeaders().setString(“投标”,投标);
此->危险消息。插入(msgUID);
}

使用gdb打印时,我没有注意到集合或字符串中有任何损坏。

您的堆可能已损坏,您的字符串或集合实例可能已损坏。

胡乱猜测:包含
我的集合的对象已超出范围/已被销毁


如果你想得到更精确的答案,你真的需要发布更多的代码。

问题也可能出在其他地方。只要您插入到集合中,程序中某个其他点的溢出就可能导致segfault。插入只是识别错误的时间点,但不一定与错误相关

考虑这个虚构的例子:

vector<MyObj*> ptrs;
if/for/while (...)
{
    MyObj o;
    ptrs.push_back(&o);
}  // end of scopre for o
// Your heap is corrupted, but no segfault has to occur right away
...
// Your insertion occurs somewhere later
// No the program segfaults because of the problem you created earlier
向量ptrs;
if/for/while(…)
{
MyObj o;
PTR.推回(&o);
}//o的scopre结束
//您的堆已损坏,但不必立即发生segfault
...
//您的插入发生在稍后的某个地方
//否:由于您先前创建的问题,程序会出现故障

由于您声明代码是无锁的多线程代码,这可能是竞争条件的结果。(至少一个竞争条件可能会导致完全相同的堆栈跟踪。)

使用适当的锁以确保正确执行。例如,如果您正在使用OpenMP,请将您向我们展示的代码更改为:

#pragma omp critical
{
    if(this->dangerous_messages.count(msgUID))
    {
        warning("(read_local_queue) Duplicate message \""+msgUID+"\" discarded");
    }
    else
    {
        msg.getHeaders().setString("BID", bid);
        this->dangerous_messages.insert(msgUID);
    }
}

但一般来说,如果发生这样的错误,您的代码逻辑从根本上是有缺陷的如果没有干净的跨线程接口,则不能在线程之间共享状态。使用预定义的跨线程通信结构在线程之间共享状态,或使用只读状态。跨线程写入几乎永远不会正常。

请同时发布相关代码。您是否碰巧使用了多线程代码,而这是由竞争条件引起的?哪些已知错误?这是我第一次在std::string中听说它。尝试使用valgrind跟踪内存问题。使用valgrind和hellgrind工具追踪赛道conditions@LoSciamano:那么您的意思是多个线程正在访问该线程,可能是在
插入时同时进行的?如果调用std::string::c_str,返回值将以null结尾。