【问题标题】:code dump in string assignment字符串赋值中的代码转储
【发布时间】:2012-02-22 04:29:44
【问题描述】:

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

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&) ()

【问题讨论】:

  • 核心转储是由未捕获的异常引起的。您认为您可以将函数包装在异常处理程序中并打印 .what() 消息吗?至于为什么会发生异常,我不知道 - 但最有可能的是,当您尝试写入字符串对象时,您的字符串对象无效。我建议使用 Valgrind 等内存调试器。
  • 看libstdc++中的代码,异常信息大概就不多说了。但是抛出的异常表明你的字符串分配中所需的新字符串缓冲区的请求大小大于字符串的最大大小,除非你在某处损坏了内存,否则这是没有意义的。
  • 如果您没有立即得到答复,请不要转发。
  • 我们无法重现此问题。我们正在努力重现它。

标签: c++ multithreading string crash centos


【解决方案1】:

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

我可以看到几种可能性:

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

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

【讨论】:

  • 有Debug类成员函数,所有函数在对其数据成员进行任何操作之前都使用互斥锁。
猜你喜欢
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多