Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Report memory leak in strategy tester - MetaTrader 5 Library | Forex Indicators Download - MT4/MT5 Resources

author EAcpu | 5 reads | 0 comments |

Strategy Tester does not report any memory leaks for MQL5 applications such as Expert Advisors, scripts or indicators. And such memory leaks can occur using new operators in your MQL5 code and forgetting to release the occupied memory later in the code by calling the delete operator. For example, in complex programs with many classes and data collections, having to use delete operators is easy to oversee, but such problems are difficult to track down.

In my opinion, a strategy tester should be used not only for developing coherent trading logic, but also for detecting software-related problems. Especially for finding memory leaks. The sooner such problems are discovered and resolved, the better.

Therefore, an include file called "checker_for_memory_leaks" is provided here. Dominique Eggert made a significant contribution to this document; see https://www.mql5.com/en/forum/438987

This header file can be used by Expert Advisors, scripts and custom indicators and should be included at the very beginning of the code. The purpose is to monitor memory leaks that eventually occur during testing of the application in the Strategy Tester. Otherwise no need to change the actual code. The operators new and delete in the header file are "bent" to indicate leaks that eventually occur. After thoroughly testing your application and fixing any memory leaks, you can simply uncomment/remove #include Declare it in your program.

As an example, here's a very simple script called " memoryleak.mq5 " displayed using this header:

 #include //+------------------------------------------------------------------+
//|Simple CA class without any content |
//+------------------------------------------------------------------+
class CA {}; blank start ()
  {
  CA*a;
  a = new california;
if (a== invalid )
    {
print ( "Null pointer!" );
return ;
    } // 'forgot' to call delete operator"
//Delete one; } //+---------------------------------------------------------------------------------+

Output:

 2023.08 . 12 11 : 45 : 26.072 MemoryLeak (.DE40Cash,M15) -- Restored memory leak -- 2023.08 . 12 11 : 45 : 26.072 Memory leak (.DE40Cash, M15) 1 leaving undeleted objects 2023.08 . 12 11 : 45 : 26.072 MemoryLeak (.DE40Cash,M15) - file MemoryLeak.mq5 line 10
2023.08 . 12 11:45 : 26.072 MemoryLeak (.DE40Cash,M15) -- End of Resume --

You can see in which file and on which line the memory has been allocated. This information should make debugging easier to find the correct place in the program where memory release should occur


Attachment download

📎 checker_for_memory_leaks.mqh (2.27 KB)

📎 memoryleak.mq5 (0.52 KB)

Source: MQL5 #45873

Verification code Refresh