0

I am getting a StackOverFlowException from a Visual Studio C# application when calling a C++ function in a separate DLL. Please see attached image.

enter image description here

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.

Adrian
  • 267
  • 2
  • 9
  • 1
    Post relevant code here and also show the exact *text* of the error message. – crashmstr Jan 30 '15 at 13:02
  • 2
    See this http://stackoverflow.com/questions/2556938/how-to-change-stack-size-for-a-net-program – Theodoros Chatzigiannakis Jan 30 '15 at 13:05
  • 1
    so many magic numbers... – DLeh Jan 30 '15 at 13:23
  • No, that array is not "put on the stack", the pinvoke marshaller simply pins it and passes a pointer. You surely need help from the C++ code owner to get the best advice, trickorama to increase the stack size (maybe that works) are covered in [this post](http://stackoverflow.com/a/28216767/17034). – Hans Passant Jan 30 '15 at 13:27
  • That post refers to arrays declared on the C++ side. This array is declared and filled on the C# side- all that data needs to be passed into the C++ function. I am the C++ code owner (or at least I am responsible for it now). The declaration on the C++ side is shown above. – Adrian Jan 30 '15 at 13:49
  • Please don't show pictures of exceptions. Look on that dialog--it says "Copy exception details to the clipboard". Do that. Then [edit] and paste the details here. –  Feb 04 '15 at 18:35

0 Answers0