2014-10-10 48 views
-1

我正在使用pthread_create從共享庫中使用函數。我收到Segmenation故障下面的代碼執行後:pthread_create中的分段錯誤

#include <stdlib.h> 
#include <stdio.h> 
#include <dlfcn.h> 
#include<string.h> 
#include<pthread.h> 
#include<unistd.h> 

void (*GetVersion)(char *version); 

void* draw(void *arg) 
{ 
    void *handle; 
    char *error;  

    handle = dlopen ("libOpenKaillera.so", RTLD_LAZY); 
    if (!handle) { 
     fputs (dlerror(), stderr); 
     exit(1); 
    } 

    GetVersion = dlsym(handle, "GetVersion"); 
    if ((error = dlerror()) != NULL) { 
     fputs(error, stderr); 
     exit(1); 
    } 

    char version[4]; 
    kailleraGetVersion(version); 
    puts(version); 
    dlclose(handle); 
    return NULL; 
} 

int main(void) 
{ 
    pthread_t tid; 

    pthread_create(&tid, NULL, &draw, NULL);   
    sleep(5000); 
    return 0; 
} 

回溯命令說以下內容:

#0 0xb68e7be0 in ??() 
#1 0xb7fa9d56 in __nptl_deallocate_tsd() at pthread_create.c:158 
#2 0xb7fa9f83 in start_thread (arg=0xb7df0b40) at pthread_create.c:325 
#3 0xb7ede4ce in clone() at ../sysdeps/unix/sysv/linux/i386/clone.S:129 

我不明白什麼原因可能導致此。你能幫我嗎?

+0

甚至沒有回溯,不,我們不可能幫助你。你有沒有看過調試器呢? – bmargulies 2014-10-10 21:14:18

+0

我在問題 – spandei 2014-10-10 21:46:09

+0

中添加了回溯輸出你用什麼編譯和鏈接選項? – bmargulies 2014-10-11 00:55:02

回答

1

每次撥打pthread_create後,請記得致電pthread_joinpthread_detach告訴線程如何執行終止。通常,在退出創建線程之前請致電pthread_join(在這種情況下,它是功能main)。

+0

我在主中添加了pthread_join,但問題仍然存在。此外,如果我刪除dlclose調用一切正常 – spandei 2014-10-11 10:31:23

+0

@spandei它可能不是一個線程問題,但一個'dlclose'的問題。如果加載的函數在終止時帶來了一些問題,可能會產生這樣的問題。你可以嘗試加載一個最簡單的'.so'文件來調查它。 – 2014-10-12 09:56:03