- 论坛徽章:
- 0
|
一个多线程linux程序,其中有这样一个函数,
string PickInCAllNum()
{
int index=-1;
if(vBoardIni[0].InCallPhoneNum.size()>;0)
{
struct timeb tp;
int count=0;
while(count++<10)
{
ftime(&tp);
srand(tp.time);
index=(int)(vBoardIni[0].InCallPhoneNum.size()*rand()/(RAND_MAX+1.0));
if((index>;=0)&&(index<vBoardIni[0].InCallPhoneNum.size()))
break;
}
return (count<10)?vBoardIni[0].InCallPhoneNum[index]:vBoardIni[0].InCallPhoneNum[0];
}
else
{
string temp="-1";
return temp;
}
}
结果用gdb跟踪到出错如下:
(gdb) c
Continuing.
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 108651 (LWP 26524)]
0x420828fc in memcpy () from /lib/i686/libc.so.6
(gdb) bt
#0 0x420828fc in memcpy () from /lib/i686/libc.so.6
#1 0x40059ce3 in string_char_traits<char>;::copy () from /usr/lib/libstdc++-libc6.2-2.so.3
#2 0x08056beb in basic_string<char, string_char_traits<char>;, __default_alloc_template<true, 0>; >;::Rep::copy (
this=0xaf135008, pos=0,
s=0x8185788 "\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b\020A \b°\\\030\b"..., n=136331536) at /usr/include/g++-3/std/bastring.cc:139
#3 0x080567cf in basic_string<char, string_char_traits<char>;, __default_alloc_template<true, 0>; >;::Rep::clone (
this=0x818577 at /usr/include/g++-3/std/bastring.cc:72
#4 0x0805625a in basic_string<char, string_char_traits<char>;, __default_alloc_template<true, 0>; >;::Rep::grab (
this=0x818577 at /usr/include/g++-3/std/bastring.h:75
#5 0x08055fdd in basic_string<char, string_char_traits<char>;, __default_alloc_template<true, 0>; >;::basic_string (
this=0x75933a3c, str=@0x818b16 at /usr/include/g++-3/std/bastring.h:177
#6 0x080679d8 in PickInCAllNum () at ../src/BootBoard.cpp:105
#7 0x080735da xxxxxxxxxxxxxxx
这个崩溃的错误并不是每次都发生,我做了个测试
for(int i=0;i<65535;i++)
{
string InPhoneNum=PickInCAllNum();
CDebugPrint("pick up a phone num:"<<InPhoneNum.c_str()<<" for Incall!" ;
}
程序运行了几次都没崩溃,有几次偶然测试几次就会出现这个崩溃错误,各位高手有什么可以指点一二的? |
|