Yet another 'array out of range' question...

 

This indicator does nothing but illustrate my problem:

//+------------------------------------------------------------------+
//|                                                          Why.mq4 |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict
#property indicator_separate_window

#property indicator_buffers 2

double Buffer1[];
double Buffer2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, Buffer1); 
   SetIndexBuffer(1, Buffer2); 

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   Print(ArraySize(Buffer1)); // Prints 65084
   Print(ArraySize(Buffer2)); // Prints 0
   
   Buffer1[0] = 0; // This is oke...
   Buffer2[0] = 0; // ...but this gives runtime error "Array out of range"
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

 

So Buffer1 gets size 65084 and I can use that. However, Buffer2 gets size 0, so everything I do with it gives me an error (array out of range). 

My solution now is to turn off the strict property, but I don't understand why Buffer2 gets size 0. Is there a way to fix this?

 
oliebol:

This indicator does nothing but illustrate my problem:

 

So Buffer1 gets size 65084 and I can use that. However, Buffer2 gets size 0, so everything I do with it gives me an error (array out of range). 

My solution now is to turn off the strict property, but I don't understand why Buffer2 gets size 0. Is there a way to fix this?

Which MT4 build are you using ? I tested your code on build 670 and it works fine, both buffers Print the same value.
 

Build 670 here as well. I just restarted MetaTrader and indeed it works fine now...

Can't believe this, I must have tested at least 20 variations of this, and got the error every time!

Anyway, thanks for testing this. I guess this thread can be closed.

Regards,
Oliebol 

 
oliebol:

Build 670 here as well. I just restarted MetaTrader and indeed it works fine now...

Can't believe this, I must have tested at least 20 variations of this, and got the error every time!

Anyway, thanks for testing this. I guess this thread can be closed.

Regards,
Oliebol 

It's an MT4 (and MT5) bug. I already got similar problem, and restarting the platform resolve it. However I was not able to reproduce the issue, it's something that sometimes happens during the development of an indicator.
 

Try to add call to IndicatorBuffers(2); function:

 

int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1); 
   SetIndexBuffer(1, Buffer2); 

//---
   return(INIT_SUCCEEDED);
  }

 

Sometimes it helps.

 

I still dont fully trust build 670

I have been writing two versions of my code, one using old mql4 and build 509 and one on the new compiler.

On the new compiler the EA I was working on compiles with zero errors but I had a problem with my error handling that I could not find. As it compiled with zero errors I was not looking for syntax errors


But when I compiled the same code on trusty old 509 ...  'iErrors' - comparison expression expected

if(iErrors(GetLastError()>1)) // should have been, if(iErrors(GetLastError())>1)
 
SDC:

I still dont fully trust build 670

I have been writing two versions of my code, one using old mql4 and build 509 and one on the new compiler.

On the new compiler the EA I was working on compiles with zero errors but I had a problem with my error handling that I could not find. As it compiled with zero errors I was not looking for syntax errors


But when I compiled the same code on trusty old 509 ...  'iErrors' - comparison expression expected




How is it related to this topic ?
 
Because it is another compiler related problem.
 
SDC: Because it is another compiler related problem.
The subject was 'array out of range' which is NOT a compiler related problem.
 
SDC:
Because it is another compiler related problem.
I saw you are a little angry against Metaquotes, but please don't transform too much topics in a battle field. Agreed ?
 
WHRoeder:
SDC: Because it is another compiler related problem.
The subject was 'array out of range' which is NOT a compiler related problem.
Yes I know the thread title was array out of range, but you can see from the conversation the issue turned out not to be array out of range at all. It is a bug in the compiler.
angevoyageur:
I saw you are a little angry against Metaquotes, but please don't transform too much topics in a battle field. Agreed ?
I was not trying to make a battlefield I was just pointing out there are other issues with the new editor not just the need to reboot the terminal after compile and a temporary solution I found useful when debugging code is to use editor 509 for testing because it's error detection is more dependable.
Reason: