我面临的问题如下:
我从一个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 发布于 2018-06-16 01:28:04
我认为最好是理解为什么动态共享对象仍然是常驻的,而不是试图强制它卸载。如果动态共享对象上的引用计数降为零,dlclose()将从内存中卸载该对象。你确定你的代码中dlopen()和dlclose()调用的次数是一样的吗?
也有可能引用计数由于其他依赖项而隐式递增。你能确认没有其他东西依赖于你的共享对象吗?
需要注意的是,在构造函数中将NULL传递给dlclose()调用。您是否打算检查handle是否不是NULL,即:
// Closing the library if it has already been opened
if (handle != NULL){
dlclose(handle);
}https://stackoverflow.com/questions/50878631
复制相似问题