MQL script giving output only for the first time not 2nd time onwards. I have to close the meta trader terminal to get the output again.

 

Hello All,

I am creating a MQL4 script which imports a C++ DLL to access some functions. The imported C++ DLL is created by me and it invokes third party licensed C++ libraries  to perform some mathematical calculations.

I am able to run the code with out any issues and I get the expected output but only for the first time. When I try to rerun the MQL script 2nd time onwards, I am not getting the output [No run time errors]. If I close the meta trader terminal and rerun the script, I get the output again. Can someone please help me fix this issue? Given below is my code:

MQL4 Script

#property strict
#import "Test.dll"
        int J_Test(double &Arr_1[], double &Arr_2[], int, double &Output_1, double &Output_2);
#import 

void OnStart()
{
        double Var_1;
        double Var_2;

        double A[20]={1.29216,1.29026,1.29622,1.29565,1.29422,1.28366,1.28224,1.27954,1.28132,1.27712,1.27461,1.2708,1.27202,1.27087,1.27039,1.27366,1.27811,1.27421,1.27584,1.2813};
        double B[20]={0.82018,0.82007,0.82042,0.82232,0.82334,0.82147,0.8207,0.82202,0.82032,0.81537,0.81542,0.81512,0.81477,0.81727,0.81777,0.81337,0.80978,0.81317,0.81318,0.81672};

        int row=ArrayRange(A,0);
        int col=2;

        int y=J_Test(A,B,row,Var_1,Var_2);

        Alert(Var_1);
        Alert(Var_2);

        ArrayFree(A);
        ArrayFree(B);
}

 

C++ DLL Code

#define MT4_EXPFUNC __declspec(dllexport)

MT4_EXPFUNC int __stdcall J_Test(double *Array_1, double *Array_2, const int rows, double *Out_1, double *Out_2)
{
        // Create a 2D array out of 2 1D arrays
        double ** Array2d = new double*[rows];
        for (int cnt_1 = 0; cnt_1 < rows; ++cnt_1)
        {
                Array2d[cnt_1] = new double[2];
        }

        //Filling the 2D array created above with data from input arrays Array_1 and Array_2
        for (int cnt_1 = 0; cnt_1 < rows; cnt_1++)
        {
                Array2d[cnt_1][0] = Array_1[cnt_1];
                Array2d[cnt_1][1] = Array_2[cnt_1];
        }

        int v_Ret_Cd=func_Calc(Array2d, rows, 2, 3, 1.25, *Out_1, *Out_2);

        delete[] Array2d;
        return v_Ret_Cd;
}

//J_TEST calls below function
MT4_EXPFUNC int __stdcall func_Calc(double **i_Array, int i_Rows, int i_Cols, int i_Arg4,  double i_Arg5, double &i_Out_1, double &i_Out_2)
{
        int v_Ret_Cd = -1;

// Calling the 3rd party library function. This function does math operations on 2D array based on input arguments and returns output to the right most 2 arguments
        v_Ret_Cd = SDK_JTEST(i_Array, i_Rows, i_Cols, i_Arg4, i_Arg5, &i_Out_1, &i_Out_2);

        return v_Ret_Cd;
}

 

 Thanks in advance.

 
vikas_trl:

[...] Can someone please help me fix this issue?

You need to put some logging inside your DLL to see how far it is getting. It sounds as though it must be dying inside SDK_JTEST(), in a way which isn't logged as an exception by MT4.

One possible reason is that your data could be said to have 2 rows and 20 columns, rather than vice versa. Are you certain that you are passing the data in the form which SDK_JTEST() expects? What happens if you do SDK_JTEST(..., i_Cols, i_Rows,...) instead of  SDK_JTEST(..., i_Rows, i_Cols,...) ?

 
jjc:

You need to put some logging inside your DLL to see how far it is getting. It sounds as though it must be dying inside SDK_JTEST(), in a way which isn't logged as an exception by MT4.

One possible reason is that your data could be said to have 2 rows and 20 columns, rather than vice versa. Are you certain that you are passing the data in the form which SDK_JTEST() expects? What happens if you do SDK_JTEST(..., i_Cols, i_Rows,...) instead of  SDK_JTEST(..., i_Rows, i_Cols,...) ?

Thanks jjc for you reply. SDK_JTEST is getting data the way it wanted else it wouldn't return outputs to the i_Out_1 and i_Out_2 variables.

I converted the DLL to C++ console application and passed the 1D arrays from main() function to JH_Test(). It ran with out any issue no matter how many times I run. So atleast with in C++ code there is no issue I can say.

 I will try to add logging with in the DLL and see what happens.