• Sign up >>

Simple assignment causes "Invalid pointer" error

To add comments, please log in or register
Doerk Hilger
1402
Doerk Hilger  

Hey,

from time to time (yes I love this sentence too), my EA causes "Invalid pointer" errors when a pointer is just assigned to a variable. Like this:

CVObject *ptr=(CVObject *)m_invalidupdates.At(i);

m_invalidupdates is an CArrayObj object which is part of the MQ library. CVObject could be any class, deriving from CObject. In my opionion, a simple assignment cannot produce such an error, but it does ... and as said: From time to time, and due to this: No sample code available. And - it happens only in MT5. 

The control libraries and classes such as CWndContainer use the same code. Any ideas?

Of course I could try to check the pointer before I copy it, but I dont want to search for all these statements in several hundred thousand lines of code to create a workaround. And I cannot be sure that it will work afterwards without problems. 

Doerk Hilger
1402
Doerk Hilger  

Update ... I was too quick: I saw, I am checking already the pointer before, but the error occurs nevertheless ... 


The macro PTR_VALID is defined as 

#define PTR_VALID(object) (object!=NULL && CheckPointer(object)!=POINTER_INVALID)

But the error occurs at line 2737 ... for me this looks like a compiler bug

Amir Yacoby
1420
Amir Yacoby  

In case you add to m_invalidvisibles array another derived class of CObject (Not a CVObject *) you might get an "Invalid casting of pointers" error. But, no idea why you get the " Invalid pointer"

Doerk Hilger
1402
Doerk Hilger  

The casting is correct. I mean, it works in 999,999 of 1,000,000 cases, but in some it simply does not. I have more "sometimes-bugs" than just this, for example with ::EventChartCustom() which is processed only "most of the time", or ::EventSetMillisecondTimer(), but these are other issues. 

MetaQuotes support?

Petr Nosek
3161
Petr Nosek  
Doerk Hilger:

Update ... I was too quick: I saw, I am checking already the pointer before, but the error occurs nevertheless ... 

The macro PTR_VALID is defined as 

But the error occurs at line 2737 ... for me this looks like a compiler bug

I have two questions. Why do you do typecasting (CVObject *) at line 2737? The return value of the At() method is a pointer to CObject and if your CVObject is derived from CObject then you don't need typecasting (see my example) and it is redundant because if the item of your array wouldn't be a CVObject you'll get an incorrect casting error.

#include <Arrays\ArrayObj.mqh>

class CVObject : public CObject
  {
private:
   string m_Mark;
public:
   CVObject(const string mark) {m_Mark=mark;}
   string Mark() {return(m_Mark);}
  };

void OnStart()
  {
   CVObject *objectA = new CVObject("A");
   CVObject *objectB = new CVObject("B");
   CArrayObj array;
   array.Add(objectA);
   array.Add(objectB);
   int size = array.Total();
   for(int i=0;i<size;i++)
      if(CheckPointer(array.At(i))!=POINTER_INVALID)
        {
         CVObject *ptr = array.At(i);
         printf("Mark of %d. object is %s",i+1,ptr.Mark());
         delete ptr;
        }
      else printf("Invalid pointer at index %d",i);
   for(int i=0;i<size;i++)
      if(CheckPointer(array.At(i))!=POINTER_INVALID)
        {
         CVObject *ptr = array.At(i);
         printf("Mark of %d. object is %s",i+1,ptr.Mark());
        }
      else printf("Invalid pointer at index %d",i);
  }

The second question is similar. Why do you check within your macro if the object is not equal to NULL? The CheckPointer() returns POINTER_INVALID if object pointer is NULL.

Maybe it won't help you with your problem (there is too little information in your posts) maybe it is a Metaquotes' bug but some redundant pieces of code could cause problems.

As the documentation says a pointer can be incorrect in the following (two) cases:

  • the pointer is equal to NULL;
  • the object has been deleted using the delete operator.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Checking Object Pointer
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Checking Object Pointer
  • www.mql5.com
The CheckPointer() function is used for checking the type of the object pointer. The function returns a value of the ENUM_POINTER_TYPE enumeration. If an incorrect pointer is used, the program execution will be immediately...
Mohammad Hossein Sadeghi
2750
Mohammad Hossein Sadeghi  
Doerk Hilger:

Update ... I was too quick: I saw, I am checking already the pointer before, but the error occurs nevertheless ... 


The macro PTR_VALID is defined as 

