!!! undeleted objects left - page 3

 
Amir Yacoby #:

Any ArrayResize for cExperts somewhere? A change in mSymbolsCount?

Try to add the static instance counter m_occ to CMarketInfo

*update Also you can view the id of the objects not deleted with m_id printed in constructor/destructor

And print it after delete

Thanks a lot Amir

Will try them out.

 
Amir Yacoby #:
Any ArrayResize for cExperts somewhere? A change in mSymbolsCount?

Good morning Amir

ArrayResize ... done at Expert OnInit() and no changes in mSymbolCount. Currently there is only one symbol in EA, and possibly that is the one not being deleted. Other 4 instances seems to come from instantiation from Class(s) other than EA.

string         InpTradeSymbols     = "XAUUSD";
//string       InpTradeSymbols     = "US30|EUSTX50|FRA40|GER40|UK100|XAUUSD";

I have tried your suggestions and indeed there is problem with number of instantiations and deletions.

2024.06.13 09:40:54.876 AlgoEA (XAUUSD,M15) CMarketInfo::CMarketInfo(): Instantiating:  1

2024.06.13 09:40:54.876 AlgoEA (XAUUSD,M15) CMarketInfo::CMarketInfo(): Instantiating:  2

2024.06.13 09:40:54.876 AlgoEA (XAUUSD,M15) CMarketInfo::CMarketInfo(): Instantiating:  3

2024.06.13 09:40:54.883 AlgoEA (XAUUSD,M15) CMarketInfo::CMarketInfo(): Instantiating:  4

2024.06.13 09:40:55.013 AlgoEA (XAUUSD,M15) CMarketInfo::CMarketInfo(): Instantiating:  5

2024.06.13 09:41:20.248 AlgoEA (XAUUSD,M15) CMarketInfo::~CMarketInfo(): Deleting:  4

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) CMarketInfo::~CMarketInfo(): Deleting:  3

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) CMarketInfo::~CMarketInfo(): Deleting:  2

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) CMarketInfo::~CMarketInfo(): Deleting:  1

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) void OnDeinit(const int): Total Objects Left :  1

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) 1 undeleted objects left

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) 1 object of type CMarketInfo left

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) 64 bytes of leaked memory

Below is the relevant codes.

//+-----------------------------------------------------------------------------------------------------------------------------+
//| OBJECT:       Instantiation
//+-----------------------------------------------------------------------------------------------------------------------------+
        // Create pointer array to hold BaseExpertClass for each symbol
        CExpertBase                                                                     *cExperts[];
//+-----------------------------------------------------------------------------------------------------------------------------+
//| EXPERT:       OnInit() Initialization Function
//+-----------------------------------------------------------------------------------------------------------------------------+
int OnInit() {

        //+---------------------------------------------------------------------------------------------------------------------------+
        //| SET UP EXPERT CLASS(s) FOR EACH TRADABLE SYMBOL FROM sSymbolArray
        //+---------------------------------------------------------------------------------------------------------------------------+
                // CONVERT string TradeSymbols variable to the symbol string 'AND' populate TradedSymbols[]
                mSymbolsCount = StringSplit(InpTradeSymbols, '|', TradedSymbols);
                PrintFormat("MultiSymbol EA will process [%i] Symbols",mSymbolsCount);

                ArrayResize(cExperts,mSymbolsCount);
                for(int i = 0; i < mSymbolsCount; i++) {
                        // Set the each symbol expert class, as well pass in TradeSymbols[] for calculating Correlation Coefficient at BaseStragey Class Level
                        cExperts[i] = new CExpertBase(TradedSymbols[i],TradedSymbols,RiskPortfolio,RiskPosition,ATR_MAPeriod);
                        // Set the indicator(s) parameter values for the symbol
                        SetIndicatorParameters(i);
                        // Initialize symbol Expert class
                        cExperts[i].OnInit();                   // OnInit() FAILED or SUCCEEDED check to be implemented for each Symbol
                }
        //+---------------------------------------------------------------------------------------------------------------------------+
                return(INIT_SUCCEEDED);

} // END of function OnInit()
//+-----------------------------------------------------------------------------------------------------------------------------+
//| EXPERT:       OnDeinit() DeInitialization Function
//+-----------------------------------------------------------------------------------------------------------------------------+
void OnDeinit(const int reason) {

        //+---------------------------------------------------------------------------------------------------------------------------+
        //| ON EXIT, DELETE ALL ExpertBase CLASS(s) CREATED BY EXPERT
        //+---------------------------------------------------------------------------------------------------------------------------+
                for(int i = 0; i < mSymbolsCount; i++) {
                        delete cExperts[i];
                        PrintFormat("%s: Total Objects Left : %2d",__FUNCSIG__,CMarketInfo::mOCC);
                }
} // END of function OnDeinit()

 

