undeleted objects left

To add comments, please log in or register
luigisimoncini
26
luigisimoncini  

Hi experts,

when recompiling an indicator I see a couple of (red triangle) messages in the Experts log about x number of objects of my program class undeleted/left from the previous version's deinit

I never used class pointers nor the new operator, I just declare variables of that class, I was expecting these to be automatically deleted when going out of scope

Should I worry?

Alain Verleyen
38676
Alain Verleyen  
luigisimoncini:

Hi experts,

when recompiling an indicator I see a couple of (red triangle) messages in the Experts log about x number of objects of my program class undeleted/left from the previous version's deinit

I never used class pointers nor the new operator, I just declare variables of that class, I was expecting these to be automatically deleted when going out of scope

You are right, but if you get these messages that normally means dynamic allocation.

Should I worry?

Difficult to say without seeing the code.
luigisimoncini
26
luigisimoncini  

thanks, let me try to rephrase, can't put a few thousands lines of code here


I defined a class

I have functions defining several of said class variables, using it, they run and terminate correctly

I have a single function receiving a class object by reference, , it runs and terminates correctly

I have NEVER (I repeat: NEVER) new-ed any class object

I have a red flag undeleted objects message in the log at deinit time

if I try to use delete <class_variable> in my code wherever I use said class variables I get a compiler error because class_variable is NOT a pointer


anybody could tell what to look for that could raise that red log message?


thanks

luigisimoncini
26
luigisimoncini  

For whoever should stumble in the same issue: I solved by changing the code, now whenever I want to use a class object I define a pointer to it, I "new" it and then I delete it before exiting the function/code section


So, essentially dynamic allocation and memory management do NOT work correctly in  MT4, be warned

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
Alain Verleyen
38676
Alain Verleyen  
luigisimoncini:

For whoever should stumble in the same issue: I solved by changing the code, now whenever I want to use a class object I define a pointer to it, I "new" it and then I delete it before exiting the function/code section


So, essentially dynamic allocation and memory management do NOT work correctly in  MT4, be warned

I suppose you had the same problem as here.

Forum on trading, automated trading systems and testing trading strategies

Memory leaks at mql4 when new object is created when initialising of class member at constructor time

Alain Verleyen, 2019.11.29 15:50

It is a compiler bug, resulting in the destructor not being call for temporary object. It has been fixed in recent ME build (around 2230) for mql5. Not sure if (and when) they will make it available for MT4.

A temporary object is created here :

   return objtmp;

So actually this bug occurs because you don't use pointers.


To add comments, please log in or register