invalid pointer access, i will destroy my computer while trying to use OOP in mql5

 

Hello,

i am getting the following error when testing: invalid pointer access in 'S_D_Trader.mq5' (58,13)


This is the important part of the indicator.

The Method CheckForMitigation() return a Pointer of Zone object.

I never had so many problems while programming OOP, like with this language.


Can anyone help or explain why the pointer is invalid??

CList *_zones = new CList();
StructureAnalyser *_structureAnalyserM15;
StructureAnalyser *_structureAnalyserH4;
OrderService *_orderService;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---   
   _structureAnalyserM15 = new StructureAnalyser(PERIOD_M15);
   _structureAnalyserH4 = new StructureAnalyser(PERIOD_H4);
   _orderService = new OrderService();
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   _structureAnalyserM15.CheckForNewBar();
   
   Zone *zone = _structureAnalyserM15.CheckForMitigation();
   if (zone != NULL)
   {
      Print(zone.GetName()); // <====== HERE THE ERROR OCCURS
      if (zone.GetType() == demand)
      {
         if (_structureAnalyserH4.GetStructureState() == SWING_UP || _structureAnalyserH4.GetStructureState() == SWING_UP_SUB)
         {
            _orderService.PlaceMarketOrder(zone);
         }
      }
      
      if (zone.GetType() == supply)
      {
         if (_structureAnalyserH4.GetStructureState() == SWING_DOWN || _structureAnalyserH4.GetStructureState() == SWING_DOWN_SUB)
         {
            _orderService.PlaceMarketOrder(zone);
         }
      }
   }

  }
Documentation on MQL5: MQL5 programs / Runtime Errors
Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
Runtime Errors - MQL5 programs - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Please insert the code correctly: using the button Code
 
NiGapo:

Hello,

i am getting the following error when testing: invalid pointer access in 'S_D_Trader.mq5' (58,13)


This is the important part of the indicator.

The Method CheckForMitigation() return a Pointer of Zone object.

I never had so many problems while programming OOP, like with this language.


Can anyone help or explain why the pointer is invalid??

Show the code for CheckForMitigation.

 

This one is calling the other one:

  Zone *CheckForMitigation()
      {
         
         datetime time = iTime(NULL, _timeframe, 0);
         double high = iHigh(NULL, _timeframe, 0);
         double low = iLow(NULL, _timeframe, 0);
         double open = iOpen(NULL, _timeframe, 0);
         double close = iClose(NULL, _timeframe, 0);
         
         Bar *bar = new Bar(-1, time, high, low, open, close);
         return _zones.CheckForMitigation(bar);
      }


Zone *CheckForMitigation(Bar *bar)
      {
         Zone *zone = _zones.GetFirstNode();
         _lastMitigatedZone = NULL;
         
         while(zone != NULL)
         {
            if (zone.IsEntry(bar))
            {
            
               DestroyZone(zone);
               _zones.DeleteCurrent();
               _lastMitigatedZone = zone;
               //return zone;
            }
            zone = _zones.GetNextNode();
         }
         return _lastMitigatedZone;
      }


Dont be confused, because of the same name "_zones", in the first Method _zones is a class and in the second its a CList

 
NiGapo #:

This one is calling the other one:



Dont be confused, because of the same name "_zones", in the first Method _zones is a class and in the second its a CList

while(zone != NULL)
         {
            if (zone.IsEntry(bar))
            {
            
               DestroyZone(zone);  <---
               _zones.DeleteCurrent(); 
               _lastMitigatedZone = zone;
               //return zone;
            }
            zone = _zones.GetNextNode();
         }

Aren't you accidentaly deleting the Zone object there? That way, the pointer would become invalid but not NULL.
Use the debugger, set a breakpoint there and check the value for _lastMitigatedZone.

 

First of all thank you for your help.


I am not accidentaly deleting the Zone. I want to delete it/deleting it, because it is not important anymore.


But i am just deleting it out of the CList. Why is it losing the pointer when i delete it from the list, when i give it back as value?

Is there a way to get the pointer back?


The "DestoryZone" Method is just calling a Method in another class to delete an drawn object on chart. But at the moment it is doing nothing. Its just like a placeholder:

void DestroyZone(Zone *zone)
      {
         //_structureDrawer.DeleteObject(zone.GetName());
      }



An other strange thing is, that when i call the same method from a method in the same class i dont get this error.

Is there maybe a problem with the scopes, when i call it from the OnInit Method?


The next thing, is that i cant debug it in the tester. It doesnt stop at my break point.


EDIT: I am using the Method DetechCurrent now. Was that the mistake?

 
NiGapo #:

First of all thank you for your help.


I am not accidentaly deleting the Zone. I want to delete it/deleting it, because it is not important anymore.


But i am just deleting it out of the CList. Why is it losing the pointer when i delete it from the list, when i give it back as value?

Is there a way to get the pointer back?


The "DestoryZone" Method is just calling a Method in another class to delete an drawn object on chart. But at the moment it is doing nothing. Its just like a placeholder:



An other strange thing is, that when i call the same method from a method in the same class i dont get this error.

Is there maybe a problem with the scopes, when i call it from the OnInit Method?


The next thing, is that i cant debug it in the tester. It doesnt stop at my break point.


EDIT: I am using the Method DetechCurrent now. Was that the mistake?

I was looking at the List.mqh (Line 362) code, it does not look like it deletes the Zone object, so it is being deleted elsewhere.


It's possible to debug on tester too, use the Start on History data:




You can control the symbol and timeframe on the options: