No undeleted object detection in MQL5, but MQL4 has

lastpunisher
82

Hi,

Initially I developed in MQL4 and it helped with leaked memory.



But in MQL5 I do not get the backtester to help me like that.

Is it something I can enable somehow?

Thank you,

Hendrik

Samuel Manoel De Souza
11245
You need delete the object yourself. do that in OnDeinit or as soon as the object is not more needed.
Documentation on MQL5: Language Basics / Operators / Object Delete Operator delete
Documentation on MQL5: Language Basics / Operators / Object Delete Operator delete
  • www.mql5.com
Object Delete Operator delete - Operators - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
lastpunisher
82
lastpunisher  
Samuel Manoel De Souza #:
You need delete the object yourself. do that in OnDeinit or as soon as the object is not more needed.

Yes, I'm aware of that. But MQL4 shows a warning when I have forgotten to do that, but MQL5 does not. I would like to see that warning also in MQL5 :)

Samuel Manoel De Souza
11245
It shows the messages as well. I'm not sure about tester, but you can try in demo account.
Dominik Christian Egert
2606
Yes, it does, also in tester.

If you cannot predict the lifespan of an object, use a smart pointer concept to hold your pointers.

Create a global object, make it take your pointer, as you create it.

Have the smart pointer a destructor that deletes the object by checking if the pointer stored is valid.

Then you can be sure all objects get destroyed on exit.
lastpunisher
82
lastpunisher  
Dominik Christian Egert #:
Yes, it does, also in tester.

Oddly enough this is all I see when the tester terminates and no reference to undelete objects.

