0

I have a strange issue with vectors, I initialize a class member vector in the main thread and then call a thread which try to access the front() of that vector. But accessing the vector front causes a runtime error

Here is code for main thread (dispatchQueue is a private class member of class Engine)

dispatchQueue.push_back(TempObj);
boost::thread processThread(&Engine::initializeExecutorService, this);
processThread.start_thread();
processThread.join();

And the code for member function initializeExecutorService is as follows (processingQueue is a private class member)

while (nextIterationAvailable) {
    if (pendingProcess) {

        processingQueue.push_back(dispatchQueue.front());
        dispatchQueue.pop_back();
    }
}

The initializeExecutorService works fine if I call it with main thread

UPDATE

The Debugger reports

Signal received: SIGSEGV (Segmentation fault) For program postmaster-cpp-ng-obj, pid 13,614 You may discard the signal or forward it and you may continue or pause the process

When I try to run with Netbeans it reports

RUN FINISHED; Segmentation fault; core dumped;

Running with gdb shows

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff68e8700 (LWP 13821)]
[New Thread 0x7ffff60e7700 (LWP 13822)]
[New Thread 0x7ffff58e6700 (LWP 13823)]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff60e7700 (LWP 13822)]
0x00007ffff79cba1b in ?? () from /usr/lib/libboost_thread.so.1.54.0

16
  • 1
    Please edit your post and add the exact error.
    – Samuel
    Apr 24, 2014 at 14:12
  • 1
    Possibly unrelated, but your code gets the front of the queue, but pops the back? Apr 24, 2014 at 14:15
  • 2
    And how is pendingProcess synchronized?
    – Casey
    Apr 24, 2014 at 14:23
  • 1
    Do the first front call crash? Or are there multiple iterations of the loop before the crash happens? And anyway it looks like a logical error that you should really look at. What if the vector have multiple entries, then you're probably removing the wrong entry. Apr 24, 2014 at 14:27
  • 1
    All we can really tell you without an SSCE is that you probably have a data race somewhere (program works single-threaded, crashes multi-threaded) or a logic error in your belief that nextIterationAvailable && pendingProcess implies !dispatchQueue.empty().
    – Casey
    Apr 24, 2014 at 14:42

2 Answers 2

1

std::vector::front fails when the vector is empty.

This is a safe version:

// lock dispatchQueue
if(!dispatchQueue.empty()) {
    processingQueue.push_back(dispatchQueue.front());
    dispatchQueue.pop_back();
}
// unlock dispatchQueue

Additionally using the first element and removing the last looks dubious.

4
  • Actually this is not safe without locking the dispatchQueue before checking if it is empty or not. Besides I know the dispatchQueue is not empty and it will never be when this code will be called due to the flag pendingProcess
    – Kami
    Apr 24, 2014 at 14:34
  • Additionally using the first element and removing the last looks dubious. At any time there is only one item in the vector so first and last item is the same
    – Kami
    Apr 24, 2014 at 14:39
  • @Kami: 1) It was not thread-safe in the first place. 2) Then why do you need/use a queue?
    – Danvil
    Apr 24, 2014 at 15:12
  • 1) I don't need thread-safety as I am not going to make concurrent changes to the dispatchQueue 2) It's not a queue its a vector queue don't have push_back and pop_back functions they are simply push and pop
    – Kami
    Apr 24, 2014 at 15:14
0

In addition to Danvil's response, I would like to add a note. May be this could help, as once I have faced similar issue in a multithreaded application.

You should also check whether dispatchQueue state is valid or not. In our application another thread was deleting the vector and it was crashing below code snippet.

if(myvector.size)

We have used Windbg to diagnose the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.