字符串赋值中的代码转储

时间:2012-01-30 11:05:28

标签: c++ multithreading string crash centos

我将在以下部分代码中获得核心转储:

void Debug::writeToFile()
{
 _ptrMutex->getLock();
 write(_fd,_cacheStr.c_str(),_cacheStr.size());
 _cacheStr = ""; //flush the write string
 _ptrMutex->releaseLock();
}

核心发生一次,堆栈转储如下

Thread 1 (Thread 8426):
#0  0x00a2a402 in __kernel_vsyscall ()
#1  0x0072bdf0 in raise () from /lib/libc.so.6
#2  0x0072d701 in abort () from /lib/libc.so.6
#3  0x0545651a in ?? () from /usr/lib/libstdc++.so.6
#4  0x05456552 in std::terminate() () from /usr/lib/libstdc++.so.6
#5  0x0545668a in __cxa_throw () from /usr/lib/libstdc++.so.6
#6  0x053ed1ef in std::__throw_length_error(char const*) () from /usr/lib/libstdc++.so.6
#7  0x0543211d in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) () from /usr/lib/libstdc++.so.6
#8  0x05433e28 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned int, unsigned int, unsigned int) () from /usr/lib/libstdc++.so.6
#9  0x05433fca in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace_safe(unsigned int, unsigned int, char const*, unsigned int) () from /usr/lib/libstdc++.so.6
#10 0x05434065 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign(char const*, unsigned int) () from /usr/lib/libstdc++.so.6
#11 0x0815e9a8 in Debug::writeToFile() ()
#12 0x08161866 in Debug::LOG_PRINT_ERROR(char*, ...) ()
#13 0x0812bcc6 in DimInternalMsgHandler::handlePeerStatusIndication(DimPeerStatusInd*) ()
#14 0x0812c52a in DimInternalMsgHandler::handleInternalMessage(unsigned char*, int) ()
#15 0x0812aa05 in DimDanIfController::handleInMessage(NwPacket&) ()

1 个答案:

答案 0 :(得分:1)

我怀疑问题出在writeToFile()本身。

我可以看到几种可能性:

  1. 第一种可能是_cacheStr已损坏,可能是由于其他地方的内存错误。

  2. 第二种可能性是另一个线程同时修改了_cacheStr。我可以看到writeToFile()受互斥锁保护,但_cacheStr可以修改的任何其他地方也需要这样做。