The code makes the metatrader 4 platform to crash

 

Hello, I am new using pointers and the `new` word.

I am trying to make a dynamic array of class objects but when I got to the line where I use the `new` word, it crash (in the line 344), I know it is there cuz I added a print before that line and after it and only the one before got executed.

The code compiles with no issues, the problem is during the execution.

I also tried creating 5 class objects and the program crashed when I tried to use the class object for first time, so I guess it is not an array issue.

Please, help.

Files:
 

PD: This is an image of my testing controls where the code runs without issues until it get to the line 344, after opening a new trade what makes the conditionals execute that part of the code

 
Jonathan Aaron Fernandez Rosa #:

PD: This is an image of my testing controls where the code runs without issues until it get to the line 344, after opening a new trade what makes the conditionals execute that part of the code

Array indexing starts with 0. The last element's index is Size-1. Also don't forget the case when Size=0. 

signals[ArraySize(signals)] = new COpenPositions;
 
Laszlo Tormasi #:

Array indexing starts with 0. The last element's index is Size-1. Also don't forget the case when Size=0. 

Thanks for your response. In my case the conditional is only executed if there's at least one trade open,

Ex for one position:

Array gets initialized with 0 spaces, I open a trade which makes true the conditional, makes some stuff and

resize the array to the the OrdersTotal() returned value, which is 1.

Now the array does have 1 space, and I try to add a new object into the space with the line that crashed.


I tried to correct the code using:

signals[ArraySize(signals) - 1] = new COpenPositions;

to get the last available position of the array, which should be 0 in my example, but the same issue, app doesn't execute more lines past this and crashes.


I still doesn't get the problem, could you please provide an example code of how to make an indefinite number of class objects into an array, so I can try to figure out what I did wrong? Thanks in advance.

 
Jonathan Aaron Fernandez Rosa #:

Thanks for your response. In my case the conditional is only executed if there's at least one trade open,

Ex for one position:

Array gets initialized with 0 spaces, I open a trade which makes true the conditional, makes some stuff and

resize the array to the the OrdersTotal() returned value, which is 1.

Now the array does have 1 space, and I try to add a new object into the space with the line that crashed.


I tried to correct the code using:

to get the last available position of the array, which should be 0 in my example, but the same issue, app doesn't execute more lines past this and crashes.


I still doesn't get the problem, could you please provide an example code of how to make an indefinite number of class objects into an array, so I can try to figure out what I did wrong? Thanks in advance.

This constructor stucks in an infinite loop:

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
CSignalsFile::CSignalsFile(void)
  {
   this.orderNumber = 0;
   while(!FileIsExist("signalNo_" + (string)this.orderNumber + ".signal",FILE_COMMON))
     {
      this.orderNumber++;
     }


   this.name = "signalNo_" + (string)orderNumber + ".signal";

  }
 
Laszlo Tormasi #:

This constructor stucks in an infinite loop:

(SOLVED) Thanks for your comment, that was the cause of the problem, deleted the constructor of the CSignalFile class (to a default constructor) and used my loop in another method/external function and now it works with not problem, and doesn't crash.

Thanks!