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

m.r*_*226 4 c++ multithreading boost segmentation-fault

当尝试使用最多 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;
                }
            }
Run Code Online (Sandbox Code Playgroud)

toh*_*haz 5

你可以使用 Valgrind,它非常简单。在调试配置中构建您的应用程序并将程序可执行文件传递给 valgrind。它可以告诉您应用程序在运行时发生的广泛编程错误。使用 Valgrind 的代价是程序运行速度比不使用 Valgrind 慢得多(有时慢几十倍)。具体来说,例如,当 Valgrind 尝试第二次释放程序内存时,它会告诉您程序的内存首先被释放的位置。