Bug in CreateInstanceId() - Standard Library (\Include\Controls\Dialog.mqh)

 

There is an issue in the following method of the CAppDialog class:

//+------------------------------------------------------------------+
//| Create unique prefix for object names                            |
//+------------------------------------------------------------------+
string CAppDialog::CreateInstanceId(void)
  {
   return(IntegerToString(rand(),5,'0'));
  }

When there are two or more indicators on the chart using this class, and they get re-initialized simultaneously (a timeframe change or platform restart), the generated random number will likely be the same for both (due to the same seed), resulting in a non-unique instance_id and thus an initialization failure.

This can be circumvented externally by applying a unique random seed via MathSrand() for each program using that class, but it would be much better if the class itself provided either a way to set the random seed or to explicitly set instance_id.

This issue exists in Dialog.mqh for both MT4 and MT5.