Download MetaTrader 5

Discussion of article "The Basics of Object-Oriented Programming"

To add comments, please log in or register
MetaQuotes Software Corp.
Moderator
182639
MetaQuotes Software Corp.  

New article The Basics of Object-Oriented Programming is published:

You don't need to know what are polymorphism, encapsulation, etc. all about in to use object-oriented programming (OOP)... you may simply use these features. This article covers the basics of OOP with hands-on examples.

Figure 5. Accessing Functions by Class Users

Author: Дмитрий

Biantoro Kunarto
15341
Biantoro Kunarto  

good article...make me easy to understanding about OOP, thank you..
Alexander Piechotta
6976
Alexander Piechotta  
thanks. good article.
Heinz Traub
314
Heinz Traub  
Thanks man. This article helped me a lot. I was migrating my EA to OOP and was having a special issue with arrays of classes. The dynamic pointers are clearly and perfectly explained. Thanx again.

wulidancing
273
wulidancing  

I have two examples of code based/copied from this article.  They both create an array of pointers to a class and then delete them upon exit.  However, one exits with a memory leak the other exits without a memory leak.  The only difference is the 2nd example has the 'Alert' in the destructor commented out.  When the alert in the destructor is removed there is a memory leak.  This is very weird.  Can anyone explain???  Please help, this is driving me CRAZY.

At the end of the examples of code from this article I have included an extremely simple example of code i wrote that also has a memory leak.  Again, why????  There is nothing complicated about this final example of code... 


 This code works without a memory leak: 

class CName
  {
private:
   int               m_arg; // Variable for the instance
public:
                     CName(int aArg)
     { // Constructor
      m_arg=aArg;
      //Alert("Constructor "+IntegerToString(m_arg));
     }
                    ~CName()
     { // Destructor
      Alert("Destructor "+IntegerToString(m_arg));
     }
  };
//---
CName *cname[]; // Array

void OnInit()
  {
// Prepare array to load ten instances of class
   ArrayResize(cname,10);

   for(int i=0;i<10;i++)
     { // Load instances
      cname[i]=new CName(i);
     }
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   for(int i=0;i<10;i++)
     { // At the end unload all instances from memory
      delete(cname[i]);
     }
}

Messages from expert log, note no leaked memory: 

PI 0 OOP_sConstDestr_2 (EURUSD,M1) 13:57:59 Destructor 0

KP 0 OOP_sConstDestr_2 (EURUSD,M1) 13:57:59 Destructor 1

JG 0 OOP_sConstDestr_2 (EURUSD,M1) 13:57:59 Destructor 2

EN 0 OOP_sConstDestr_2 (EURUSD,M1) 13:57:59 Destructor 3

LF 0 OOP_sConstDestr_2 (EURUSD,M1) 13:57:59 Destructor 4

OM 0 OOP_sConstDestr_2 (EURUSD,M1) 13:57:59 Destructor 5

FD 0 OOP_sConstDestr_2 (EURUSD,M1) 13:57:59 Destructor 6

IK 0 OOP_sConstDestr_2 (EURUSD,M1) 13:57:59 Destructor 7

HS 0 OOP_sConstDestr_2 (EURUSD,M1) 13:57:59 Destructor 8

CJ 0 OOP_sConstDestr_2 (EURUSD,M1) 13:57:59 Destructor 9 

 

This code exits WITH a memory leak!!!!   Why?????????  The only difference is commenting out the 'Alert' in the destructor...

class CName
     {
private:
   int               m_arg; // Variable for the instance
public:
                     CName(int aArg)
     { // Constructor
      m_arg=aArg;
      //Alert("Constructor "+IntegerToString(m_arg));
     }
                    ~CName()
     { // Destructor
      //Alert("Destructor "+IntegerToString(m_arg));
     }
  };
//---

CName *cname[]; // Array

void OnInit()
  {
// Prepare array to load ten instances of class
   ArrayResize(cname,10);

   for(int i=0;i<10;i++)
     { // Load instances
      cname[i]=new CName(i);
     }
  }

//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   for(int i=0;i<10;i++)
     { // At the end unload all instances from memory
      delete(cname[i]);
     }
}

 Messages from expert log, note leaked memory:

FM 1 OOP_sConstDestr_2 (EURUSD,M1) 13:51:19 10 undeleted objects left

EG 1 OOP_sConstDestr_2 (EURUSD,M1) 13:51:19 10 objects of type CName left

GO 1 OOP_sConstDestr_2 (EURUSD,M1) 13:51:19 200 bytes of leaked memory 

 Final example, my code, very simple but exits with a memory leak....

class CCandleStick
{
public:
   CCandleStick() { };
  ~CCandleStick() { };
};

CCandleStick *cCandleArray[];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void OnInit()
  {
   ArrayResize(cCandleArray, 10);
   for (int i = 0; i < 10; i++) {
      cCandleArray[i] = new CCandleStick();
   }   
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   for (int i = 0; i < 10; i++) {
      delete(cCandleArray[i]);
   }   
   Print("Last Error- ", GetLastError(), "   Current time: ", TimeCurrent());
  }

Messages from expert log, note leaked memory: 

MR 0 tester (EURUSD,M1) 14:07:23 Last Error- 0   Current time: 2013.02.06 23:07:23

FL 1 tester (EURUSD,M1) 14:07:23 10 undeleted objects left

KN 1 tester (EURUSD,M1) 14:07:23 10 objects of type CCandleStick left

JS 1 tester (EURUSD,M1) 14:07:23 160 bytes of leaked memory



Documentation on MQL5: Date and Time / TimeCurrent
  • www.mql5.com
Date and Time / TimeCurrent - Documentation on MQL5
Dmitry Fedoseev
43501
Dmitry Fedoseev  
wulidancing:

I have two examples...

Checked all three varaints. All three are identical. All are normal. None gives leaks. In these examples, it can not be - we have the array, in each element of the array have instance, when finishing we delete all objects. Objects do not create copies of themselves, you can not miss to delete samething. If object creates the copy of himself, then we can have difficulties with deleting, very easy to miss something. Show variants that realy have leaking.

Use SRC button to insert code (better - attach files).

Sorry for my english:)

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.
khai hoan nguyen
242
khai hoan nguyen  
OOP is modern program language but to understand it and wirte code we need much time and indulge. Thank good article
wulidancing
273
wulidancing  
Integer:

Checked all three varaints. All three are identical. All are normal. None gives leaks. In these examples, it can not be - we have the array, in each element of the array have instance, when finishing we delete all objects. Objects do not create copies of themselves, you can not miss to delete samething. If object creates the copy of himself, then we can have difficulties with deleting, very easy to miss something. Show variants that realy have leaking.

Use SRC button to insert code (better - attach files).

Sorry for my english:)

 

Thanks so much for taking the time to reply.  I'm not sure you saw my follow up question. It was in a separate comment that probably got deleted because I'm not very familiar with this forum.  However,  I tried running all three examples on an older computer with a older version of MetaTrader.  In the older version all 3 seemed to work fine.  It did not generate an expert log 'memory leak' error, I think it was build 560???, for any of the code examples.  I believe it was build 560??  I do not remember and I upgraded the 560 to the new 756 build.  After the new build I have the same memory leak problems.  Is it possible the old build did not report the memory leak problems because it wasn't a feature?  What build are you using?  Thanks again.

Dmitry Fedoseev
43501
Dmitry Fedoseev  
wulidancing:
Now I also have a memory leak (example 1 - no, example 2 - yes). But it should not be. Nothing helps. Write about it to Service Desk (link in you profile).
wulidancing
273
wulidancing  
Integer:
Now I also have a memory leak (example 1 - no, example 2 - yes). But it should not be. Nothing helps. Write about it to Service Desk (link in you profile).
I have sent a request to the Service Desk.  Thanks again, I am curious as to what the problem is.
wulidancing
273
wulidancing  
wulidancing:
I have sent a request to the Service Desk.  Thanks again, I am curious as to what the problem is.

Here is the service desk message:

 

 

Support Team 2013.02.07 11:18
Status: Open Closed
Thank you for your message. Bug is fixed, please wait for updates.

In the current build of the terminal (756) You can fix this by adding a string type member to the class CName. 

To add comments, please log in or register