Error "Acces Violation Read to 0x0001F5D0"

 

Hello! I am getting this error when I close the EA . ("Acces Violation Read to 0x0001F5D0")

It is only coming when I use iMAOnArray funtion.


double EMA_H(double &array[]){ return(  iMAOnArray(array,0,InpMAPeriod,0,MODE_EMA,1)); }

Any ideas?

Thanks!

 

You choose number of elements to be zero and try to access shift=1.

iMAOnArray(array,0,InpMAPeriod,0,MODE_EMA,1));
 
Yashar Seyyedin #:
Maybe the input array has a small length and shift=1 does not exist.
Thanks! But not getting any compiler errors, and the output of this function works. I only get this  error when I close the EA…
 
Daniel Cioca #:
Thanks! But not getting any compiler errors, and the output of this function works. I only get this  error when I close the EA…

Compiler errors only include syntax and grammar errors. Wrong memory access during runtime has nothing to do with compiler.

 
Yashar Seyyedin #:

You choose number of elements to be zero and try to access shift=1.

iMAOnArray

0 means the whole array.

iMAOnArray

shift

[in]  Index of the value taken from the indicator buffer (shift relative to the current bar the given amount of periods ago).

 
Daniel Cioca #:
iMAOnArray

0 means the whole array.

iMAOnArray

shift

[in]  Index of the value taken from the indicator buffer (shift relative to the current bar the given amount of periods ago).

Yes. You are right :D
Maybe the array is too small in length.
 

So I have noticed that this error will come only if I pass the array by ref. If I use the function and call that array inside function, it will not give any erros.


// it will give error on EA removal
double EMA_H(double &array[]){ return(  iMAOnArray(array,0,InpMAPeriod,0,MODE_EMA,1)); }


// this way it will not give error
double EMA_C(){ return(  iMAOnArray(array,0,InpMAPeriod,0,MODE_EMA,1)); }

But 2nd version of this function it means that I have to wright this funtion for every single array.


Kindly advice why do I get this error?

 
Daniel Cioca #:

So I have noticed that this error will come only if I pass the array by ref. If I use the function and call that array inside function, it will not give any erros.


But 2nd version of this function it means that I have to wright this funtion for every single array.


Kindly advice why do I get this error?

Market closed now. I backtested this code and there was no problem.  

void OnTick()
  {
   double array[]={1,2,3,4};
   EMA_H(array);
  }

int InpMAPeriod=10;
double EMA_H(double &array[]){ return(  iMAOnArray(array,0,InpMAPeriod,0,MODE_EMA,1)); }
 
  1. Set the array to as-series before populating it with values, so onArray knows how to process it.

  2. void OnTick()
      {
       double array[]={1,2,3,4};
       EMA_H(array);
      }
    
    int InpMAPeriod=10;
    double EMA_H(double &array[]){ return(  iMAOnArray(array,0,InpMAPeriod,0,MODE_EMA,1)); }

    Remember, you need at least InpMAPeriod elements for a InpMAPeriod average.

  3. Daniel Cioca #:
    iMAOnArray

    0 means the whole array.

    Don't hard code constants, use the proper symbols. WHOLE_ARRAY

 
Daniel Cioca #:

So I have noticed that this error will come only if I pass the array by ref. If I use the function and call that array inside function, it will not give any erros.


But 2nd version of this function it means that I have to wright this funtion for every single array.


Kindly advice why do I get this error?

Can you provide full code that compiles to reproduce this issue ?
 
Alain Verleyen #:
Can you provide full code that compiles to reproduce this issue ?

Hello,

This is the code




#define HeikaAshi  "Indicators\\Heiken Ashi.ex4"
#resource  "\\" + HeikaAshi


input int               InpMAPeriod          = 21                 ; // Heik EMA Period

input ENUM_TIMEFRAMES   InpHeikTimeframe     = PERIOD_D1                  ; // HeikoNashi Timeframe
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
//---Global Variables
int               ArrayIndex        =30                  ; // PriceArraySize

double D_heike_array[];
double cur_heike_array[];
int magic;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double EMA_H(double &array[],int shift) { return(iMAOnArray(array,0,InpMAPeriod,0,MODE_EMA,shift)); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
 {




  FillArrays(InpHeikTimeframe,ArrayIndex,D_heike_array);
  FillArrays(_Period,ArrayIndex,cur_heike_array);

    EventSetTimer(2);

//---
  return(INIT_SUCCEEDED);
 }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void FillArrays(int periods,int sArrayIndex,double &array[])
 {

  ArraySetAsSeries(array,true);

  for(int i=0; i<sArrayIndex; i++)
   {
    ArrayResize(array,i+1);
    array[i]= iCustom(_Symbol,periods,"::"+HeikaAshi,3,i);

   }
 }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
 {
//---
  EventKillTimer();


 }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
 {

   double ma=EMA_H(cur_heike_array,1);
   double ma_d = EMA_H(D_heike_array,1);
   
   Print("MA=",ma);
   Print("ma=",ma_d);
   


 }

void OnTimer()
  {
   

   
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+


 But now I get this errors :

2023.08.06 07:12:15.460 Access violation read to 0x1C04D7FD
2023.08.06 07:13:23.243 Access violation read to 0x1C04D7FA
2023.08.06 07:14:42.815 Access violation read to 0x1C04D7FA
2023.08.06 07:16:44.102 Access violation read to 0x0001FB48
2023.08.06 15:49:41.265 Access violation read to 0x0001506C in 'C:\Users\danie\AppData\Roaming\MetaQuotes\Terminal\B8925BF731C22E88F33C7A8D7CD3190E\MQL4\Experts\Test\Heien_Ashi_EA.ex4'
2023.08.06 15:51:09.473 Access violation read to 0x0001506C in 'C:\Users\danie\AppData\Roaming\MetaQuotes\Terminal\B8925BF731C22E88F33C7A8D7CD3190E\MQL4\Experts\Test\Heien_Ashi_EA.ex4'
2023.08.06 15:51:38.812 Access violation read to 0x0001506C in 'C:\Users\danie\AppData\Roaming\MetaQuotes\Terminal\B8925BF731C22E88F33C7A8D7CD3190E\MQL4\Experts\Test\Heien_Ashi_EA.ex4'

 Thank you

Reason: