I have a project using pthreads; there's the main thread and a sub-thread and a pipe between them. It's all working fine, except sometimes it doesn't work. The sub thread runs a command interpreter and an ncurses-based GUI shuttles (some) input over the pipe to it.
I create the thread normally (thread is a pthread_t file-scope variable, interp_start is the function)
if (pthread_create(&thread, NULL, interp_start, NULL)) { perror("couldn't create thread"); return; }
Then, if the interpreter thread receives an "exit" command from the user, it calls interp_exit
fclose(output);
pthread_exit(NULL);
The main thread has a select() that examines, among other things, output's FD and calls a function that read()s from the FD:
int num=read(interp_output[0], &ch, 1);
if (num==0) shell_done();
if (num==-1) perror("read");
The intended behavior, which often works, is to close the FILE* in the thread, which makes the select() report ready, which makes the read() happen, which returns 0, which calls shell_done(). This does, after some simple and unrelated cleanup:
//fprintf(stderr, "joining thread\n");
pthread_join(thread, NULL);
//fprintf(stderr, "joined\n");
exit(EXIT_SUCCESS);
All of this sometimes segfaults. Usually it's fine. If I uncomment those two printfs, if it fails I get neither (it segfaults in pthread_exit) or just the first (it segfaults in pthread_join).
I'm not messing with 'thread' at any other point, and I'm only dealing with null pointers. What's the deal? I'd be looking elsewhere except I'm consistently having pr开发者_运维百科oblems at one of those two lines - once even at restore_sem_to_pool. I think it has to be the way I'm killing the thread, but I'm doing just about the simplest thing possible.
Thanks in advance...
Try using valgrind
(specifically the "memcheck" part). It can quickly help you to pinpoint invalid memory accesses at runtime, sometimes even including runs of your program that do not crash.
I suspect it's a combination of all of these, but the problem went away. It only happened when I typed 'quit' too soon after the program started, but I don't have time to properly debug this now and the assignment is due already anyway. @John - I actually was setting up valgrind for just this purpose. [EDIT] I ran it earlier and it didn't help me with this problem, but I definitely found some memory I wasn't freeing [/EDIT] Everybody else - I think you're right, it's probably some sort of memory corruption. @caf - I forgot to check other thread... but I was seeing the problem in both threads. GDB was pointing me at the one that was actually segfaulting. [EDIT] I checked the other threads and it was either waiting on the pt_join (if the segfault was with the pt_exit) or somewhere just before the pt_exit (if the segfault was in the pt_join) [/EDIT]
Votes to all, but I don't think it's fair to accept an answer here.
精彩评论