【问题标题】:calling shared object crashes at the second time第二次调用共享对象崩溃
【发布时间】:2013-11-26 16:18:43
【问题描述】:

我创建了一个“.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)

}

【问题讨论】:

  • 可能是dll没有正确卸载
  • @Paranaix 如何卸载它? dlclose 不会这样做吗?
  • 我的意思是 dll 代码本身有问题,如果卸载了也不会正确取消初始化,这只是猜测
  • 使用-g3 -o0 编译所有内容并使用调试器。

标签: c++ c dll shared-libraries


【解决方案1】:

我认为你必须使用

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

如果您想多次加载同一个共享库。 看看this post

【讨论】:

  • 如果我使用“dlmopen”,它会在第一次调用时崩溃。
  • @user3021933 但总的来说,我认为他是对的。您不能两次加载您的 DLL。 dlclose() 不会卸载它 IIRC,它只是释放当前进程访问加载的图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多