I am getting a StackOverFlowException from a Visual Studio C# application when calling a C++ function in a separate DLL. Please see attached image.
The exception is caused presumably because the whole of the array dblExptData is being put on the stack.a
Note I am not a C# programmer, this is code belonging to my C# colleague; I am responsible for the C++ DLL he is calling. I too get an exception from a C++ test harness calling this DLL, but I simply increase the stack size in the C++ calling program, and it works fine.
However I don't know how to do this from C#, and nor does he. Any pointers?
Here is the code:
namespace Sample_BO_50_crash
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int[] aPVTcorrelations = new int[4] { 4, 0, 2, 2 };
int iFm = 56;
int iDs = 112;
int iDp = 2128;
double[] afluidData = new double[448] { 365.653948309249, 0, 0.8, 0.85, 0, 0, 0, 0.068947448, 365.653948309249, // I have truncated this for the post
double[] dblWeightFactData = new double[2576] { 1, 1, 10, 0, 0, 0, 0, 1, // I have truncated this for the post
int[] iExptInfo = new int[448] { 1, 0, 6, 19, 1, // I have truncated this for the post
double[] dblExptData = new double[48944]
{ -10000, 404.2611, 33784310.2078, 0 }; // I have truncated this for the post
double[] BoundsData = new double[6] { 50, 50, 50, 50, 50, 50 };
double[] aPVTtuningMults = new double[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int errorcode = (int)BO_Tune.TUNEBOPVTCORRS(aPVTcorrelations, iFm, iDs, iDp, afluidData, iExptInfo, dblExptData, dblWeightFactData, BoundsData, aPVTtuningMults);
}
}
public class BO_Tune
{
public const string strMapTune1 = "BOPVTTune.dll";
// Interface of method to get Tuned results
[DllImport(strMapTune1, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TUNEBOPVTCORRS(int[] aPVTcorrelations, int Ifm, int Ids, int Idp, double[] afluidData,
int[] ExptInfo, double[] ExptData, double[] WeightFactData, double[] BoundsData, double[] aPVTtuningMults);
}
}
The error message is: An unhandled exception of type 'System.StackOverflowException' occurred in Sample_BO_50_crash.exe
Trying to avoid EDITBIN.EXE if possible, would like to do it from the compiler/linker.
Declaration on the C++ side:
_declspec(dllexport) int TUNEBOPVTCORRS(
int* aPVTcorrelations,
int Ifm,
int Ids,
int Idp,
double* afluidData,
int* ExptInfo,
double* ExptData, //output
double* WeightFactData,
double* BoundsData,
double* aPVTtuningMults) // output
It is the ExptData array which is causing the problem.