2013-11-26 63 views
0

我創建了一個「.so」文件,並從我的代碼中調用。它第一次運行得非常好,而且我得到了期望的結果,而當第二次調用它時它會崩潰。以下是我的代碼。我做錯了什麼?第二次調用共享對象崩潰

#include <dlfcn.h> 
#include <stdio.h> /*for printf() */ 
#include <stdlib.h> /* for exit() */ 
#include <FaceRecognition.h> 
#include <string> 

using namespace std; 

typedef void (*pf)(string, string, string); 

int func() 
{ 
void *lib; 
pf greet; 

const char * err; 

    lib=dlopen("/home/libh.so", RTLD_NOW); 

    if (!lib) 
    { 
    printf("failed to open hello.so: %s \n", dlerror()); 
    exit(1); 
    } 
    dlerror(); /*first clear any previous error; redundant 
       in this case but a useful habit*/ 
    greet= (pf) dlsym(lib, "sample");/*locate hello() */ 

    err=dlerror();/*check for errors and copy error message*/ 
    if (err) 
    { 
    printf("failed to locate hello(): %s \n", err); 
    exit(1); 
    } 

    greet("auth", "/home", "/home/train1.gal"); /*call hello() */ 

    dlclose(lib); 

return 0; 
} 


int main() { 
    func(); --> getting the expected result for the first time 
    func(); --> getting crashed here (core dumbed) 

} 
+0

可能是dll不能正常卸載 – Paranaix

+0

@Paranaix如何卸載它? dlclose不會這樣做嗎? – user3021933

+0

我的意思是說,DLL代碼本身是越野車,並沒有正確的deinitialize如果卸載,這只是一個猜測 – Paranaix

回答

0

我認爲你必須使用

lib=dlmopen(LM_ID_NEWLM, "/home/libh.so", RTLD_NOW); 

,如果你想在同一共享庫多次加載。 看看this post

+0

如果我使用「dlmopen」它的第一次調用本身崩潰。 – user3021933

+0

@ user3021933但總的來說,我認爲他是對的。您無法加載您的DLL兩次。 'dlclose()'不會將其卸載IIRC,它只是將當前進程從訪問已加載的映像中退出。 –

相關問題