Rezaeimh7
Rezaeimh7

Reputation: 1545

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

When try to run my program with up to 1 thread, it works fine for a while (some seconds or minutes) but finally get segmentation fault(core dumped) or double free(faststop ) error.

Here are the function which the threads run.

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

Upvotes: 4

Views: 1127

Answers (2)

max66
max66

Reputation: 66200

I'm not sure that it's the problem, but...

Are you sure that you must call delete over pcapTask?

I mean: you delete it but queue in struct HandlerTask is a class member, not a pointer to a class.

Suggestion: try to comment the line

delete pcapTask;

at the end of Connection()

--- EDIT ---

Looking at you added typedef, I confirm that (if I'm not wrong) there is something strange in your code.

pcapTask is defined as a PcapTask pointer, that is a folly::ProducerConsumerQueue<std::string*> pointer; you initialize it with a folly::ProducerConsumerQueue<std::string*> (not pointer)

I'm surprised that you can compile your code.

I think you should, first of all, resolve this antinomy.

p.s.: sorry for my bad English.

Upvotes: 0

tohaz
tohaz

Reputation: 197

You can use Valgrind, its very easy. Build your app in debug config and pass program executable to valgrind. It can tell you wide spectre of programming errors occuring in your app in runtime. The price of using Valgrind is that program runs considerably slower (some times tens times slower) than without Valgrind. Specically, for example, Valgrind will tell you where your your programs' memory was free'ed first when it tried to free it second time when it happens.

Upvotes: 5

Related Questions