简体   繁体   English

调用共享对象在第二次崩溃

[英]calling shared object crashes at the second time

I have created a ".so" file and invoked from my code. 我创建了一个“ .so”文件,并从我的代码中调用了该文件。 It works very well for the first time, and I am getting the desired result, whereas when the same so is invoked for the second time its getting crashed. 第一次效果很好,我得到了预期的结果,而第二次调用相同的结果时,它崩溃了。 The below is my code. 下面是我的代码。 Am I doing anything wrong. 我做错什么了吗?

#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)

}

I think you have to use 我想你一定要用

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

if you want to load the same shared library multiple times. 如果要多次加载相同的共享库。 Take a look at this post 看看这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM