0

我面临的问题如下:

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

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

customkinetics.so: customkinetics.f90
    gfortran -c customkinetics.f90 -g -fPIC -o customkinetics.o
    gfortran -shared -o customkinetics.so customkinetics.o   
4

1 回答 1

0

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

由于其他依赖关系,引用计数也可能隐式增加。你能确认没有其他东西取决于你的共享对象吗?

在相关说明中,您将传递NULLdlclose()构造函数中的调用。您是否打算检查handleis not NULL,即:

// Closing the library if it has already been opened
if (handle != NULL){
    dlclose(handle);
}
于 2018-06-15T17:28:04.440 回答