我的Fortran程序遇到了一个奇怪的问题。以下是我的申请的一些细节:
使用ALLOCATABLE
数据结构来保存信息
与C ++ dll的接口,它返回指向双精度数组的指针。我使用内在方法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的正确方法是什么。
答案 0 :(得分:0)
我认为这是一个MSWindows程序。我猜你有一个名字错误的问题。 Fortran代码编译器在函数名称中添加了一个下划线。有时你的编译器也会加上下划线,所以你的函数BisectionRootFinder
变成BisectionRootFinder_或_BisectionRootFinder _
您应该查看此命令的输出
dumpbin /SYMBOLS MathRoutines.dll
多年前我使用过这个例程。现在我不再拥有MSWindows,所以请不要完全信任命令语法,试验它。