Invalid Pointer When Change Timeframe of the Graph

Samuel Sant Anna  
Hello people!

All right?


When I change the timeframe of the graph, my EA triggers the invalid pointer error, currently I use many pointers, I noticed that with few pointers the error does not occur. I would like ideas and solutions for this situation. If I can keep working with the pointers better, as they are perfect for storing information in more complex EAs. I currently use pointers in the global scope of EA, which makes working with them a lot easier.

I use pointers to store order information that could not be modified (positions and pending orders), or could not be closed (positions), or could not be deleted (pending orders). For later, take these orders / positions and make the necessary checks and try again with the previous values or updated according to the current market price.

My test code:

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#property version   "1.00"

#include <Arrays\ArrayLong.mqh>
CArrayLong *ArrayOrdersModifyTickets = new CArrayLong;
CArrayLong *ArrayPositionsModifyTicket = new CArrayLong;
CArrayLong *ArrayOrdersModifyExpiration = new CArrayLong;
CArrayLong *ArrayOrdersDeleteTickets = new CArrayLong;
CArrayLong *ArrayPositionsCloseTickets = new CArrayLong;
CArrayLong *arraytest = new CArrayLong;

#include <Arrays\ArrayDouble.mqh>
CArrayDouble *ArrayPositionsModifySL_Price = new CArrayDouble;
CArrayDouble *ArrayPositionsModifyTP_Price = new CArrayDouble;
CArrayDouble *ArrayOrdersModifyPrice = new CArrayDouble;
CArrayDouble *ArrayOrdersModifySL_Price = new CArrayDouble;
CArrayDouble *ArrayOrdersModifyTP_Price = new CArrayDouble;
CArrayDouble *ArrayOrdersModifyVolume = new CArrayDouble;
CArrayDouble *ArrayOrdersModifyStopLimit = new CArrayDouble;

#include <Arrays\ArrayInt.mqh>
CArrayInt *ArrayOrdersModifyTypeTime = new CArrayInt;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit() {
   if (CheckPointer(arraytest) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   else {       Print("Invalid Pointer "); }
   if (CheckPointer(ArrayPositionsModifyTicket) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   else {       Print("Invalid Pointer "); }
   if (CheckPointer(ArrayOrdersDeleteTickets) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   else {       Print("Invalid Pointer "); }
   if (CheckPointer(ArrayOrdersModifyExpiration) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   else {       Print("Invalid Pointer "); }
   if (CheckPointer(ArrayOrdersModifyPrice) != POINTER_INVALID ){ Print("Valid Pointer "); }  
   else {       Print("Invalid Pointer "); }
   if (CheckPointer(ArrayOrdersModifySL_Price) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   else {       Print("Invalid Pointer "); }
   if (CheckPointer(ArrayOrdersModifyStopLimit) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   else {       Print("Invalid Pointer "); }
   if (CheckPointer(ArrayOrdersModifyTP_Price) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   else {       Print("Invalid Pointer "); }
   if (CheckPointer(ArrayOrdersModifyTickets) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   else {       Print("Invalid Pointer "); }
   if (CheckPointer(ArrayOrdersModifyTypeTime) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   else {       Print("Invalid Pointer "); }
   if (CheckPointer(ArrayOrdersModifyVolume) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   else {       Print("Invalid Pointer "); }
   if (CheckPointer(ArrayPositionsCloseTickets) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   else {       Print("Invalid Pointer "); }
   if (CheckPointer(ArrayPositionsModifySL_Price) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   else {       Print("Invalid Pointer "); }
   if (CheckPointer(ArrayPositionsModifyTP_Price) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   else {       Print("Invalid Pointer "); }

   if(ArrayOrdersModifyTickets == NULL || ArrayPositionsModifyTicket == NULL || ArrayOrdersModifyExpiration == NULL || ArrayPositionsModifySL_Price == NULL || ArrayPositionsModifyTP_Price == NULL || ArrayOrdersModifyPrice == NULL
         || ArrayOrdersModifySL_Price == NULL || ArrayOrdersModifyTP_Price == NULL || ArrayOrdersModifyVolume == NULL || ArrayOrdersModifyStopLimit == NULL || ArrayOrdersModifyTypeTime == NULL || ArrayOrdersDeleteTickets == NULL
         || ArrayPositionsCloseTickets == NULL)
   {
      Print("ObjectCreateError()");
      return(INIT_FAILED);
   }
          
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Função de Desinicialização do Expert                             |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
   delete ArrayOrdersModifyTickets;
   delete ArrayPositionsModifyTicket;
   delete ArrayOrdersModifyExpiration;
   delete ArrayPositionsModifySL_Price;
   delete ArrayPositionsModifyTP_Price;
   delete ArrayOrdersModifyPrice;
   delete ArrayOrdersModifySL_Price;
   delete ArrayOrdersModifyTP_Price;
   delete ArrayOrdersModifyVolume;
   delete ArrayOrdersModifyStopLimit;
   delete ArrayOrdersModifyTypeTime;
   delete ArrayOrdersDeleteTickets;
   delete ArrayPositionsCloseTickets;
        delete arraytest;
        
        Print(TimeCurrent(),": " ,__FUNCTION__," código de motivo = ",reason);
//--- "limpa" comentário
   Comment("");
}

//+------------------------------------------------------------------+
//| Função tick (ponto) de um Expert                                 |
//+------------------------------------------------------------------+
void OnTick() {
   //if (CheckPointer(arraytest) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   //else {     Print("Invalid Pointer "); }
   //if (CheckPointer(ArrayPositionsModifyTicket) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   //else {     Print("Invalid Pointer "); }
   //if (CheckPointer(ArrayOrdersDeleteTickets) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   //else {     Print("Invalid Pointer "); }
   //if (CheckPointer(ArrayOrdersModifyExpiration) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   //else {     Print("Invalid Pointer "); }
   //if (CheckPointer(ArrayOrdersModifyPrice) != POINTER_INVALID ){ Print("Valid Pointer "); }  
   //else {     Print("Invalid Pointer "); }
   //if (CheckPointer(ArrayOrdersModifySL_Price) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   //else {     Print("Invalid Pointer "); }
   //if (CheckPointer(ArrayOrdersModifyStopLimit) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   //else {     Print("Invalid Pointer "); }
   //if (CheckPointer(ArrayOrdersModifyTP_Price) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   //else {     Print("Invalid Pointer "); }
   //if (CheckPointer(ArrayOrdersModifyTickets) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   //else {     Print("Invalid Pointer "); }
   //if (CheckPointer(ArrayOrdersModifyTypeTime) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   //else {     Print("Invalid Pointer "); }
   //if (CheckPointer(ArrayOrdersModifyVolume) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   //else {     Print("Invalid Pointer "); }
   //if (CheckPointer(ArrayPositionsCloseTickets) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   //else {     Print("Invalid Pointer "); }
   //if (CheckPointer(ArrayPositionsModifySL_Price) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   //else {     Print("Invalid Pointer "); }
   //if (CheckPointer(ArrayPositionsModifyTP_Price) != POINTER_INVALID ){ Print("Valid Pointer "); } 
   //else {     Print("Invalid Pointer "); }
}

Attached, test file.


Thanks in advance for your help!


Best regards,
Samuel.
Files:
Teste.mq5  7 kb
Samuel Sant Anna  

Testing I found the following solution:

When the reason for OnDeinit is REASON_CHARTCHANGE or REASON_PARAMETERS, do not delete the pointers, because of all the reasons OnDeinit were the only ones that generated an invalid pointer when restarting the EA. This solution worked! Does it have any bad side effects on EA in doing this?

void OnDeinit(const int reason) {
        if(reason != REASON_CHARTCHANGE && reason != REASON_PARAMETERS){
           delete ArrayOrdersModifyTickets;
           delete ArrayPositionsModifyTicket;
           delete ArrayOrdersModifyExpiration;
           delete ArrayPositionsModifySL_Price;
           delete ArrayPositionsModifyTP_Price;
           delete ArrayOrdersModifyPrice;
           delete ArrayOrdersModifySL_Price;
           delete ArrayOrdersModifyTP_Price;
           delete ArrayOrdersModifyVolume;
           delete ArrayOrdersModifyStopLimit;
           delete ArrayOrdersModifyTypeTime;
           delete ArrayOrdersDeleteTickets;
           delete ArrayPositionsCloseTickets;
                delete arraytest;
        }
        Print(TimeCurrent(),": " ,__FUNCTION__," código de motivo = ",reason);
//--- "limpa" comentário
   Comment("");
}

Reason: