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

 

In the File localvar_testscript_4.mq5

If after the call to GetPointer(), we print the pointer type, it tells us that the pointers are POINTER_AUTOMATIC not POINTER_DINAMIC.

m_array[i]=GetPointer(array[i]);
Print(EnumToString(CheckPointer(m_array[i])));  //POINTER_AUTOMATIC not POINTER_DINAMIC

Therefore this line is never executed

if(CheckPointer(m_array[i])==POINTER_DYNAMIC) delete(m_array[i]);
 

<quote.

@In this order of declaration,items_array is destroyed before the array of objects array [5] is destroyed. In the Destroy() function, which is called from theitems_array destructor, the work is done with pointers to still existing objects, so no errors occur.

Hi everyone!

This is where I don't understand the logic at all. =)

After all, in the Destroy method we compare the type of our pointer with a dynamic type. And our type is automatic. Therefore, the delete operator is not used.

But it is not necessary, because automatically created objects are deleted automatically.

And lastly, the file forgot to specify the type of return value void for the Destroy and SetArray methods at the stage of implementation of methods outside the Class, because of which it throws an error.

Newbies (like me) may be scared =)


void CItemArray::Destroy(void)
  {
   for(int i=0;i<ArraySize(m_array);i++)
     {
      if(CheckPointer(m_array[i])!=POINTER_INVALID)
        {
         if(CheckPointer(m_array[i])==POINTER_DYNAMIC) delete(m_array[i]);
        }
      else Print("Invalid pointer to delete");
     }
  }