HH    0    18:30:12.084    127.0.0.1    login (build 3360)
JL    0    18:30:12.091    Tester    account info found with currency USD
MF    0    18:30:12.095    Tester    expert file added: Experts\Trendy\Trendy.ex5. 194257 bytes loaded
DQ    0    18:30:12.110    Tester    initial deposit 10000.00 USD, leverage 1:500
KJ    0    18:30:12.110    Tester    successfully initialized
LP    0    18:30:12.110    Network    185 Kb of total initialization data received
FK    0    18:30:12.110    Tester    Intel Core i7-9750H  @ 2.60GHz, 32495 MB
GR    0    18:30:12.111    Symbols    XAUUSD: symbol to be synchronized
NH    0    18:30:12.111    Symbols    XAUUSD: symbol synchronized already, 18 bytes received
QM    0    18:30:12.113    History    XAUUSD: load 27 bytes of history data to synchronize in 0:00:00.000
HR    0    18:30:12.113    History    XAUUSD: history synchronized from 2021.01.04 to 2022.07.15
EH    0    18:30:12.195    History    XAUUSD,M1: history cache allocated for 554043 bars and contains 352314 bars from 2021.01.04 01:02 to 2021.12.31 20:59
CK    0    18:30:12.195    History    XAUUSD,M1: history begins from 2021.01.04 01:02
QG    0    18:30:12.197    Tester    XAUUSD,M1 (ICMarketsSC-Demo): every tick generating
NP    0    18:30:12.197    Tester    XAUUSD,M1: testing of Experts\Trendy\Trendy.ex5 from 2022.01.01 00:00 to 2022.07.16 00:00 started with inputs:
RO    0    18:30:12.266    History    XAUUSD,M5: history cache allocated for 111022 bars and contains 70676 bars from 2021.01.04 01:00 to 2021.12.31 20:55
RO    0    18:30:12.266    History    XAUUSD,M5: history begins from 2021.01.04 01:00
HE    0    18:30:12.267    Tester    program file added: \Indicators\donchian_channels.ex5. 11723 bytes loaded
RI    0    18:30:12.997    Trade    2022.01.03 02:14:32   market buy 0.01 XAUUSD (1827.48 / 1827.57 / 1827.48)
JS    0    18:30:12.997    Trades    2022.01.03 02:14:32   deal #2 buy 0.01 XAUUSD at 1827.57 done (based on order #2)
GN    0    18:30:12.997    Trade    2022.01.03 02:14:32   deal performed [#2 buy 0.01 XAUUSD at 1827.57]
IH    0    18:30:12.997    Trade    2022.01.03 02:14:32   order performed buy 0.01 at 1827.57 [#2 buy 0.01 XAUUSD at 1827.57]
DJ    0    18:30:13.208    Trade    2022.01.03 20:36:38   market sell 0.01 XAUUSD (1801.12 / 1801.15 / 1801.12)
CH    0    18:30:13.208    Trades    2022.01.03 20:36:38   deal #3 sell 0.01 XAUUSD at 1801.12 done (based on order #3)
CJ    0    18:30:13.208    Trade    2022.01.03 20:36:38   deal performed [#3 sell 0.01 XAUUSD at 1801.12]
LS    0    18:30:13.208    Trade    2022.01.03 20:36:38   order performed sell 0.01 at 1801.12 [#3 sell 0.01 XAUUSD at 1801.12]
JO    0    18:31:00.658    Trade    2022.07.15 23:56:59   position closed due end of test at 1707.64 [#3 sell 0.01 XAUUSD 1801.12]
FL    0    18:31:00.658    Trades    2022.07.15 23:56:59   deal #4 buy 0.01 XAUUSD at 1707.64 done (based on order #4)
IJ    0    18:31:00.658    Trade    2022.07.15 23:56:59   deal performed [#4 buy 0.01 XAUUSD at 1707.64]
DE    0    18:31:00.658    Trade    2022.07.15 23:56:59   order performed buy 0.01 at 1707.64 [#4 buy 0.01 XAUUSD at 1707.64]
IK    0    18:31:00.658    Trade    2022.07.15 23:56:59   position closed due end of test at 1707.32 [#2 buy 0.01 XAUUSD 1827.57]
DJ    0    18:31:00.659    Trades    2022.07.15 23:56:59   deal #5 sell 0.01 XAUUSD at 1707.32 done (based on order #5)
FO    0    18:31:00.659    Trade    2022.07.15 23:56:59   deal performed [#5 sell 0.01 XAUUSD at 1707.32]
HN    0    18:31:00.659    Trade    2022.07.15 23:56:59   order performed sell 0.01 at 1707.32 [#5 sell 0.01 XAUUSD at 1707.32]
OO    0    18:31:00.659    Tester    final balance 9971.69 USD
KS    0    18:31:00.664    Tester    XAUUSD,M1: 12628679 ticks, 190471 bars generated. Environment synchronized in 0:00:00.027. Test passed in 0:00:48.552 (including ticks preprocessing 0:00:00.531).
PD    0    18:31:00.664    Tester    XAUUSD,M1: total time from login to stop testing 0:00:48.579 (including 0:00:00.027 for history data synchronization)
NS    0    18:31:00.664    Tester    533 Mb memory used including 38 Mb of history data, 256 Mb of tick data
OE    0    18:31:00.664    Tester    log file "C:\Users\hendr\AppData\Roaming\MetaQuotes\Tester\D0E8209F77C8CF37AD8BF550E51FF075\Agent-127.0.0.1-3000\logs\20220803.log" written
CR    0    18:31:00.664        test Experts\Trendy\Trendy.ex5 on XAUUSD,M1 thread finished
II    0    18:31:00.858    127.0.0.1    prepare for shutdown

Samuel Manoel De Souza
11245
lastpunisher #:

Oddly enough this is all I see when the tester terminates and no reference to undelete objects.

HH    0    18:30:12.084    127.0.0.1    login (build 3360)
JL    0    18:30:12.091    Tester    account info found with currency USD
MF    0    18:30:12.095    Tester    expert file added: Experts\Trendy\Trendy.ex5. 194257 bytes loaded
DQ    0    18:30:12.110    Tester    initial deposit 10000.00 USD, leverage 1:500
KJ    0    18:30:12.110    Tester    successfully initialized
LP    0    18:30:12.110    Network    185 Kb of total initialization data received
FK    0    18:30:12.110    Tester    Intel Core i7-9750H  @ 2.60GHz, 32495 MB
GR    0    18:30:12.111    Symbols    XAUUSD: symbol to be synchronized
NH    0    18:30:12.111    Symbols    XAUUSD: symbol synchronized already, 18 bytes received
QM    0    18:30:12.113    History    XAUUSD: load 27 bytes of history data to synchronize in 0:00:00.000
HR    0    18:30:12.113    History    XAUUSD: history synchronized from 2021.01.04 to 2022.07.15
EH    0    18:30:12.195    History    XAUUSD,M1: history cache allocated for 554043 bars and contains 352314 bars from 2021.01.04 01:02 to 2021.12.31 20:59
CK    0    18:30:12.195    History    XAUUSD,M1: history begins from 2021.01.04 01:02
QG    0    18:30:12.197    Tester    XAUUSD,M1 (ICMarketsSC-Demo): every tick generating
NP    0    18:30:12.197    Tester    XAUUSD,M1: testing of Experts\Trendy\Trendy.ex5 from 2022.01.01 00:00 to 2022.07.16 00:00 started with inputs:
RO    0    18:30:12.266    History    XAUUSD,M5: history cache allocated for 111022 bars and contains 70676 bars from 2021.01.04 01:00 to 2021.12.31 20:55
RO    0    18:30:12.266    History    XAUUSD,M5: history begins from 2021.01.04 01:00
HE    0    18:30:12.267    Tester    program file added: \Indicators\donchian_channels.ex5. 11723 bytes loaded
RI    0    18:30:12.997    Trade    2022.01.03 02:14:32   market buy 0.01 XAUUSD (1827.48 / 1827.57 / 1827.48)
JS    0    18:30:12.997    Trades    2022.01.03 02:14:32   deal #2 buy 0.01 XAUUSD at 1827.57 done (based on order #2)
GN    0    18:30:12.997    Trade    2022.01.03 02:14:32   deal performed [#2 buy 0.01 XAUUSD at 1827.57]
IH    0    18:30:12.997    Trade    2022.01.03 02:14:32   order performed buy 0.01 at 1827.57 [#2 buy 0.01 XAUUSD at 1827.57]
DJ    0    18:30:13.208    Trade    2022.01.03 20:36:38   market sell 0.01 XAUUSD (1801.12 / 1801.15 / 1801.12)
CH    0    18:30:13.208    Trades    2022.01.03 20:36:38   deal #3 sell 0.01 XAUUSD at 1801.12 done (based on order #3)
CJ    0    18:30:13.208    Trade    2022.01.03 20:36:38   deal performed [#3 sell 0.01 XAUUSD at 1801.12]
LS    0    18:30:13.208    Trade    2022.01.03 20:36:38   order performed sell 0.01 at 1801.12 [#3 sell 0.01 XAUUSD at 1801.12]
JO    0    18:31:00.658    Trade    2022.07.15 23:56:59   position closed due end of test at 1707.64 [#3 sell 0.01 XAUUSD 1801.12]
FL    0    18:31:00.658    Trades    2022.07.15 23:56:59   deal #4 buy 0.01 XAUUSD at 1707.64 done (based on order #4)
IJ    0    18:31:00.658    Trade    2022.07.15 23:56:59   deal performed [#4 buy 0.01 XAUUSD at 1707.64]
DE    0    18:31:00.658    Trade    2022.07.15 23:56:59   order performed buy 0.01 at 1707.64 [#4 buy 0.01 XAUUSD at 1707.64]
IK    0    18:31:00.658    Trade    2022.07.15 23:56:59   position closed due end of test at 1707.32 [#2 buy 0.01 XAUUSD 1827.57]
DJ    0    18:31:00.659    Trades    2022.07.15 23:56:59   deal #5 sell 0.01 XAUUSD at 1707.32 done (based on order #5)
FO    0    18:31:00.659    Trade    2022.07.15 23:56:59   deal performed [#5 sell 0.01 XAUUSD at 1707.32]
HN    0    18:31:00.659    Trade    2022.07.15 23:56:59   order performed sell 0.01 at 1707.32 [#5 sell 0.01 XAUUSD at 1707.32]
OO    0    18:31:00.659    Tester    final balance 9971.69 USD
KS    0    18:31:00.664    Tester    XAUUSD,M1: 12628679 ticks, 190471 bars generated. Environment synchronized in 0:00:00.027. Test passed in 0:00:48.552 (including ticks preprocessing 0:00:00.531).
PD    0    18:31:00.664    Tester    XAUUSD,M1: total time from login to stop testing 0:00:48.579 (including 0:00:00.027 for history data synchronization)
NS    0    18:31:00.664    Tester    533 Mb memory used including 38 Mb of history data, 256 Mb of tick data
OE    0    18:31:00.664    Tester    log file "C:\Users\hendr\AppData\Roaming\MetaQuotes\Tester\D0E8209F77C8CF37AD8BF550E51FF075\Agent-127.0.0.1-3000\logs\20220803.log" written
CR    0    18:31:00.664        test Experts\Trendy\Trendy.ex5 on XAUUSD,M1 thread finished
II    0    18:31:00.858    127.0.0.1    prepare for shutdown

So there is no undeleted objects.

But if you can't believe that. build you class like that

#include <Arrays/ArrayObj.mqh>
class MyObject : public CObject
  {
   static CArrayObj  my_objects;
public:
                     MyObject(void) {my_objects.Add(GetPointer(this));}
                    ~MyObject(void) {my_objects.Delete(my_objects.Search(GetPointer(this)));}
                     
   static void       Clear(void) {my_objects.Clear();}
  };
CArrayObj MyObject::my_objects;

then you call MyObject::Clear() in OnDeinit.