在使用boost线程的多线程中出现分段错误(核心转储)

Segmentation fault(core dumped) in multi threading using boost threads

本文关键字:错误 分段 核心 转储 boost 线程 多线程      更新时间:2023-10-16

当尝试用最多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*> (而不是指针)

初始化它

我很惊讶你能编译你的代码。

我认为你应该首先解决这个矛盾。

注。