首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >卸载在C++中使用dlopen()打开的共享对象(.so文件

卸载在C++中使用dlopen()打开的共享对象(.so文件
EN

Stack Overflow用户
提问于 2018-06-15 23:28:09
回答 1查看 543关注 0票数 0

我面临的问题如下:

我从一个f90文件生成一个名为customkinetics.so的共享对象,我在一个用C++编写的带有python接口的Cantera (化学代码)中使用这个对象。在子例程(CustomKinetics.cpp)中调用该对象,如下所示:

代码语言:javascript
复制
#include "cantera/kinetics/CustomKinetics.h"

#include <iostream>
#include <dlfcn.h>

using namespace std;

namespace Cantera 
{
  //Fortran External Routine
  extern "C"
  { 
    void customkinetics_(doublereal* P, doublereal* T, doublereal* rho, const doublereal* m_y, doublereal* wdot);
  }

  CustomKinetics::CustomKinetics(thermo_t* th) : GasKinetics(th)
  {
    printf("WARNING: Using customized kinetics from f90 file.\n");
    // Closing the library if it has already been opened
    if (handle == NULL){
        dlclose(handle);
    }
    handle = dlopen("customkinetics.so", RTLD_LAZY | RTLD_LOCAL);
    // load symbol
    ck = (ck_t) dlsym(handle, "customkinetics_");
  }

    void CustomKinetics::get_wdot_custom(doublereal* wdot)
  {

    doublereal P = thermo().pressure();
    doublereal T = thermo().temperature();
    doublereal rho = thermo().density();
    const doublereal* m_y = thermo().massFractions();

    // calculation
    ck(&P,&T,&rho,&m_y[0],&wdot[0]);
  }
}

它工作得很好,直到我重写了.f90文件,然后我再次编译该文件,从而重写了.so,然后当我再次调用它时,我实际上调用了第一个对象。

我知道dlclose()不应该从内存中完全删除对象,但是有什么方法可以做到吗?

fortran Makefile:

代码语言:javascript
复制
customkinetics.so: customkinetics.f90
    gfortran -c customkinetics.f90 -g -fPIC -o customkinetics.o
    gfortran -shared -o customkinetics.so customkinetics.o   
EN

回答 1

Stack Overflow用户

发布于 2018-06-16 01:28:04

我认为最好是理解为什么动态共享对象仍然是常驻的,而不是试图强制它卸载。如果动态共享对象上的引用计数降为零,dlclose()将从内存中卸载该对象。你确定你的代码中dlopen()dlclose()调用的次数是一样的吗?

也有可能引用计数由于其他依赖项而隐式递增。你能确认没有其他东西依赖于你的共享对象吗?

需要注意的是,在构造函数中将NULL传递给dlclose()调用。您是否打算检查handle是否不是NULL,即:

代码语言:javascript
复制
// Closing the library if it has already been opened
if (handle != NULL){
    dlclose(handle);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50878631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档