e*********k 发帖数: 12 | 1 下面的代码有时候会crash在LINE_A或LINE_B这两行,哪儿不对?
class A{
//...
map 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::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(key,
string ((const char*)temp, 6)));
//...
}
//...
}
用gdb bt coredump,结果如下:
#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
或者
#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, std::
allocator >::basic_string () from /usr/lib/libstdc++.so.6
#10 0x080843e4 in problematic() at bad.cpp:LINE_B |
N******K 发帖数: 10202 | 2 多线程?
【在 e*********k 的大作中提到】 : 下面的代码有时候会crash在LINE_A或LINE_B这两行,哪儿不对? : class A{ : //... : map 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
|
c*******u 发帖数: 1269 | 3 Make pair
★ 发自iPhone App: ChineseWeb 8.7
【在 e*********k 的大作中提到】 : 下面的代码有时候会crash在LINE_A或LINE_B这两行,哪儿不对? : class A{ : //... : map 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
|
n*****u 发帖数: 465 | 4 加个 string 怎么弄得那么别扭?
【在 e*********k 的大作中提到】 : 下面的代码有时候会crash在LINE_A或LINE_B这两行,哪儿不对? : class A{ : //... : map 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
|
k**********g 发帖数: 989 | 5 Please allocate an extra char element for temp (i.e. increase its length by
1), and set it to 0. It doesn't hurt to do so.
In other words, make temp length 7, but pass in the same value of 6 to the
string constructor.
Based on just the code shown above, this suggestion probably shouldn't
matter, because std::string::string(const char*, size_t count) should copy
exactly "count" characters, and should not read any bytes beyond that.
http://www.cplusplus.com/reference/string/string/string/
However, the characters in temp will not necessarily be null-terminated.
This would be the case if you set all 6 elements to non-zero values. If you
pass temp into any function that expects null-terminated string, this will
crash of course. |
A*********l 发帖数: 2005 | 6 如果是多线程的话,在两个线程里同时作map deletion/insersion 就会出问题,
【在 e*********k 的大作中提到】 : 下面的代码有时候会crash在LINE_A或LINE_B这两行,哪儿不对? : class A{ : //... : map 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
|