views:

775

answers:

2

hey, sorry to be a bother, but I need help urgently. I searched for this before I posted this question. I'm getting a segmentation fault when I try to do

pthread_mutex_lock(&_mutex).

This is really odd, I'm not sure what might have caused it. I have initialized _mutex in the constructor with

pthread_mutex_init(&_mutex,NULL).

anything I can do?

+1  A: 

Attach a debugger and find out exactly what is causing the segfault. It is possible some pointer is just pointing into randomness or uninitialized area.

Also run valgrind's memcheck and see what that says.

edit

In response to comments below, the usage of the pthread API sound incorrect somewhere. I recommended "PThread Programming" by O'Reilly as a reference. It is what got me going :) I guessed this because the usage of the API is moving a pointer internal to the pthread_mutex_t struct somewhere dangerous. This should not happen with correct usage of the API.

Aiden Bell
I would, but the Eclipse chose not to cooperate and debugging is not working at the moment :x
Nefzen
You are going to have to. It is unlikely to be a bug in the pthread API ... so that just leaves your code :S :)
Aiden Bell
I suppose.well, I managed to solve the Eclipse bug now, so I'm happy, as much as I can be considering the debugging I have to do now.
Nefzen
Also you can launch 'gdb' and 'valgrind --leak-check=full myprog' from the command-line. Eclipse isn't required for this.
Aiden Bell
:) Good luck with your debugging :) If you are new, debugging does get faster
Aiden Bell
not new, though debugging is rarely fun, unless you find some of your more basic and stupid errors.Thankfully, Eclipse supports threads, so that works. However, it seems that in the creation of a new thread it changes the values a pointer inside of pthread_mutex_t to a place it can't read from.Any ideas now?
Nefzen
At a guess, your pthread usage sounds incorrect. "Pthread Programming" by O'Reilly is a good source.
Aiden Bell
it's like it's copying the mutex but doing it badly. I don't know, should I allocate the mutex on the heap instead of the stack? I seem to do all the right things :S
Nefzen
You can't allocate on the stack. You can store a pointer to a heap allocation. See this: https://computing.llnl.gov/tutorials/pthreads/#CreatingThreads
Aiden Bell
I meant normal allocation, aka using new int* a = int instead of int a;
Nefzen
A: 

solved it, and I am very annoyed at this. I wanted to send a Producer* as an argument to the function the Pthread runs, so I used &(*iter), where iter is an iterator that runs on a producers vector.

little did I notice it was (rightfully) a vector< Producer* >, which meant I've been sending Producer* * which produced undefined results. grrrrr. Obviously, I didn't notice this because Pthreads is in pure C and therefor uses void* as it's only way of accepting any type of arguments.

Nefzen