Getting _LastError = 4012 unexcpectedly immediate following the return from a Class method.

 

In my MQL5 EA, I have a class StatusReport that contains a method:   

 static void  TestReports().

In OnInit() I have a call to   

 StatusReport::TestReports();

Before the call to TestReports(), the  err = GetLastError(); returns 0.

At the end of TestReports(), before returning, the statement err = GetLastError(); returns 0;

Immediately following the return from TestReports(), the statement err = GetLastError(); returns 4012 (ERR_INVALID_POINTER). WHY?

If TestReports() had no _LastError just before the function return, why did OnInit() have a _LastError = 4012 immediately after the return from the call to:

StatusReport::TestReports();

Where did the _LastError pointer error (4012) come from following the return from the call to StatusReport::TestReports(); This call functions as it should.

Is there a better way to call a Method like TestReports(), defined within the class StatusReport ?

The MQL5 compiler required that the TestReports method with StatusReport  to have a static access class declaration.

Is this error caused by a BUG in the MQL5 runtime system that should not otherwise happen?

What is the best way to eliminate this error?

Any Help is much appreciated.


 
it’s almost certainly not a bug in MQL5, and not related to static .
You’re almost certainly hitting an invalid pointer / double-free in some destructor or hidden cleanup code that runs after your last line in TestReports()

void StatusReport::TestReports()
{
   // ...
   int err = GetLastError();             // 0 here
   // return;
}
int OnInit()
{
   ResetLastError();
   int err = GetLastError();             // 0

   StatusReport::TestReports();          // call

   err = GetLastError();                 // 4012 here
}

Before the call → 0 
Inside, just before return → 0 
After the call → 4012

 
Alireza Zahedi #:

Your answer looks like generated content. Please note that AI-generated answers are not allowed on the forum.

However, as it turns out, the delete operator will indeed generate a 4012 error if applied to an invalid pointer.

class A {};

void OnStart()
  {
   A* a = new A;
   delete a;
   delete a;
   Print(GetLastError()); // 4012
  }

This may be obvious to some, but I personally didn't know about it.

 
Alireza Zahedi #:
it’s almost certainly not a bug in MQL5, and not related to static .
You’re almost certainly hitting an invalid pointer / double-free in some destructor or hidden cleanup code that runs after your last line in TestReports()


Before the call → 0 
Inside, just before return → 0 
After the call → 4012

My StatusReport class does not have a destructor or hidden cleanup code, so the invalid pointer cannot be coming from there.
Could the MQL5 runtime system be doing some hidden cleanup operations on the function return that I am not aware of?

Where else can it be coming from?

You are right:

Before the call → GetLastError returns 0 
Inside, just before return → GetLastError returns 0 
After the call returns → GetLastError returns 4012

 
Vladislav Boyko #:

Your answer looks like generated content. Please note that AI-generated answers are not allowed on the forum.

However, as it turns out, the delete operator will indeed generate a 4012 error if applied to an invalid pointer.

This may be obvious to some, but I personally didn't know about it.

I do not use a delete operator anywhere in my StatusReport class nor in the StatusReport::TestReports().

Where else could the Invalid Pointer 4012 be coming from?

Thanks for your response.

I am still baffled

.

 
Don Baechtel #:

I do not use a delete operator anywhere in my StatusReport class nor in the StatusReport::TestReports().

Where else could the Invalid Pointer 4012 be coming from?

Thanks for your response.

I am still baffled

.

Post code that compiles to reproduce the issue if you want a clear answer.
 
Don Baechtel #: I do not use a delete operator anywhere in my StatusReport class nor in the StatusReport::TestReports().

Do you use the new operator anywhere?