gpt4 book ai didi

c++ - 将空字符串分配给字符串变量时面临核心转储

转载 作者:行者123 更新时间:2023-11-28 08:11:03 25 4
gpt4 key购买 nike

当我的应用程序试图将文本写入文件时,我遇到了核心转储。它正在使用代码中所示的字符串操作。当多个线程尝试调用此函数并尝试写入日志文件时,我的应用程序正在转储核心。我浏览了不同的论坛并尝试搜索字符串分配中的任何问题。我看到我们中的一些人对那个字符串赋值语句发表了评论,因为它不好用。而是使用字符串成员函数。

功能:

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

缓存写入字符串

bool Debug::cacheWriteString(string strIn)
{
if(_ptrMutex == NULL)
_ptrMutex = new CndMutex();
_ptrMutex->getLock();
char timeStamp[100];

struct timeval tv;
struct tm tm;

if (gettimeofday(&tv, NULL)==0 && localtime_r((time_t*)&(tv.tv_sec), &tm)) {
int ret = strftime(timeStamp,sizeof(timeStamp),"%F-%T",&tm);
snprintf(timeStamp+ret, sizeof(timeStamp)-ret, ".%03ld", tv.tv_usec/1000);
} else
Time::getTimeStamp(timeStamp,100);
_cacheStr = _cacheStr + "[" + timeStamp + "] " + strIn;
if(_cacheStr.size() >= _maxCacheSize)
{
_ptrMutex->releaseLock();
return true;
}
_ptrMutex->releaseLock();
return false;
}

这个函数被称为

void Debug::LOG_PRINT_ERROR(char* ptrFormat,...)
{
va_list argList;
va_start(argList,ptrFormat);

if(_logType == CONSOLE_LOG) //console log
{
cout<<"#### [ERROR] ";
vprintf(ptrFormat,argList);
cout<<endl;
}
else if(_logType == SYS_LOG)// [syslog]
{
syslog(LOG_ERR,"%s","[ERROR]####");
vsyslog(LOG_ERR,ptrFormat,argList);
}
else if(_logType == FILE_LOG)
{
char str[1024];
//SBYTE4 ret = vsprintf(str,ptrFormat,argList);
SBYTE4 ret = vsnprintf(str,1023,ptrFormat,argList);
str[1023]='\0';
if(ret > -1)
{
string dataStr = "[ERROR]####";
dataStr = dataStr +str+"\n";
if(cacheWriteString(dataStr))
{
if(chkFile() == DIM_PASS)
{
writeToFile();
}
else
{
cout<<"#### LOG_ERROR() Unable to Write Data To File: "<<_fileName <<endl;
}
}
}
else
{
cout<<"#### LOG_ERROR() Output Error Is Encountered: "<<_fileName <<endl;
}
}
va_end(argList);

核心转储详情如下:

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&) ()
#16 0x081ab28f in WorkerThreadPool::onEvent(int, nxs_util::EventMessage64*) ()
#17 0x0021dd94 in nxs_util::EventHandler64::workerProc(int) () from /home/nextgen/NXS/lib/libnxsutil.so
#18 0x001c193e in nxs_util::WorkerThread::run() () from /home/nextgen/NXS/lib/libnxsutil.so
#19 0x001c5b36 in nxs_util::Thread::__Thread_run(void*) () from /home/nextgen/NXS/lib/libnxsutil.so
#20 0x0087f832 in start_thread () from /lib/libpthread.so.0
#21 0x007d4e0e in clone () from /lib/libc.so.6

我们怀疑我们在代码中使用的 <_cacheStr = "";> 语句存在问题。我们正在考虑更改为 _cacheStr.clear();但是我们现在无法复制这个内核。

如果有人对此有任何想法或遇到过它,请告诉我们。

非常感谢您的投入。

最佳答案

这看起来真的非常狡猾:

if(_ptrMutex == NULL)
_ptrMutex = new Mutex();

如果两个线程进入这个 block 并动态创建它们自己的互斥体会怎样?有什么理由不能在初始化时创建单个互斥量并避免“创建互斥量”竞争条件?

关于c++ - 将空字符串分配给字符串变量时面临核心转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9061539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com