DrVonTrap
DrVonTrap

Reputation: 33

Member mutex causes SegFault

I can't figure out why the code below is causing Segmentation Faults.

If I remove the call to pushLock.lock() and .unlock(), it runs fine.

#include <mutex>
#include <queue>

class FunctionQueue{
public:
    FunctionQueue();
    ~FunctionQueue();
    void pushInt(int);
private:
    std::mutex pushLock;
    int currentPushQueue;
    std::queue<int> instructionQueues[2];
};

FunctionQueue::FunctionQueue(){
    instructionQueues[0] = std::queue<int>();
    instructionQueues[1] = std::queue<int>();
    // pushLock.unlock();
}

FunctionQueue::~FunctionQueue(){}

void FunctionQueue::pushInt(int newArgument){
    pushLock.lock();
    instructionQueues[currentPushQueue].push(newArgument);
    pushLock.unlock();
}

int main(int argc, char* argv[]){
    FunctionQueue testQueue;
    testQueue.pushInt(10);
}

The output from a gdb BackTrace was the very unhelpful:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00007ffff347a291 in ?? () from /lib/x86_64-linux-gnu/libdl.so.2
#2  0x00007ffff347a6d7 in ?? () from /lib/x86_64-linux-gnu/libdl.so.2
#3  0x00007ffff347a198 in dlsym () from /lib/x86_64-linux-gnu/libdl.so.2
#4  0x00007ffff7904b3e in ?? () from /usr/lib/nvidia-331/libGL.so.1
#5  0x00007ffff78e8db4 in ?? () from /usr/lib/nvidia-331/libGL.so.1
#6  0x00007ffff7dea0fd in ?? () from /lib64/ld-linux-x86-64.so.2
#7  0x00007ffff7dea223 in ?? () from /lib64/ld-linux-x86-64.so.2
#8  0x00007ffff7ddb30a in ?? () from /lib64/ld-linux-x86-64.so.2
#9  0x0000000000000001 in ?? ()
#10 0x00007fffffffe8a6 in ?? ()
#11 0x0000000000000000 in ?? ()

Any help you can give would be excellent.

Thanks in advance.

Upvotes: 2

Views: 4018

Answers (1)

Michael A.
Michael A.

Reputation: 1011

Completely remove the commented out code in the constructor of your class as that shouldn't be there in the first place since you haven't locked anything. Problems with this are:

1. You haven't initialized or assigned the member variable 'currentPushQueue' to any value so this code:

 instructionQueues[currentPushQueue].push(newArgument);

is completely wrong unless currentPushQueue is assigned.

2. You aren't using mutexs as they are meant to be used, which is with the provided wrappers (std::unique_lock/std::lock_guard).

Try this code and respond please:

#include <mutex>
#include <queue>

class FunctionQueue
{
public:
    FunctionQueue();
    ~FunctionQueue();
    void pushInt(int);
private:
    std::mutex pushLock;
    int currentPushQueue = 0; // Set this variable somehow
    std::queue<int> instructionQueues[2];
};

FunctionQueue::FunctionQueue()
{
    instructionQueues[0] = std::queue<int>();
    instructionQueues[1] = std::queue<int>();
}

FunctionQueue::~FunctionQueue() {}

void FunctionQueue::pushInt(int newArgument)
{
    std::unique_lock<std::mutex> mutexLock(pushLock);
    instructionQueues[currentPushQueue].push(newArgument);
    // Unlocks automatically
}

int main(int argc, char* argv[])
{
    FunctionQueue testQueue;
    testQueue.pushInt(10);
}

Upvotes: 1

Related Questions