Invalid Pointer Access error for a function using array of a custom class instance

 

I have a small custom class and I create an array from instances of that class.

I need to use a function that adds or updates the elements of that array and I get "Invalid pointer access error" when I call that function. The code is compilable but when started in the MT4 client gives that error.

Here is my code (MQL4, MetaEditor 5.00 Build 2375) {the line of the error has a comment just above it in method *add_new_pivot}:

class PivotPoint
{
    public:
        int Index;
        double PP;
        
        // Default constructor
        PivotPoint(void){};
        // Parametric constructor
        PivotPoint(int index, double pp)
        {
            Index = index;
            PP = pp;
        };
};

// create chain of 6 pivot points for the fork chain
PivotPoint *fork_chain[6];

// counter for the elements added in *fork_chain
int fork_counter = 0;

void OnStart()
{
    int index_ = iBars(_Symbol, _Period);
    double high_ = iHigh(_Symbol, _Period, 1);
    
    // increment #fork_counter
    fork_counter++;
    
    // create new fork PivotPoint
    PivotPoint new_pp = new PivotPoint(index_, high_);

    // add the result
    add_new_pivot(fork_chain, new_pp, fork_counter);
}


void add_new_pivot(PivotPoint* &arr_pp[], PivotPoint &pp_, int counter)
{
    ///<summary>Adds new #PivotPoint instance to a #PivotPoint
    /// _ list.</summary>
    
    // find array size
    int len = ArraySize(arr_pp);
    
    // when #counter is less than #len
    if(counter <= len)
    {        
        ////----- Error "invalid pointer acces" on the next row (51)-------
        arr_pp[counter - 1].Index = pp_.Index; // row 51
        arr_pp[counter - 1].PP = pp_.PP;
        
        // row below gives the same error if used as na alternative to 
        // _ the two lines above
        //arr_pp[counter - 1] = pp_;
    }
        
    else
    {
        ArrayResize(arr_pp, len + 1);
        //arr_pp[len] = pp_;
        arr_pp[len].Index = pp_.Index;
        arr_pp[len].PP = pp_.PP;                        
        EraseOrdered(arr_pp, 0);
    }        
}


template <typename T> void EraseOrdered(T& A[], int iPos)
{
   ///<summary>Deletes certain index number from an array.</summary>
   /// Note: The code keeps the order of the elements in the array.
   
   int iLast;
   for(
      iLast = ArraySize(A) - 1; 
      iPos < iLast; 
      ++iPos
      )
   {
      A[iPos] = A[iPos + 1];
   }
      
   ArrayResize(A, iLast);
}

Any ideas?

 
mdragganov:

I have a small custom class and I create an array from instances of that class.

I need to use a function that adds or updates the elements of that array and I get "Invalid pointer access error" when I call that function. The code is compilable but when started in the MT4 client gives that error.

Here is my code (MQL4, MetaEditor 5.00 Build 2375) {the line of the error has a comment just above it in method *add_new_pivot}:

Any ideas?


Hello 
I just noticed that you have passed pointer of pp_ to the function because you have used the new keyword , when the function accepts not pointer for that object Pivot point you d cleared it to accept instance not pointer this might cause the issue try to modify the second argument of the function to accept pointer or simply don't use the new keyword I'm the declaration of pivot point 
In other words your are mixing pointers with instances
 
Kareem Abdelhakim:

Hello 
I just noticed that you have passed pointer of pp_ to the function because you have used the new keyword , when the function accepts not pointer for that object Pivot point you d cleared it to accept instance not pointer this might cause the issue try to modify the second argument of the function to accept pointer or simply don't use the new keyword I'm the declaration of pivot point 
In other words your are mixing pointers with instances
I think I get what you mean but specifically how should I modify my code to make it work?
 
void OnStart()
{
    int index_ = iBars(_Symbol, _Period);
    double high_ = iHigh(_Symbol, _Period, 1);
    
    // increment #fork_counter
    fork_counter++;
    
    // create new fork PivotPoint
    PivotPoint *new_pp = new PivotPoint(index_, high_);

    // add the result
    add_new_pivot(fork_chain, new_pp, fork_counter);
    
    for(int i=0;i<ArraySize(fork_chain);i++)
      if(CheckPointer(fork_chain[i])==POINTER_DYNAMIC)
         delete fork_chain[i];
}


void add_new_pivot(PivotPoint* &arr_pp[], PivotPoint *pp_, int counter)
{
    ///<summary>Adds new #PivotPoint instance to a #PivotPoint
    /// _ list.</summary>
    
    // find array size
    int len = ArraySize(arr_pp);

    // when #counter is less than #len
    if(counter <= len)
    {        
        ////----- Error "invalid pointer acces" on the next row (51)-------
        arr_pp[counter - 1]=pp_;
        Print(arr_pp[counter - 1].Index); // row 51
        Print(arr_pp[counter - 1].PP);
        
        // row below gives the same error if used as na alternative to 
        // _ the two lines above
        //arr_pp[counter - 1] = pp_;
    }
Reason: