Error in Strategy Tester: freeing unaligned memory 0x0000005EA7DFED43

 
Please help me what could cause subjected error while running the strategy tester.
 

Show us some code. This is quite an uncommon error - don't know if anyone had this before.


From the message, we see that you are trying to free memory that is not 4-byte aligned. Don't know if this should truly be an error, though. Are you calling functions from a DLL? (specially allocating memory somehow). If you're so, try debugging your code where you call new/malloc and delete/free.


Calling new and delete directly in a MQL5 program (EA, indicator, etc.) shouldn't be a problem, considering the platform does all the memory management for you, but try debugging the part of your code where you call these operators in MQL5 aswell. 

 
Emanuel Cavalcante Amorim Filho #:

Show us some code. This is quite an uncommon error - don't know if anyone had this before.

From the message, we see that you are trying to free memory that is not 4-byte aligned. Don't know if this should truly be an error, though. Are you calling functions from a DLL? (specially allocating memory somehow). If you're so, try debugging your code where you call new/malloc and delete/free.

Calling new and delete directly in a MQL5 program (EA, indicator, etc.) shouldn't be a problem, considering the platform does all the memory management for you, but try debugging the part of your code where you call these operators in MQL5 aswell. 

Hi Emanuel

Thanks for your reply. Yeh its seems uncommon, as I never got this error before.

No I am not calling any function from a DLL, and coding for memory allocations are beyond my current skills:)

Yes I do call 'new' pointers for class declaration in Mql classes and EA. Moreover this error just started a day ago only.

2024.05.15 14:48:01.090 freeing invalid size memory in small allocator 0x000000196FBFEBF0 (bucket=0x00000150EE4B0150)
2024.05.15 14:48:01.090 freeing failed in small allocator 0x000000196FBFEC00
2024.05.15 14:48:01.090 2024.05.10 01:01:00   [XAUUSD] getFxSessionEA: timeD01[2024.05.06 00:00] | LastBarH01 Idx/Time[70][2024.05.06 23:00] | NYC Begin[2024.05.05 20:00]
2024.05.15 14:48:01.090 freeing invalid size memory in small allocator 0x000000196FBFEBF0 (bucket=0x00000150EE4B0150)
2024.05.15 14:48:01.090 freeing failed in small allocator 0x000000196FBFEC00
2024.05.15 14:48:01.090 2024.05.10 01:01:00   [XAUUSD] getFxSessionEA: timeD01[2024.05.03 00:00] | LastBarH01 Idx/Time[93][2024.05.03 23:00] | NYC Begin[2024.05.02 17:00]
        string vMethod = "[" + pSymbol + "] " + (string)__FUNCTION__;

                ArrayFree(pFxSessions);
                if(pCount <= 0)                                                                                                                                                                                                                                                                                                                                                                                                         return(0);
                ArrayResize(pFxSessions,pCount);

                // Forex pairs start at 17:00 NY. Gold starts an hour later.
                string beginNYC = "17:00";

                if(StringSubstr(pSymbol,0,3) == "XAU" || StringSubstr(pSymbol,0,4) == "GOLD") {
                        beginNYC = "18:00";
                }

                int day  = idxPTF;
                int bars = iBars(pSymbol,PERIOD_H1);

                datetime timeD01         = iTime(pSymbol,PERIOD_D1,day);
                datetime timeLBH01 = 0;
                CisNewBar isNewBar;
                isNewBar.getLBTimeOnLTF(pSymbol,PERIOD_D1,PERIOD_H1,timeD01,timeLBH01);
                int                      idxLBH01        = iBarShift(pSymbol,PERIOD_H1,timeLBH01);

                datetime tNYC = CTimeZoneInfo::ConvertTimeForPlace(timeD01, ZONE_ID_BROKER, ZONE_ID_NEWYORK);
                PrintFormat("%s: timeD01[%s] | LastBarH01 Idx/Time[%d][%s] | NYC Begin[%s]",vMethod,TimeToString(timeD01),idxLBH01,TimeToString(timeLBH01),TimeToString(tNYC));

                if(timeLBH01 == tNYC) {

                        for(int i = idxLBH01; i < bars; i++) {
        
                                datetime bartime = iTime(pSymbol,PERIOD_H1,i);
                                datetime tNYC    = CTimeZoneInfo::ConvertTimeForPlace(bartime, ZONE_ID_BROKER, ZONE_ID_NEWYORK);
                                //PrintFormat("%s: isNewSession[%s]",vMethod,(string)isNewBar.isNewBar(tNYC));
        
                                if(TimeToString(tNYC,TIME_MINUTES) == beginNYC) {
        
                                        //--- server time corresponding to forex opening at 5 pm in New York (or 6 pm for gold)
                                        pFxSessions[day][SYD].BKRBegin = bartime;
                                        pFxSessions[day][SYD].BKREnd     = bartime + 9*PeriodSeconds(PERIOD_H1);
        
                                        //--- set session hours to 8:00 am - 5:00 pm local time
                                        datetime beginlocal = StringToTime(TimeToString(tNYC+PeriodSeconds(PERIOD_D1), TIME_DATE) + " " + "08:00");
                                        datetime endlocal   = StringToTime(TimeToString(tNYC+PeriodSeconds(PERIOD_D1), TIME_DATE) + " " + "17:00");
        
                                        //--- conversion to broker time
                                        pFxSessions[day][TOK].BKRBegin = CTimeZoneInfo::ConvertTimeForPlace(beginlocal, ZONE_ID_TOKYO,   ZONE_ID_BROKER);
                                        pFxSessions[day][TOK].BKREnd     = CTimeZoneInfo::ConvertTimeForPlace(endlocal,         ZONE_ID_TOKYO,   ZONE_ID_BROKER);
                                        pFxSessions[day][LON].BKRBegin = CTimeZoneInfo::ConvertTimeForPlace(beginlocal, ZONE_ID_LONDON,  ZONE_ID_BROKER);
                                        pFxSessions[day][LON].BKREnd     = CTimeZoneInfo::ConvertTimeForPlace(endlocal,         ZONE_ID_LONDON,  ZONE_ID_BROKER);
                                        pFxSessions[day][NYC].BKRBegin = CTimeZoneInfo::ConvertTimeForPlace(beginlocal, ZONE_ID_NEWYORK, ZONE_ID_BROKER);
                                        pFxSessions[day][NYC].BKREnd     = CTimeZoneInfo::ConvertTimeForPlace(endlocal,         ZONE_ID_NEWYORK, ZONE_ID_BROKER) - 1*PeriodSeconds(PERIOD_M1);
        
                                        PrintFormat("%s: day[%d][%d] NYC Begin[%s]",vMethod,day,i,TimeToString(pFxSessions[day][NYC].BKRBegin));
                                        //---
                                        if(++day >= (pCount+idxPTF)) {
                                                break;
                                        }
                                }
                        }
                }

                pCount = MathMin(pCount,day);
                ArrayResize(pFxSessions,pCount);
                //ArrayPrint(pFxSessions);

                //---
                return(pCount);

} // End of function getFxSessionEA()
 
Anil Varma #:

Hi Emanuel

Thanks for your reply. Yeh its seems uncommon, as I never got this error before.

No I am not calling any function from a DLL, and coding for memory allocations are beyond my current skills:)

Yes I do call 'new' pointers for class declaration in Mql classes and EA. Moreover this error just started a day ago only.

You are using a beta build I guess ?
 
Alain Verleyen #:
You are using a beta build I guess ?

It is Meta Trader 5 Version 5.00 build 4260 | 22 Mar 2024

Update:

I dont know how and why, the error is no longer appearing now. Though the code is still the same.

 
Anil Varma #:

Hi Emanuel

Thanks for your reply. Yeh its seems uncommon, as I never got this error before.

No I am not calling any function from a DLL, and coding for memory allocations are beyond my current skills:)

Yes I do call 'new' pointers for class declaration in Mql classes and EA. Moreover this error just started a day ago only.

When you mess up with memory, things like this can happen. When you delete/free a memory space you really didn't allocated or using the incorrect function/operator (i.e., calling new and free instead of delete in c++), undefined behaviors may happen. The program may even work without errors for a while, but a memory error is inevitable to occur at some point.

The first part of your code is incomplete, but I would take a look at the usage of the function ArrayFree. I've never used this function, but the documentation states that it allows users to free the memory occupied by the already unnecessary dynamic array explicitly and immediately.

If it truly frees memory, after you call it, you can no longer access the array, because the address it points doesn't refer to that array anymore. Seems like you don't need the function ArrayFree, since you are using the array afterwards and even resizing it.

Just remove the ArrayFree function and be sure to declare your array as a dynamic array (double myArray[]).

I normally wouldn't expect this to work, as I believe MQL5 uses some sort of memory arena to avoid unnecessary allocations, thus ArrayFree wouldn't really free memory, but that's what I see that can be causing you some problems.

 
Anil Varma #:

It is Meta Trader 5 Version 5.00 build 4260 | 22 Mar 2024

Update:

I dont know how and why, the error is no longer appearing now. Though the code is still the same.

Ok so the last release. Interesting.

I reported it to MetaQuotes attention.

 
Emanuel Cavalcante Amorim Filho #:

Just remove the ArrayFree function and be sure to declare your array as a dynamic array (double myArray[]).

Thanks Emanuel for high lighting potential problem area as use of 'ArrayFree()' function.

int getFxSessionEA(string pSymbol,SFxSession &pFxSessions[][4],int idxPTF,int pCount=1) {

        string vMethod = "[" + pSymbol + "] " + (string)__FUNCTION__;

                ArrayFree(pFxSessions);
                if(pCount <= 0)                                                                                                                                                                                                                                                                                                                                                                                                         return(0);
                ArrayResize(pFxSessions,pCount);

This is first time I have used MultiDimension array and it was passed in as parameter to return calculated values in it.

The logic to use ArrayFree() was to clear any previously stored data in the passed in array and return with freshly calculated data.

From the calling method of another class, the array has been declared as dynamic array.

I will monitor if the error occurs again.

 
Alain Verleyen #:

Ok so the last release. Interesting.

I reported it to MetaQuotes attention.

Thanks a lot Alain :)

 
Anil Varma #:

Thanks a lot Alain :)

Hi Anil

Could you provide source code to reproduce this behavior?
You can minimize your code or if you don't want to send it to me, send the ex5 file please.

Additionally I need testing/running parameters.

You can send it to me by private message.