Download MetaTrader 5

OOP: nested classes and undeleted objects and strings after removing the indi or script - what to do?

To add comments, please log in or register
Carl Schreiber
6691
Carl Schreiber  

Hi,

I have written a three nested classes:

  1. a manager,
  2. the quotes class (as many instances as symbols required)
  3. the worker(s) (calculating hat is required)

Now this triumvirate is called either by an indicator and alternatively by a script (mainly for debugging)

Everything works fine so far until I remove the indicator or the script from the chart:

2017.04.20 12:06:23.066 Script test_CWorker EURGBP,M1: removed
2017.04.20 12:06:23.063 test_CWorker EURGBP,M1: 2520 bytes of leaked memory
2017.04.20 12:06:23.063 test_CWorker EURGBP,M1: 8 leaked strings left
2017.04.20 12:06:23.063 test_CWorker EURGBP,M1: 3 objects of type CTicks left
2017.04.20 12:06:23.063 test_CWorker EURGBP,M1: 1 object of type CWorker left
2017.04.20 12:06:23.063 test_CWorker EURGBP,M1: 1 object of type CManager left
2017.04.20 12:06:23.063 test_CWorker EURGBP,M1: 5 undeleted objects left
2017.04.20 12:06:23.061 test_CWorker EURGBP,M1: uninit reason 1
2017.04.20 12:06:16.585 test_CWorker EURGBP,M1: initialized
2017.04.20 12:06:16.545 Script test_CWorker EURGBP,M1: loaded successfully
2017.04.20 11:59:01.304 Custom indicator CWorkerIndi EURGBP,M1: removed
2017.04.20 11:59:01.302 CWorkerIndi EURGBP,M1: 2552 bytes of leaked memory
2017.04.20 11:59:01.302 CWorkerIndi EURGBP,M1: 8 leaked strings left
2017.04.20 11:59:01.302 CWorkerIndi EURGBP,M1: 3 objects of type CTicks left
2017.04.20 11:59:01.302 CWorkerIndi EURGBP,M1: 1 object of type CWorker left
2017.04.20 11:59:01.302 CWorkerIndi EURGBP,M1: 1 object of type CManager left
2017.04.20 11:59:01.302 CWorkerIndi EURGBP,M1: 5 undeleted objects left
2017.04.20 11:59:01.302 CWorkerIndi EURGBP,M1: uninit reason 1
2017.04.20 11:58:05.453 CWorkerIndi EURGBP,M1: initialized

Of course I put in the de-constructor of CManager the deletion of every link to the objects it controls: delete(..);

I even tried to follow the de-construction via DebugBreak() - which is just ignored :(

As well as a Print(__LINE__); as the first order in the deconstruction body to see in which sequence the various de-constructors are called - as you can see in the log: nothing like that. :(

And as the classes do have string arrays (Cmmt[10]) to show their state I put a ArrayFree(Cmmt) in the de-constructor - still I found

8 leaked strings left

Do I make a mistake or shall inform the service desk?

Any hint what to do (may be except don't use nested classes).

Thanks in advance

Calli

honest_knave
Moderator
2423
honest_knave  

@Carl Schreiber


Do you have any stripped out code that can replicate the problem?

Blahtech Limited
4019
James Cater  

Try tracking down the easiest candidate first. Your 1 CManager class is not being deleted.

Usually you would delete the CManager instance in the OnDeinit method

Carl Schreiber
6691
Carl Schreiber  

Sigh ..

BTW if I debug the script and stop the debugging I don't get the memory leaks:

2017.04.20 13:13:14.844 Script test_C3virat EURUSD,M1: removed
2017.04.20 13:13:14.829 test_C3virat EURUSD,M1: uninit reason 4
2017.04.20 13:13:14.748 test_C3virat EURUSD,M1: debugging terminated
2017.04.20 13:11:22.907 test_C3virat EURUSD,M1: initialized
2017.04.20 13:11:22.861 Script test_C3virat EURUSD,M1: loaded successfully
Blahtech Limited
4019
James Cater  
Carl Schreiber:

Sigh ..

BTW if I debug the script and stop the debugging I don't get the memory leaks:

Ok, that's a bit odd. However I doubt it's the platform.

You might have to create a test case and post it here (strip out the code as honest_knave suggested)

Carl Schreiber
6691
Carl Schreiber  
Ok, if I delete the Objects in OnDeinit() the de-constructors are called, my understanding was that if the objects are removed they call their destructor themselves - sigh, why do I have a computer if I still have to do everything on my own :(
Blahtech Limited
4019
James Cater  
Carl Schreiber:
Ok, if I delete the Objects in OnDeinit() the de-constructors are called, my understanding was that if the objects are removed they call their destructor themselves - sigh, why do I have a computer if I still have to do everything on my own :(
You would like Java and C#. They have garbage collectors which do much of the work for you.
whroeder1
14780
whroeder1  
Carl Schreiber: my understanding was that if the objects are removed they call their destructor themselves - sigh, why do I have a computer if I still have to do everything on my own :(
  1. True but if you new them (to a pointer,) you have to delete them before the last pointer goes out of scope.
  2. Because you new'ed them stating that you are responsible for them.
To add comments, please log in or register