Invalid Pointer Access when calling a class method

 

Hi

This mql5 code is giving a run time error "invalid pointer access", Not sure why and how to fix it, please?

Thank you


News              *news[];                        // at the start of the file

ArrayResize(news, 82);

// then in a loop      
for(int i=0; i<82; i++){    
  news[i] = new News(some variables);               // inside some function 
}

// then inside another function
Print(idx);                                       // Prints 1
Print(ArraySize(news));                           // Prints 82                                  
datetime newsTime = news[idx].getTime();          // <<< Invalid pointer access


            
 
samjesse:

Hi

This mql5 code is giving a run time error "invalid pointer access", Not sure why and how to fix it, please?

Thank you


Just like any array you have to resize it. This, of course, is best left handled to the object pointer collections CList and CArrayObj. 

 
nicholi shen:

Just like any array you have to resize it. This, of course, is best left handled to the object pointer collections CList and CArrayObj. 

I am sorry, I had to include more of the code wo show that the resize of the array is OK.

 
samjesse:

I am sorry, I had to include more of the code wo show that the resize of the array is OK.

Don't know what to tell ya... Most likely something is deleting the object or it losing its ref in the mix. I don't recommend working with pointers like that. Instead, take advantage of one of the collections I mentioned above. 

#include <objvector.mqh>
#include <Strings\String.mqh>

void OnStart()
{
   objvector<CString*> tests;
   for(int i=0; i<5; i++) {
      CString *s = new CString;
      s.Assign(string(i));
      tests.Add(s);
   }
   for(int i=0; i<tests.Total(); i++)
      Print(tests[i].Str());
}

objvector is a subcass of CArrayObj

#include <Arrays\ArrayObj.mqh>
template <typename T> //MUST BE OBJECT POINTER!!!
class objvector : public CArrayObj
{
 public:
   T operator[](const int i) const { return this.At(i); }
   bool Add(T element){ return CArrayObj::Add(element); }
};
 
samjesse:

Hi

This mql5 code is giving a run time error "invalid pointer access", Not sure why and how to fix it, please?

Thank you


I was replying yesterday (mine) to your first topic, when you deleted it, to create this new one later. Please don't do that.

If the pointer is invalid, there is a reason, you need to debug your code. Nobody can help you with the snippet you posted, only possibility I see is the pointer was not initialized correctly at the start (lack of memory ?), or of course something like nicholishen said.

If you need help please post a code that compiles and reproduce your issue.

 

As suggested by nicholi

But errors 'see below';

compile time error in the file below:


include/objvector.mqh

#include <Arrays\ArrayObj.mqh>
template <typename T> //MUST BE OBJECT POINTER!!!
class objvector : public CArrayObj
{
 public:
   T operator[](const int i) const { return this.At(i); }     //<<< 'return'- type mismatch
   bool Add(T element){ return CArrayObj::Add(element); }     //<<< 'element'-parameter conversion not allowed  
};


#include <objvector.mqh>

objvector<News*>  news;

News *n = new News(country, title, t, impact, forecast);
news.Add(n);    
 

Any ideas on how to fix that last error pleaes?

Thank you

 
samjesse:

This mql5 code is giving a run time error "invalid pointer access", Not sure why and how to fix it, please?

class CLASS
{
public:
  int i;
  
  CLASS( const int Tmp ) : i(Tmp) {}
};

void OnStart()
{
  CLASS* Array[];
  
  const int Size = ArrayResize(Array, 100);
  
  for (int i = 0; i < Size; i++)
    Array[i] = new CLASS(i);
    
  int Sum = 0;
  
  for (int i = 0; i < Size; i++)
    Sum += Array[i].i;
    
  Print(Sum); // 4950 - OK
    
  for (int i = 0; i < Size; i++)
    delete Array[i];
}
 
samjesse:

Any ideas on how to fix that last error pleaes?

Thank you

class News : public CObject
 {

 ...

 }
Reason: