Very Strange MQL Code Behaviour

 
Hello all,

MetaTrader Version: 4.00 Build 224 (15 May 2009)
Metaeditor Version: 4.00 Build 224 (14 May 2009)

I have encountered a very strange bug in MQL that has me completely baffled, so much so that I am looking back at ALL my old code to make sure that it is written to avoid this problem.

I hope someone can shed some light on what exactly is going on here.

Here is the code, a simple indicator that essentially does nothing. However in the init() section I have placed a call to 2 functions when the chart period is H1.

The two functions are identical in all but one respect. One has a static int array defined and the other has a non-static int array defined. All the functions do is print out the size of its locally defined array, assign some values to the array elements and then print them out.
#property indicator_chart_window
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void afunc() 
  {
   int i;
   int array[4];

   array[0]=100;
   array[1]=101;
   array[2]=102;
   array[3]=103;

   Print("afunc array[4] size="+ArraySize(array));

   for(i=0;i<4;i++) 
     {
      Print("afunc array["+i+"]="+array[i]);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void afunc_static() 
  {
   int i;
   static int array[4];

   array[0]=100;
   array[1]=101;
   array[2]=102;
   array[3]=103;

   Print("afunc_static array[4] size="+ArraySize(array));

   for(i=0;i<4;i++) 
     {
      Print("afunc_static array["+i+"]="+array[i]);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init() 
  {
   if(Period()==PERIOD_H1) 
     {
      afunc();
      afunc_static();
     }

   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit() 
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start() 
  {
   return(0);
  }
//+------------------------------------------------------------------+

If you look at the code there should never be any circumstance where the two functions behave differently from one another.

However, if I attach the indicator to an M5 chart and then switch to the H1 timeframe I get the following output.
2009.05.23 18:12:47 afunc EURUSD,H1: initialized
2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[3]=103
2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[2]=102
2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[1]=101
2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[0]=100
2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[4] size=4
2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[3]=0
2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[2]=0
2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[1]=0
2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[0]=0
2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[4] size=0
2009.05.23 18:12:47 afunc EURUSD,M5: uninit reason 3
2009.05.23 18:12:47 afunc EURUSD,M5: deinitialized
2009.05.23 18:12:45 afunc EURUSD,M5: initialized
2009.05.23 18:12:43 afunc EURUSD,M5: loaded successfully
Notice that even though I have defined 'int array[4]' in afunc() the size of that array is actually ZERO and I cannot assign values to the elements of the array. This function obviously does not work as expected.

Notice that I have defined 'static int array[4]' in afunc_static() and the size is correctly shown as 4 as are the values I assigned to the elements of the array. This function works fine.

Bottom line. If you define a non-static array in a function and if that function does not get called by your indicator then you could have a problem.

If you subsequently switch the chart timeframe and then call the function the size of any locally defined arrays in the function will be zero irresepective of what you have defined the size of the arrays to be.

The only way around this problem as far as I can see is to make sure that any locally defined arrays in your functions are defined as being static arrays. Nuts I know, but I cannot see any other way to avoid the problem.

I really would appreciate any feedback on this issue.

Regards,

Laurence.
Files:
afunc.ex4  3 kb
 

I agree, looks buggy.

Method:

Compile while indicator is attached to 30m chart

No printout (ok)

Change to H1

Printout occurs

Error occurs.

...

Compile while indicator is attached to H1 chart

Printout occurs

no error

Change to 30m

No printout (ok)

change to H1

Printout occurs

No error

...

4002 error noted

ERR_ARRAY_INDEX_OUT_OF_RANGE 4002 Attempt to access an array item number of which is out of the array range

 

Was going to say something else, but then reproduced phy's scenario.


PS: another "workaround" would be to declare global variables.

 
blogzr3 wrote >>

Was going to say something else, but then reproduced phy's scenario.


PS: another "workaround" would be to declare global variables.

Thanks for the replies. Hopefully a future MetaTrader update will resolve this problem.

Regards,

Laurence.

Reason: