"I think your MQL program is causing my MetaTrader to crash"

7 January 2024, 12:12
Stanislav Korotky
0
130

If you've ever thought or heard something like this, in the title, it's almost certainly not true.

MQL programs are compiled by native MetaEditor's tools and executed inside MetaTrader under its full control. By design, MQL program does not and should not contain any means to break the terminal.

The only exception for the rule is the case when MQL code imports a DLL. DLL is a binary program which is outside the control of the terminal, and hence is able to produce demolicious effects not only to the terminal but to Windows in general. This is why MetaTrader prevents DLL imports by default, and a user must enable them explicitly on per single instance basis, solely at his/her discretion and risk. If you faced a problem with the terminal, which you think comes from an MQL program, check if it uses a DLL in the first place.

Most of publicly available MQL programs and all MQL programs delivered through MQL market do not use DLLs.

If DLL is not involved, MQL program by itself should not be a reason of MetaTrader crash. Nevertheless, if it's happened, it's most likely an induced error in the terminal, forced by high load or other circumstances encountered while executing legitimate MQL instructions and built-in MQL APIs.

The great analogue for the terminal and MQL program running in it is a browser showing a webpage (nowadays a webpage uses a bunch of technologies - JavaScript, graphics, multimedia, networking, databases and so on - pretty much as it is in MQL program). Probably you saw moments when your browser crashes on specific page. Did you blame the page or the browser? Even if we admit that some pages may be written incorrectly or in a malicious manner, the browser is responsible for handling this reliably. Errors must be intercepted and reported.

MetaTrader is a money-handling software, so it's supposed to be rock solid, and specifically to trap any errors which may creep into MQL code. Especially taking into account that many authors of MQL programs are traders, not developers, and their mistakes are forgivable.

A division by zero or an attempt of accessing an element out of array's bound are examples of most common errors that occur in MQL source codes. Invalid pointer access or running out of stack memory are less common, but also unrecoverable runtime errors for a single MQL program written without precautions.

All such cases are intercepted by MetaTrader and reported to the Experts log. The MQL program with the error is stopped and unloaded, but the terminal keeps running.

Yet most of errors are not considered critical and do not lead to MQL program termination. For example, math function call on an invalid argument (e.g. square root of a negative number) or operating with invalid handle for a file or an indicator belong to this type of errors, to name a few. After such errors MQL program will probably produce incorrect results, and require user's intervention anyway, but that's another story.

Remember: there are no programs without flaws. When it comes to MQL program, it's normal to have errors inside, but they should not affect the terminal.

The only bad thing with the terminal, that can be caused by native MQL program, is an infinite loop. If it happens in an expert adviser, a script, or a service, it loads a single core of your CPU (until you stop it). If it happens in an indicator, it hangs part of MetaTrader's GUI. The last case is indeed dangerous, but it's not a crash, strictly speaking. Such hangs are possible because indicators are executed in the GUI thread of MetaTrader. Personally, I consider this a flaw in the terminal's design, because it breaks the principle of "sandboxing", applied to MQL in many other aspects (restricted access to files, network, other MQL programs, etc). As a platform, MetaTrader should prevent any damage from MQL program being executed. Probably this will be fixed someday by MQ team.

So what to do if MetaTrader went down unexpectedly? Try to collect all logs from the terminal folder and crashlogs (for MT5 it's in /Users/<username>/AppData/Roaming/MetaQuotes/Crashes) and provide for MQ on the forums. Try to reproduce the problem with minimal configuration (single chart, only related MQL programs), and describe details on how to reproduce it. Try different MT builds and environments as a workaround to alleviate the problem before it's pinned down and fixed.


Share it with friends: