在使用boost线程的多线程中出现分段错误(核心转储)
Segmentation fault(core dumped) in multi threading using boost threads
当尝试用最多1个线程运行我的程序时,它可以正常工作一段时间(几秒钟或几分钟),但最终得到分段错误(核心转储)或双重免费(faststop)错误。
下面是线程运行的函数
//used in the Function
[Added] typedef folly::ProducerConsumerQueue<std::string*> PcapTask;
struct s_EntryItem {
Columns* p_packet; //has some arbitrary method and variables
boost::mutex _mtx;
};
//_buffersConnection.wait_and_pop()
Data wait_and_pop() {
boost::mutex::scoped_lock lock(the_mutex);
while (the_queue.empty()) {
the_condition_variable.wait(lock);
}
Data popped_value = the_queue.front();
the_queue.pop();
return popped_value;
}
struct HandlerTask {
std::string year;
folly::ProducerConsumerQueue<std::string*> queue = NULL;
};
-----------------------------------------
//The function which threads run
void Connection() {
std::string datetime, year;
uint32_t srcIPNAT_num, srcIP_num;
std::string srcIP_str, srcIPNAT_str, srcIPNAT_str_hex;
int counter = 0;
while (true) {
//get new task
HandlerTask* handlerTask = _buffersConnection.wait_and_pop();
PcapTask* pcapTask = handlerTask->queue;
year = handlerTask->year;
counter = 0;
do {
pcapTask->popFront();
s_EntryItem* entryItem = searchIPTable(srcIP_num);
entryItem->_mtx.lock();
if (entryItem->p_packet == NULL) {
Columns* newColumn = new Columns();
newColumn->initConnection(srcIPNAT_str, srcIP_str, datetime, srcIP_num);
entryItem->p_packet = newColumn;
addToSequanceList(newColumn);
} else {
bool added = entryItem->p_packet->addPublicAddress(srcIPNAT_str_hex, datetime);
if (added == false) {
removeFromSequanceList(entryItem->p_packet);
_bufferManager->addTask(entryItem->p_packet);
Columns* newColumn = new Columns();
newColumn->initConnection(srcIPNAT_str, srcIP_str, datetime, srcIP_num);
//add to ip table
entryItem->p_packet = newColumn;
addToSequanceList(newColumn);
}
}
entryItem->_mtx.unlock();
++_totalConnectionReceived;
} while (true);
delete pcapTask;
delete handlerTask;
}
}
你可以使用Valgrind,它很简单。在调试配置中构建应用程序,并将程序可执行文件传递给valgrind。它可以告诉你广泛的编程错误发生在你的应用程序在运行时。使用Valgrind的代价是程序运行速度比不使用Valgrind慢得多(有时慢几十倍)。具体来说,例如,Valgrind会告诉你你的程序的内存是在哪里第一次被释放的,当它第二次试图释放它的时候。
我不确定这是问题所在,但是…
你确定一定要叫delete
而不是pcapTask
吗?
我的意思是:你delete
它,但struct HandlerTask
中的queue
是一个类成员,而不是一个指向类的指针。
建议:尝试注释
行delete pcapTask;
在Connection()
结尾
—EDIT—
看着你添加了typedef
,我确认(如果我没有错的话)你的代码中有一些奇怪的东西。
pcapTask
定义为PcapTask
指针,即folly::ProducerConsumerQueue<std::string*>
指针;你用folly::ProducerConsumerQueue<std::string*>
(而不是指针)
我很惊讶你能编译你的代码。
我认为你应该首先解决这个矛盾。
注。
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 为什么我使用 std::copy() 收到运行时错误(分段错误)?
- 为什么我的代码包含错误分段错误(核心转储)?
- 为什么会出现此错误?- 分段错误(核心转储)
- 意外的运行时错误(分段错误)
- C++将文本文件读入数组,运行错误:分段错误
- mmap 错误:分段错误/指针无效错误
- 在我的代码中找不到错误....分段错误
- 错误:分段故障(核心转储)阵列
- 用string.h将错误分段到队列中
- c++字符串流转换错误:分段错误
- 为什么我的代码显示错误分段错误(核心转储)