Memory corruption when reloading old EAs on build 610 of MT4

 
There seems to be a problem with reloading of old EAs (compiled with build <= 509) on build 610 of MT4 when the timeframe of a chart is changed. [Also submitted to Service Desk.]

In the following example code, the glbLastServer variable is corrupted if an EA is reloaded by changing the chart timeframe. The EA should log one change of the value of glbLastServer, from a blank string to the name of the broker server. Instead, after being reloaded, it logs continual changes, with the value of glbLastServer being corrupted by the value of the implicit cast of the number 123456 to a string.

string glbLastServer = "";

int start()
{
   CastToString(123456); // Implicit cast of any numeric value to a string 
   if (AccountServer() != glbLastServer) {
      Print("Change of global variable: to ", AccountServer() , " from ", glbLastServer);
      glbLastServer = AccountServer();
   }
}  

void CastToString(string x)
{
   // The glbLastServer variable can end up holding the value of this string-cast,
   // i.e. the value of glbLastServer is reported as 123456
}
Steps to reproduce:
  • Compile the code using v509 of MT4 (i.e. metalang.exe, not mql.exe)
  • Put the .ex4 file into an MT4 installation (but not the .mq4, to make sure that the EA is not recompiled using the new compiler)
  • Add the EA to a chart
  • Change the timeframe of the chart.
  • The EA will then start logging corrupted values. For example, glbLastServer will be logged as containing 123456
On the one hand, this problem seems to be very specific. There are several variations on the above code which do not cause a problem (see below). Removing the use of AccountServer(), or removing the number->string cast, or removing the string-concatenation (i.e. use of Print) makes the problem go away.

On the other hand, this looks like a fundamental memory-management problem which is almost certainly causing other subtle issues elsewhere. It will potentially cause lots of EAs to behave erratically if they are reloaded by changing the timeframe of their chart. It is not a good sign that the problem only manifests itself after an EA is reloaded.

For completeness, the following code is also affected – replacing the string concatenation by Print() with a string concatenation which is not printed. There are continual prints of "gvar change" when there should only be one.

string glbLastServer = "";

int start()
{
   CastToString(123456); // Implicit cast of any numeric value to a string 
   if (AccountServer() != glbLastServer) {
      string strMsg = StringConcatenate("Change of global variable: to ", AccountServer() , " from ", glbLastServer);
      Print("gvar change");
      glbLastServer = AccountServer();
   }
}  

void CastToString(string x) {}

The following version does not seem to be affected: replacing AccountServer() with some other function which returns a string:

string glbLastServer = "";

int start()
{
   CastToString(123456); // Implicit cast of any numeric value to a string 

   if (ExampleFunction() != glbLastServer) {
      Print("Change of global variable: to ", ExampleFunction() , " from ", glbLastServer);
      glbLastServer = ExampleFunction();
   }
}  

string ExampleFunction()
{
   return ("Example");
}

The following version also does not seem to be affected: removing the implicit cast from a number to a string:
string glbLastServer = "";

int start()
{
   DummyFunctionNoCast(123456); // Remove implicit cast of number to string
   if (AccountServer() != glbLastServer) {
      Print("Change of global variable: to ", AccountServer() , " from ", glbLastServer);
      glbLastServer = AccountServer();
   }
}  

void DummyFunctionNoCast(int x) {}
 
(Still not fixed in build 613. No response from Service Desk... 8 days and counting.)
 
vabz2014:
(Still not fixed in build 613. No response from Service Desk... 8 days and counting.)
What is your ticket number ?
 
angevoyageur:
What is your ticket number ?
#959802
 
Looks as though it may have been fixed in build 614. Still no response from Service Desk, however.
 
vabz2014:
Looks as though it may have been fixed in build 614. Still no response from Service Desk, however.
I ask to an admin of Metaquotes to check, but they are very busy.
 
I think that they may have done a quick fix of this by doing a complete reload of the EA when the timeframe changes. In v509, a static or global variable keeps its value if the EA is re-initialised by changing the timeframe or symbol. In v614 the variable is reset, implying that the EA is being reloaded from scratch. (I can't remember now, but I think that this was perhaps the behavior in versions of MT4 < 500.)
 

Thank you. Fixed.

Wait for the next build please

 
v616 seems to behave the same way as v509. The bug described above is no longer present, as in v614, with the further change that static and global variables are no longer reset when changing the timeframe of an EA (like v509, but unlike v614).
Reason: