我的 Fortran 程序有一个奇怪的问题。以下是有关我的应用程序的一些详细信息:
使用
ALLOCATABLE
数据结构来保存信息与 C++ dll 接口,该 dll 返回指向双精度数组的指针。我使用内部方法将 C++ 数组指针转换为 Fortran 指针
c_f_pointer
从 XML 文件读取并将输出写入 XML 文件和两个文本文件。
我gfortran
用来编译 Fortran 和Simply Fortran IDE。在 C++ 方面,我正在使用gcc
和code::Blocks。应用程序构建没有任何问题,并在调试模式下运行。
但是,当我双击应用程序可执行文件以查看这样的启动会做什么时,它没有响应。我不得不去任务管理器并杀死该程序。我尝试了多次,每次都发生这种情况。我删除了应用程序和 gmon.out 创建的输出文件,然后双击可执行文件,瞧,程序再次运行。
它可能会运行几次并随机冻结。然后我重复上述过程,有时这会使应用程序恢复活力。我真的很迷茫这里有什么问题。这是一个糟糕的内存管理过程吗?这与 C++ 指针和 Fortran 指针的处理方式有关吗?
我认为这可能是调试版本的问题,所以我将项目更改为不包含调试变量的选项,但问题仍然存在。
如果需要,我可以尝试制作一个简单的程序来演示该问题。
非常感谢任何想法/帮助/建议。
*新的信息
我尝试按照评论中的要求创建一个小型工作示例,同时我解决了这个问题。但是我不知道它解决的原因。所以为了更好地理解它,我把我的 CPP 头文件和源文件放在这里:
DLL.h
#ifndef DLL_H_INCLUDED
#define DLL_H_INCLUDED
#include <windows.h>
#include <stdio.h>
#include <math.h>
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#endif
extern "C"
{
double DLL_EXPORT __cdecl *Function1(int InputCombination, double Input1, double Input2, double Pressure);
double DLL_EXPORT __cdecl Function2(double DBTemperature, double WBTemperature, double Pressure);
double DLL_EXPORT __cdecl Function3(double DBTemperature, double WBTemperature, double Pressure);
double DLL_EXPORT __cdecl Function4(double DBTemperature, double WBTemperature, double Pressure);
double DLL_EXPORT __cdecl Function5(double DBTemperature, double WBTemperature, double Pressure);
double DLL_EXPORT __cdecl Function6(double DBTemperature, double WBTemperature, double Pressure);
double DLL_EXPORT __cdecl Function7(double DBTemperature, double WBTemperature, double Pressure);
double DLL_EXPORT __cdecl Function8(double DBTemperature, double RelHumidity, double Pressure,int LoadLibraryFlag=1);
double DLL_EXPORT __cdecl Function9(double DBTemperature, double HumRatio, double Pressure);
double DLL_EXPORT __cdecl Function10(double DBTemperature, double Enthalpy, double Pressure);
}
double DB,WB,HR,RH,DP,SpVOL,ENTH;
const double CtoK = 273.15;
const double KtoC = -273.15;
#endif // DLL_H_INCLUDED
DLL.cpp
#include "DLL.h"
typedef double (__cdecl *fp_BisectionRootFinderTYPE) (double, double, double, double, double*, int, double(*)(double,double*));
extern fp_BisectionRootFinderTYPE BisectionRootFinder;
fp_BisectionRootFinderTYPE BisectionRootFinder;
double DLL_EXPORT __cdecl *CalcAirProperties(int InputCombination, double Input1, double Input2, double Pressure)
{
/*
Input Combination:
1: Given DB [C], WB [C] and Pressure [kPa]
2: Given DB [C], RH and Pressure [kPa]
3: Given DB [C], phi and Pressure [kPa]
4: Given DB [C], DP [C] and Pressure [kPa]
*/
double *AirProperties = new double[7];
/*
Air Properties:
[0]: Dry bulb Temperature [C]
[1]: Wet bulb temperature [C]
[2]: Humidity ratio []
[3]: Relative Humidity []
[4]: Dew point Temperature [C]
[5]: Specific Volume [m3/kg_da]
[6]: Enthalpy [kJ/kg_da]
*/
HINSTANCE MathRoutinesInstance;
MathRoutinesInstance = LoadLibrary("./MathRoutines.dll");
BisectionRootFinder = (fp_BisectionRootFinderTYPE) GetProcAddress(MathRoutinesInstance,"BisectionRootFinder");
if(InputCombination == 1)
{
DB = Input1;
WB = Input2;
HR = Function2(DB,WB,Pressure);
RH = Function3(DB,WB,Pressure);
DP = Function4(DB,WB,Pressure);
SpVOL = Function5(DB,WB,Pressure);
ENTH = Function6(DB,WB,Pressure);
}
else if(InputCombination == 2)
{
DB = Input1;
RH = Input2;
WB = Function8(DB,RH,Pressure,1);
HR = Function2(DB,WB,Pressure);
DP = Function4(DB,WB,Pressure);
SpVOL = Function5(DB,WB,Pressure);
ENTH = Function6(DB,WB,Pressure);
}
else if(InputCombination == 3)
{
DB = Input1;
HR = Input2;
RH = Function7(DB,HR,Pressure);
WB = Function8(DB,RH,Pressure,1);
DP = Function4(DB,WB,Pressure);
SpVOL = Function5(DB,WB,Pressure);
ENTH = Function6(DB,WB,Pressure);
}
else if(InputCombination == 4)
{
DB = Input1;
ENTH = Input2;
HR = HumRatfn_DB_Enthalpy_Pressure(DB,ENTH,Pressure);
RH = RelHumidityfn_DB_HumRat_Pressure(DB,HR,Pressure);
WB = WBTempfn_DB_RH_Pressure(DB,RH,Pressure,1);
DP = Function4(DB,WB,Pressure);
SpVOL = Function5(DB,WB,Pressure);
}
else
{
DB = Input1; // + KtoC
WB = Input2; // + KtoC
HR = HumRatfn_DB_WB_Pressure(DB,WB,Pressure);
RH = RelHumidityfn_DB_WB_Pressure(DB,WB,Pressure);
DP = DewPointTempfn_DB_WB_Pressure(DB,WB,Pressure);
SpVOL = SpecVolumefn_DB_WB_Pressure(DB,WB,Pressure);
ENTH = Enthalpyfn_DB_WB_Pressure(DB,WB,Pressure);
}
AirProperties[0] = DB;
AirProperties[1] = WB;
AirProperties[2] = HR;
AirProperties[3] = RH;
AirProperties[4] = DP;
AirProperties[5] = SpVOL;
AirProperties[6] = ENTH;
FreeLibrary(MathRoutinesInstance);
return AirProperties;
}
double DLL_EXPORT __cdecl Function8(double DBTemperature, double RelHumidity, double Pressure,int LoadLibraryFlag) //
{
/*
Input:
DBTemperature - Dry bulb Temperature [C]
RelHumidity - Relative Humidity [-]
Pressure - Barometric Pressure [kPa]
Output:
WBTemperature- Wet bulb Temperature [C]
Reference:
ASHRAE Fundamentals (SI) - Chapter 1
*/
bool Converged;
bool RHBounded;
double WBTempHi;
double WBTempLo;
double WBTemperature;
double RHCalc_Lo;
double Params[2];
Converged = false;
WBTempHi = DBTemperature;
WBTempLo = DBTemperature - 4.0f;
HINSTANCE MathRoutinesInstance;
if(LoadLibraryFlag == 0)
{
MathRoutinesInstance = LoadLibrary("./MathRoutines.dll");
BisectionRootFinder = (fp_BisectionRootFinderTYPE) GetProcAddress(MathRoutinesInstance,"BisectionRootFinder");
}
// Do some calculations here
if(Converged == true)
WBTemperature = WBTemperature;
else
{
Params[0] = DBTemperature;
Params[1] = Pressure;
WBTemperature = BisectionRootFinder(WBTempLo,WBTempHi,RelHumidity,0.0005,Params,50,RelHumidityfn_WB_Params);
}
if(LoadLibrary == 0)
{
FreeLibrary(MathRoutinesInstance);
}
return WBTemperature;
}
您会注意到,上述 dll 引用了另一个 dll“MathRoutines.dll”来进行计算。在 Function8 中,我有一个 if 块来加载 Mathroutines dll。这是因为我将在我的 C# 接口程序中使用 dll,其中将直接调用 function8。由于 Function 8 使用 MathRoutines.dll 中的函数,因此我需要在直接调用时加载它。
通过这种设置,程序遇到了我在原始帖子中提到的问题。我意识到冻结与文件无关。现在,作为一个简单的工作示例的尝试,我在 Function8 中注释了以下几行:
/*
HINSTANCE MathRoutinesInstance;
if(LoadLibraryFlag == 0)
{
MathRoutinesInstance = LoadLibrary("./MathRoutines.dll");
BisectionRootFinder = (fp_BisectionRootFinderTYPE) GetProcAddress(MathRoutinesInstance,"BisectionRootFinder");
}
*/
和
/*
if(LoadLibrary == 0)
{
FreeLibrary(MathRoutinesInstance);
}
*/
此外,我还必须在 Function1 的末尾注释 FreeLibrary 调用。通过上述更改,应用程序可以正常工作。我在这里没有包含 FORTRAN 代码,因为问题似乎与我如何在 C++ 中加载 MatRoutines 库有关。
我想知道我上面显示的行的评论是如何使它起作用的。加载/卸载 DLL 的正确方法是什么。