MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal

Automated Trading and Strategy Testing Forum

AIIV USDCHF Active Index Inflection Values USDCHFAIIV USDCHF Active Index Inflection Values USDCHF Try product
AIIV USDCHF Active Index Inflection Values USDCHF
Author: Aktiniy
How Secure Is It to Buy MQL5 Market Products? How Secure Is It to Buy MQL5 Market Products? Subscribe to signal
A 0 HawkNest X
77.58%, 7 737.10 USD
Screenshot
EURUSD, H4
Real
CrossIndex Indicator
CrossIndex
Author: GODZILLA
To add comments, please log in or register
MetaQuotes
64674
MetaQuotes 2011.12.07 12:43 

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.

Author: Дмитрий

Auto-Generated Documentation for MQL5 Code
Most Java coders will be familiar with the auto-generated documentation that can be created with JavaDocs. The idea is to add comments into the code in a semi-structured way that can then be extracted into an easy to navigate help file. The C++ world also has a number of documentation auto-generators, with Microsoft's SandCastle and Doxygen being two leaders. The article describes the use of Doxygen to create HTML help file from structured comments in MQL5 code. The experiment worked very well and I believe the help documentation that Doxygen produces from MQL5 code will add a great deal of value.
biantoro kunarto
1210
biantoro 2011.12.09 06:09  

good article...make me easy to understanding about OOP, thank you..
Alexander Piechotta
4589
FinGeR 2012.04.19 16:55  
thanks. good article.
Heinz Traub
311
TripleHeinz 2012.06.11 06:26  
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
25
wulidancing 2013.02.06 15:40  

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



Dmitry Fedoseev
15711
Integer 2013.02.06 16:20  
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:)

 

khai hoan nguyen
143
hoan 2013.02.07 02:45  
OOP is modern program language but to understand it and wirte code we need much time and indulge. Thank good article
wulidancing
25
wulidancing 2013.02.07 05:51  
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
15711
Integer 2013.02.07 08:42  
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
25
wulidancing 2013.02.07 10:18  
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
25
wulidancing 2013.02.11 19:47  
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