class CMarketInfo {

        private:
                int             mID;

        public:
                static int      mOCC;
                CMarketInfo() { mOCC++;
                                mID = mOCC;
                                PrintFormat("%s: Instantiating: %2d",__FUNCSIG__,mID);
                              }
         ~CMarketInfo() { mOCC--;
                          mID = mOCC;
                          PrintFormat("%s: Deleting: %2d",__FUNCSIG__,mID);
                        }
        public:
                bool    IsMarketOpen(string pSymbol,datetime pTime);

}; // END of class definition
int CMarketInfo::mOCC = 0;                                                                                                                                      // Initialize the variable

EDIT: CExpertBase class code also included now as below

class CExpertBase final {

public:

        CExpertBase(string pSymbol,const string &pTradedSymbols[],double pRiskPortfolio,double pRiskPerTrade,int pATRPeriod);
      ~CExpertBase();
        
        int                OnInit();
        void               OnTick();
        void               OnTimer();
private:
        CMarketInfo        *cMarketInfo;  // Does not require any initialization parameters

}; // END Of Class DEFINITION
//+-----------------------------------------------------------------------------------------------------------------------------+
//| METHOD:             CExpertBase()
//| APPLICATION:        INITILIZATION OF CLASS
//+-----------------------------------------------------------------------------------------------------------------------------+
CExpertBase::CExpertBase(string pSymbol,const string &pTradedSymbols[],double pRiskPortfolio,double pRiskPerTrade,int pATRPeriod) {

                cMarketInfo = new CMarketInfo();

}; // END Of constructor method CExpertBase()
//+-----------------------------------------------------------------------------------------------------------------------------+
//| METHOD:                     ~CExpertBase()
//| APPLICATION:        DEFUALT DECONSTRUCTOR METHOD
//+-----------------------------------------------------------------------------------------------------------------------------+
CExpertBase::~CExpertBase() {

                // Delete Function Class(s)
                // 2024.06.11 10:02:48.654      AlgoEA (XAUUSD,M15)       - File ExpertBase.mqh Line 121
                delete cMarketInfo;

}; // END Of deconstructor method CExpertBase()
//+-----------------------------------------------------------------------------------------------------------------------------+
//| EXPERT:       OnInit() Initialization Function
//+-----------------------------------------------------------------------------------------------------------------------------+
int CExpertBase::OnInit() {

        //+---------------------------------------------------------------------------------------------------------------------------+
        //| SET UP TRADING DAYS AND TRADING HOURS FOR mSymbol, 
        //+---------------------------------------------------------------------------------------------------------------------------+
                cMarketInfo     = new CMarketInfo();                                                                                                                                                            // Input parameters ... NONE
                cMarketInfo.SetSymbolTradeTimes(vSymbolParam.symbol,vSymbolTradeHours);         // Initialize Trading Hours for the CExpertBase Class's Symbol

                mCloseDoW       = 5;
                mCloseHour      = ((int)(((vSymbolTradeHours[mCloseDoW].timeClose)%86400)/3600));               // return Closing Hour 23:55 as 23
                mCloseMinute    = (int(((vSymbolTradeHours[mCloseDoW].timeClose)%3600)/60)) - 5;                // Subtracted 5 minutes
                SetNextCloseTime();
                //PrintFormat("%s CloseALL Trade on WeekDay[5] at Hours[%s:%s]",vMethod,IntegerToString(mCloseHour,2,'0'),IntegerToString(mCloseMinute,2,'0'));

                return(INIT_SUCCEEDED);

} // END of function OnInit()
 
Anil Varma #:

Good morning Amir

ArrayResize ... done at Expert OnInit() and no changes in mSymbolCount. Currently there is only one symbol in EA, and possibly that is the one not being deleted. Other 4 instances seems to come from instantiation from Class(s) other than EA.

I have tried your suggestions and indeed there is problem with number of instantiations and deletions.

2024.06.13 09:40:54.876 AlgoEA (XAUUSD,M15) CMarketInfo::CMarketInfo(): Instantiating:  1

2024.06.13 09:40:54.876 AlgoEA (XAUUSD,M15) CMarketInfo::CMarketInfo(): Instantiating:  2

2024.06.13 09:40:54.876 AlgoEA (XAUUSD,M15) CMarketInfo::CMarketInfo(): Instantiating:  3

2024.06.13 09:40:54.883 AlgoEA (XAUUSD,M15) CMarketInfo::CMarketInfo(): Instantiating:  4

2024.06.13 09:40:55.013 AlgoEA (XAUUSD,M15) CMarketInfo::CMarketInfo(): Instantiating:  5

2024.06.13 09:41:20.248 AlgoEA (XAUUSD,M15) CMarketInfo::~CMarketInfo(): Deleting:  4

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) CMarketInfo::~CMarketInfo(): Deleting:  3

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) CMarketInfo::~CMarketInfo(): Deleting:  2

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) CMarketInfo::~CMarketInfo(): Deleting:  1

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) void OnDeinit(const int): Total Objects Left :  1

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) 1 undeleted objects left

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) 1 object of type CMarketInfo left

2024.06.13 09:41:20.249 AlgoEA (XAUUSD,M15) 64 bytes of leaked memory

Below is the relevant codes.

 

So you finally tried what I said to do in post #1 four days ago and now you can see your problem and the fix is simple.

Maybe don’t be so fussy over the answers you get next time and you will save some time
 
Paul Anscombe #:
So you finally tried what I said to do in post #1 four days ago and now you can see your problem and the fix is simple.

Hi Paul

I personally feel that Amir is trying to find error more logically then just simple guess work.

Now we have pin-pointed where the error is caused, but there seems to issue something else (a technical glitch or coding error) not just a matter of deleting the object, which is what you are suggesting.

May be you are reaching to conclusion too fast without realizing the real problem.

If you think deleting the object is the only solution, then look at the Expert code above, there is a command to delete, however object is still left. I will be glad if you can answer why it is still left.

 
Anil Varma #:


Why CExpertBase instansiates CMarketInfo twice (once in it's constructor and once in OnInit()) ?


 
Anil Varma #:

Hi Paul

I personally feel that Amir is trying to find error more logically then just simple guess work.

Now we have pin-pointed where the error is caused, but there seems to issue something else (a technical glitch or coding error) not just a matter of deleting the object, which is what you are suggesting.

May be you are reaching to conclusion too fast without realizing the real problem.

If you think deleting the object is the only solution, then look at the Expert code above, there is a command to delete, however object is still left. I will be glad if you can answer why it is still left.

You are making massive assumptions about what I think….  When did I suggest simple guess work?

You made it clear you didn’t like my suggestions, which you have now done and which would have led to this point 4 days ago  so pointless me telling you how to fix it now because you won’t be interested.  I merely posted again in the hope you realise your mistake, like so many here, of being so dismissive of answers received, but no your latest post is still dismissive 

If a person already knows the answer they want why ask a question?
 
Amir Yacoby #:

Why CExpertBase instansiates CMarketInfo twice (once in it's constructor and once in OnInit()) ?


Also if still not helping, remove the second line in the attached pic for more correct delete printing

         ~CMarketInfo() { mOCC--;
                          // mID = mOCC;  <=== Remove this line
                          PrintFormat("%s: Deleting: %2d",__FUNCSIG__,mID);
 
BTW just to clarify

Allocating an object twice and saving them in the same pointer makes the first object lost in memory and undeletable.
 
Amir Yacoby #:
CMarketInfo twice

Hi Amir

Yes I agree, there was a mistake to declaring it twice, and this fact was hidden behind so many other lines of code in between. Here when I posted it, I removed unrelated code, however still failed to recognize it :)

THANKS A LOT for pin-pointing the exact cause and resolving it too.

 
Amir Yacoby #:
Allocating an object twice and saving them in the same pointer makes the first object lost in memory and undeletable.

this is technical information I was not aware of earlier. But logically also it is not correct to declare something twice.