我創建了一個「.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)
}
可能是dll不能正常卸載 – Paranaix
@Paranaix如何卸載它? dlclose不會這樣做嗎? – user3021933
我的意思是說,DLL代碼本身是越野車,並沒有正確的deinitialize如果卸載,這只是一個猜測 – Paranaix