C++ 第二次调用共享对象时崩溃

C++ 第二次调用共享对象时崩溃,c++,c,dll,shared-libraries,C++,C,Dll,Shared Libraries,我已经创建了一个“.so”文件,并从我的代码中调用了它。它第一次运行得非常好,我得到了期望的结果,而当第二次调用相同的so时,它会崩溃。下面是我的代码。我做错什么了吗 #include <dlfcn.h> #include <stdio.h> /*for printf() */ #include <stdlib.h> /* for exit() */ #include <FaceRecognition.h> #include <string&

我已经创建了一个“.so”文件,并从我的代码中调用了它。它第一次运行得非常好,我得到了期望的结果,而当第二次调用相同的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)

}
#包括
#包括/*用于printf()*/
#包含/*用于退出()*/
#包括
#包括
使用名称空间std;
typedef void(*pf)(字符串,字符串,字符串);
int func()
{
void*lib;
pf问候;
常量字符*错误;
lib=dlopen(“/home/libh.so”,RTLD_NOW);
if(!lib)
{
printf(“无法打开hello.so:%s\n”,dlerror());
出口(1);
}
Dleror();/*首先清除以前的任何错误;冗余
在这种情况下,这是一个有用的习惯*/
greet=(pf)dlsym(lib,“sample”);/*定位hello()*/
err=dlerror();/*检查错误并复制错误消息*/
如果(错误)
{
printf(“未能找到hello():%s\n”,错误);
出口(1);
}
问候(“auth”、“/home”、“/home/train1.gal”);/*打招呼()*/
dlclose(lib);
返回0;
}
int main(){
func();-->第一次获得预期结果
func();-->在此处崩溃(核心已哑)
}

我想你必须使用

lib=dlmopen(LM_ID_NEWLM, "/home/libh.so", RTLD_NOW);
如果要多次加载同一共享库。
看看

可能是dll没有卸载properly@Paranaix如何卸下它?dlclose不会这样做吗?我的意思是dll代码本身有缺陷,如果卸载,就不能正确地去初始化,这只是一个猜测。用
-g3-o0
编译所有东西并使用调试器。如果我使用“dlmopen”,第一次调用本身就会崩溃。@user3021933但一般来说,我认为他是对的。您不能加载DLL两次
dlclose()
不会将其卸载IIRC,它只是将当前进程从访问加载的映像中释放出来。