But the error occurs at line 2737 ... for me this looks like a compiler bug

In the first post you assigned *ptr from 

m_invalidupdates

while in your second post you assigned it from

m_invalidvisibles

Doerk Hilger
1402
Doerk Hilger  

@Petr

I have two questions. Why do you do typecasting (CVObject *) at line 2737? The return value of the At() method is a pointer to CObject and if your CVObject is derived from CObject then you don't need typecasting (see my example) and it is redundant because if the item of your array wouldn't be a CVObject you'll get an incorrect casting error.

It may be old school but its not wrong. Anyway, it does not explain the behavior, it should not be like it is.

The second question is similar. Why do you check within your macro if the object is not equal to NULL? 

Also old-school, but a comparison of two variables is quicker than a function call with stack operations.

maybe it is a Metaquotes' bug but some redundant pieces of code could cause problems.

Where do you see a redundancy in my code here?

@Mohammad

You`re right, actually its two identical blocks. Sorry for confusing. 


Petr Nosek
3161
Petr Nosek  
Doerk Hilger:

Its not wrong. Anyway, it does not explain the behavior, it should not be like it is.

I agree.

Doerk Hilger:

Where do you see a redundancy in my code here?

Nobody (except you) has your code. Nobody can't replicate your error. Nobody can really help you. If you need help you should provide a sample code that causes the error. As I said my notes may not help you (because nobody knows if it is a Metaquotes' bug or your bug). If I were you I'll start removing the redundancy from your code (especially from the line where the error occurs). The redundancy that I see is the typecasting at line 2737 (where the error occurs) and the double-check in your macro (less important in this case). It is a high probability that it won't help to solve your problem but it is so easy trying it.

Doerk Hilger:

Also old-school, but a comparison of two variables is quicker than a function call with stack operations.

You have three function calls (2xAt() and 1xCheckPointer()) and do two comparisons in your code (macro). Whereas I call just two (same) functions (1xAt() and 1xCheckPointer()) and do just one comparison. I don't believe that my code is slower (maybe the compiler optimizes your code and in that case, both codes should be equally fast).


Btw. I'm not sure but maybe I'm an older school than you.

Alain Verleyen
39085
Alain Verleyen  
Doerk Hilger:

Hey,

from time to time (yes I love this sentence too), my EA causes "Invalid pointer" errors when a pointer is just assigned to a variable. Like this:

m_invalidupdates is an CArrayObj object which is part of the MQ library. CVObject could be any class, deriving from CObject. In my opionion, a simple assignment cannot produce such an error, but it does ... and as said: From time to time, and due to this: No sample code available. And - it happens only in MT5. 

The control libraries and classes such as CWndContainer use the same code. Any ideas?

Of course I could try to check the pointer before I copy it, but I dont want to search for all these statements in several hundred thousand lines of code to create a workaround. And I cannot be sure that it will work afterwards without problems. 

Is it reproducible ?

What build ? 2280 I suppose. If yes and reproducible, do you have the same with build 2190 ?

When you report an issue, please always mention the build, it's boring to have to ask each time. Also please provide the exact error message. Something like

2020.01.22 14:57:19.745    331019 (USDCHF,H1)    invalid pointer access in '331019.mq5' (42,24)

What column is the error message pointing to ? the '=' or the '(' ?
Doerk Hilger
1402
Doerk Hilger  
Alain Verleyen:

Is it reproducible ?

What build ? 2280 I suppose. If yes and reproducible, do you have the same with build 2190 ?

When you report an issue, please always mention the build, it's boring to have to ask each time. Also please provide the exact error message. Something like

2020.01.22 14:57:19.745    331019 (USDCHF,H1)    invalid pointer access in '331019.mq5' (42,24)

What column is the error message pointing to ? the '=' or the '(' ?

Yes, you´re right. This time the error was reported by a user of mine, I forgot to ask for the build. 

I will keep watching this anyway. Next time a user reports this, I will provide more details. 

Alain Verleyen
39085
Alain Verleyen  
Doerk Hilger:

Yes, you´re right. This time the error was reported by a user of mine, I forgot to ask for the build. 

I will keep watching this anyway. Next time a user reports this, I will provide more details. 

I don't know how you manage it, but I guess you have a dev version and a release version. You need to also be sure to use the right source code when investigating. Well it seems obvious but I prefer to mention it, as the error you reported is really weird. Please keep us posted if you find anything.

To add comments, please log in or register