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

时间:2016-06-25 03:29:08

标签: c++ multithreading boost segmentation-fault

当尝试使用最多1个线程运行我的程序时,它可以正常运行一段时间(几秒或几分钟),但最终会出现分段错误(核心转储)或双重自由(快速停止)错误。

以下是线程运行的函数。

        //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;
                }
            }

2 个答案:

答案 0 :(得分:4)

你可以使用Valgrind,非常简单。在debug config中构建应用程序并将程序可执行文件传递给valgrind。它可以告诉您运行时应用程序中出现的编程错误。使用Valgrind的代价是程序运行速度比没有Valgrind慢得多(有时慢几十倍)。特别是,例如,Valgrind会在第二次尝试释放程序的内存时告诉你程序的内存是免费的。

答案 1 :(得分:0)

我不确定这是问题,但是......

您确定必须通过pcapTask致电delete吗?

我的意思是:你是queue但是struct HandlerTask中的delete pcapTask; 是一个类成员,而不是指向类的指针。

建议:尝试评论该行

Connection()

typedef

的末尾

---编辑---

看着你添加pcapTask,我确认(如果我没错),你的代码中有一些奇怪的东西。

PcapTask被定义为folly::ProducerConsumerQueue<std::string*>指针,即folly::ProducerConsumerQueue<std::string*> 指针;您使用 { "results": [ { "alternatives": [ { "confidence": 0.965, "transcript": "how do I raise the self esteem of a child in his academic achievement at the same time " } ], "final": true }, { "alternatives": [ { "confidence": 0.919, "transcript": "it's not me out of ten years of pseudo teaching and helped me realize " } ], "final": true }, { "alternatives": [ { "confidence": 0.687, "transcript": "is so powerful that it can turn bad morals the good you can turn awful practice and the powerful once they can teams men and transform them into angel " } ], "final": true }, { "alternatives": [ { "confidence": 0.278, "transcript": "you know if not on purpose Arteaga Williams who got in my mother " } ], "final": true }, { "alternatives": [ { "confidence": 0.621, "transcript": "for what pink you very much " } ], "final": true } ], "result_index": 0 } 指针)

对其进行初始化

我很惊讶您可以编译代码。

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

p.s:抱歉我的英语不好。