Discussion of article "The Order of Object Creation and Destruction in MQL5"

 

New article The Order of Object Creation and Destruction in MQL5 is published:

Every object, whether it is a custom object, a dynamic array or an array of objects, is created and deleted in MQL5-program in its particular way. Often, some objects are part of other objects, and the order of object deleting at deinitialization becomes especially important. This article provides some examples that cover the mechanisms of working with objects.

MQL5-programs are written in concepts of Object Oriented Programming (OOP), and that not only opens up new possibilities for creating custom libraries, but also allows you to use complete and tested classes of other developers. In the Standard Library, that is included into the MetaTrader 5 Client Terminal, there are hundreds of classes that contain thousands of methods.

To take full advantages of OOP we must clarify some details about creating and deleting objects in MQL5 programs. Creating and Deleting Objects is briefly described in Documentation, and this article will illustrate this topic in examples.

Author: MetaQuotes

 

When LocalVar_TestScript_2.mq5 is run for the first time, it gives only

PO      0       LocalVar_TestScript_2 (EURGBP,M1)       12:08:19        CItem::CItem Constructor
LD      0       LocalVar_TestScript_2 (EURGBP,M1)       12:08:19        CItem::CItem Constructor
HJ      0       LocalVar_TestScript_2 (EURGBP,M1)       12:08:19        CItem::CItem Constructor
DS      0       LocalVar_TestScript_2 (EURGBP,M1)       12:08:19        CItem::CItem Constructor
PH      0       LocalVar_TestScript_2 (EURGBP,M1)       12:08:19        CItem::CItem Constructor
GP      0       LocalVar_TestScript_2 (EURGBP,M1)       12:08:19        CItem::~CItem Destructor
KF      0       LocalVar_TestScript_2 (EURGBP,M1)       12:08:19        CItem::~CItem Destructor
OM      0       LocalVar_TestScript_2 (EURGBP,M1)       12:08:19        CItem::~CItem Destructor
CD      0       LocalVar_TestScript_2 (EURGBP,M1)       12:08:19        CItem::~CItem Destructor
GK      0       LocalVar_TestScript_2 (EURGBP,M1)       12:08:19        CItem::~CItem Destructor

i.e. without the message"delete invalid pointer". After recompiling the same file (i.e. after the second compilation), it shows this:

PQ      0       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        CItem::CItem Constructor
LH      0       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        CItem::CItem Constructor
HO      0       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        CItem::CItem Constructor
DF      0       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        CItem::CItem Constructor
PL      0       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        CItem::CItem Constructor
GR      0       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        CItem::~CItem Destructor
KK      0       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        CItem::~CItem Destructor
OP      0       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        CItem::~CItem Destructor
CF      0       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        CItem::~CItem Destructor
GO      0       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        CItem::~CItem Destructor
PH      1       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        敤敬整渠湯搠湹浡捩漠橢捥t
RP      1       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        敤敬整渠湯搠湹浡捩漠橢捥t
DH      1       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        敤敬整渠湯搠湹浡捩漠橢捥t
FP      1       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        敤敬整渠湯搠湹浡捩漠橢捥t
HH      1       LocalVar_TestScript_2 (EURUSD,H1)       12:09:12        敤敬整渠湯搠湹浡捩漠橢捥t
Has the terminal's reaction to deleting invalid pointers been changed since the article was published? XP, 32
 
Yedelkin:

When LocalVar_TestScript_2.mq5 is run for the first time, it gives only

i.e. without the message"delete invalid pointer". After recompiling the same file (i.e. after the second compilation), it shows this:

Has the terminal's reaction to deleting invalid pointers been changed since the article was published? XP, 32
Thanks for the message. Fixed. The behaviour has changed, now the"delete invalid pointer" messageis generated only when compiling for debugging.
 

mql5:
Спасибо за сообщение. Исправлено. Поведение изменилось, теперь сообщение "delete invalid pointer" выдаётся только при компиляции под отладку.

OK, we would like to see a clarification of the article.
 

Why does this line cause the constructor to run?

 CItem* array1[5];

and this one doesn't:

CObjectC *pObjectC;
?
 
The new operator is needed only to make the class dynamic? But if a dynamic array is "an array with an unspecified value in the first pair of square brackets", what is a dynamic class?
Документация по MQL5: Основы языка / Типы данных / Объект динамического массива
Документация по MQL5: Основы языка / Типы данных / Объект динамического массива
  • www.mql5.com
Основы языка / Типы данных / Объект динамического массива - Документация по MQL5
 
Burgunsky:

Why this line causes the constructor to be called:

I checked, no constructor call is made, please attach the whole code please
 
Burgunsky:

And why this line causes the constructor to run: CItem* array1[5];,

but this one does not: CObjectC *pObjectC; ?

I understood that the line

CItem* array1[5];

by itself does not cause the constructor to be launched. Here is more complete code from the article:

void OnStart()
  {
//--- declare the first array of pointers to the object
   CItem* array1[5];
//--- declare the second array of pointers to the object
   CItem* array2[5];
//--- now let's fill the arrays in the loop
   for(int i=0;i<5;i++)
     {
      //--- pointer for the first array will be created by the operator new
      array1[i]=new CItem;
      //--- the pointer for the second array will be copied from the first array
      array2[i]=array1[i];
     }
The constructor is called not when declaring the first pointer array, but when filling this array in a loop using the new operator.
 
Burgunsky:
The new operator is needed only to make the class dynamic? But if a dynamic array is "an array with an unspecified value in the first pair of square brackets", what is a dynamic class?
I understood that the word "dynamic" in programming is polysemous. When it comes to classes, there is an opposition: "automatically created object" vs "dynamically created object". The difference between them is approximately the same as between variables declared at the global level and variables declared at the local level. In other words, a "dynamically created object" is an object created "for a while", its lifetime is from the moment of creation by the new operator to the moment of deletion by the delete operator.
 

But still there are questions:

What is the point of creating a dynamic object via the new operator?

When creating an object automatically, the class object is created in the stack, it is faster than dynamic object by execution time.

When creating an object dynamically, the class object is created in memory (in the heap) while using the OS memory manager, the process is slower.

Here's a question: if automatic creation is faster, why are dynamic objects created? To prevent the stack from overflowing?

 
Attached source code files and source code insets in HTML code are now completely translated into Portuguese for your convenience.
MQL5.community - User Memo
MQL5.community - User Memo
  • 2010.02.25
  • MetaQuotes Software Corp.
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.