更新:之前的代码无法准确描述我的问题。现在改变了:
以下是我的程序的几行代码(Linux上的C ++)。我的程序经常(但不总是)在LINE_A和LINE_B
行崩溃 class A{
//...
map<int, const string> m_int2StrMap;
//...
void problematic(){
//...insert and erase of m_int2StrMap are involved
char temp[6] = {0};
int key = 12345;
//...temp's elements and key might be changed
map<int, const string>::iterator iter = this->m_int2StrMap.find(key);
if(iter != this->m_int2StrMap.end())
{
/*LINE_A*/ this->m_int2StrMap.erase(iter);
}
/*LINE_B*/ this->m_int2StrMap.insert(pair<int, const string>(key, string ((const char*)temp, 6)));
//...
}
//...
}
我使用gdb来回溯生成的核心转储,有些人给了我这个信息:
(gdb) bt
#0 0x00ae0410 in __kernel_vsyscall ()
#1 0x00138df0 in vfprintf () from /lib/libc.so.6
#2 0x0013a701 in vfprintf () from /lib/libc.so.6
#3 0x0017128b in __wcstod_internal () from /lib/libc.so.6
#4 0x00179595 in ____wcstof_l_internal () from /lib/libc.so.6
#5 0x001799d9 in ____wcstof_l_internal () from /lib/libc.so.6
#6 0x05bfd581 in operator delete () from /usr/lib/libstdc++.so.6
#7 0x05bda14d in std::string::_Rep::_M_destroy () from /usr/lib/libstdc++.so.6
#8 0x08084bf7 in problematic() at /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:233
有些人表示:
(gdb) bt
#0 0x00b81410 in __kernel_vsyscall ()
#1 0x009ccdf0 in vfprintf () from /lib/libc.so.6
#2 0x009ce701 in vfprintf () from /lib/libc.so.6
#3 0x00a0528b in __wcstod_internal () from /lib/libc.so.6
#4 0x00a0e250 in ____wcstof_l_internal () from /lib/libc.so.6
#5 0x00a0fd87 in wcsxfrm_l () from /lib/libc.so.6
#6 0x05bfeab7 in operator new () from /usr/lib/libstdc++.so.6
#7 0x05bda0fb in std::string::_Rep::_S_create () from /usr/lib/libstdc++.so.6
#8 0x05bdaef5 in ?? () from /usr/lib/libstdc++.so.6
#9 0x05bdaff1 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string () from /usr/lib/libstdc++.so.6
#10 0x080843e4 in probblematic() at bad.cpp:LINE_B
上面显示的代码中是否有任何不安全的内容?谢谢