I have a std::queue I am pushing and popping items to / from. A output threads constantly ask for the oldest buffer (on queue::front()). I wrote a safety function that checks if the queue is not empty, and another function which calls that functions, and thus, if the function returns true (queue is not empty), return front() of the queue, and if the function returns false (queue is empty) it should return a zero-constructed item. Essentially, this is my code:
Expand|Select|Wrap|Line Numbers
- //Somewhere in InPort.h:
- std::shared_ptr<std::queue<DataBlock*>> outputData;
- //Beware, the queue is constructed somewhere else
- bool InPort::checkIfNewBufferAvailable(){
- return !outputData->empty();
- }
- DataBlock* InPort::getNewestBuffer(){
- int qSize = outputData->size();
- if(checkIfNewBufferAvailable()){
- return outputData->front(); //BREAKPOINT ONE
- } else {
- DataBlock* data = new DataBlock(); //BREAKPOINT TWO
- return data;
- }
- }
But when I debugged that code (because of a segfault that follows because I try to access front which apparently doesn't exist), I noticed that breakpoint one is hit. That means checkIfNewBufferAvailable returns true, so the queue shouldn't be empty. But also, when debugging, I noticed that the qSize is -1, so the size of the queue is -1. Why is the queue not empty if it has a size of -1? How can such a size value even exist?
Thanks