How can i delete CTrade my_trade object?

To add comments, please log in or register
Tibor Horvath
895
Tibor Horvath  

Sorry,  but i cant find info:


Im using now C class in mql5 .


#include <Trade\Trade.mqh>

CTrade my_trade=new CTrade;


and my errors: 

2019.05.24 15:06:47.929 Breakout_2019 (EURAUD,M5) 1 undeleted objects left
2019.05.24 15:06:47.930 Breakout_2019 (EURAUD,M5) 1 object of type CTrade left


and a cant delete my_trade object  :

void OnDeinit(const int reason)
  {
//---

// i tried these, but not works
my_trade  =  NULL;

delete  my_trade;

  }

Seng Joo Thio
594
Seng Joo Thio  
Tibor Horvath:

and a cant delete my_trade object  :

void OnDeinit(const int reason)
  {
//---

// i tried these, but not works
my_trade  =  NULL;

delete  my_trade;

  }

You cannot set my_trade to NULL before deleting - so do the delete first.

And after deleting, and given that this is OnDeinit, there is no real need to set my_trade to NULL, although there is no harm.

Alain Verleyen
38395
Alain Verleyen  
Seng Joo Thio:

...

And after deleting, and given that this is OnDeinit, there is no real need to set my_trade to NULL, although there is no harm.

Actually it could be useful, depending of how you deal with pointers, as if the DeInit() reason is parameters change for example, it will be followed by OnInit() where you could check for NULL. Global variables being not reset.

Alain Verleyen
38395
Alain Verleyen  
Tibor Horvath:
...

CTrade my_trade=new CTrade;

Using it as you have done

CTrade my_trade=new CTrade;

my_trade is NOT a pointer. So what is happening is : an object is created dynamically and then copied (copy constructor) to my_trade. The dynamic object then remain "lost" in memory.

A pointer should be declared as :

CTrade *my_trade=new CTrade;

Actually you most probably don't need a pointer at all for CTrade. Just declare it as :

CTrade my_trade;

Don't use pointer and new/delete when there is no need.

Seng Joo Thio
594
Seng Joo Thio  
Alain Verleyen:

Actually it could be useful, depending of how you deal with pointers, as if the DeInit() reason is parameters change for example, it will be followed by OnInit() where you could check for NULL. Global variables being not reset.

That's true, provided the reason wasn't checked beforehand to see if there is any real need to delete.

To add comments, please log in or register