Download MetaTrader 5

invalid pointer access

To add comments, please log in or register
okansahin
16
okansahin  

Hello,

 Can you help me please? 

We define the array as

UpTrend* UpTrends[];

 

And the code of function call 

UpTrend* trend=new UpTrend(Time[lowest_index_on_range],Time[1]);


            if(trend.isSuitable() && !trend.hasSameLow(UpTrends))
              {
                
                int UpTrendsCount=ArraySize(UpTrends);
                ArrayResize(UpTrends, UpTrendsCount+1);
                UpTrends[UpTrendsCount] = trend;
                trend.drawTrendLine();
                
              }
              
            delete trend;

 

 

I am getting invalid pointer access on red line.  

bool UpTrend::hasSameLow(UpTrend *&trends[])
  {

   int count=ArraySize(trends);

   for(int i=0; i<count; i++)
     {
     Print(count); //it writes 1 
     Print(i); // it writes 0
    
      if(trends[i].low_bar.time==low_bar.time)
        {
         return true;
        }
     }
   return false;

  }  

Ex Ovo Omnia
3350
Ex Ovo Omnia  
okansahin:

Hello,

 Can you help me please? 

...

Could you show us the code snippet where you filled the UpTrends[] array?
okansahin
16
okansahin  
Ovo Cz:
Could you show us the code snippet how you filled the trends[] array?

In the first block of message. 

 

                UpTrends[UpTrendsCount] = trend;

 

its in white loop.

 

Here is the full code

 

int totalBars=0;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

   if(Bars>totalBars)
     {
      totalBars=Bars;
      bool found=false;
      int shift=2;
      while(!found)
        {

         if(High[1]<=High[shift] && High[1]>=Low[shift])
           {
            int lowest_index_on_range=iLowest(NULL,PERIOD_CURRENT,MODE_LOW,shift,0);

            UpTrend* trend=new UpTrend(Time[lowest_index_on_range],Time[1]);


            if(trend.isSuitable() && !trend.hasSameLow(UpTrends))
              {
                
                int UpTrendsCount=ArraySize(UpTrends);
                ArrayResize(UpTrends, UpTrendsCount+1);
                UpTrends[UpTrendsCount] = trend;
                trend.drawTrendLine();
                
              }
              
            delete trend;

           }

         if(shift>800)
            break;

         shift++;
        }

     }
  }
whroeder1
15925
whroeder1  
okansahin: I am getting invalid pointer access on red line. 
Why does that surprise you?


  1. You copy the valid pointer to the array.
  2. You use the valid pointer to draw a line
  3. You delete the object, invalidating all pointers to the object.
  4. You then try to use the invalid pointer in the array.
   UpTrends[UpTrendsCount] = trend;
   trend.drawTrendLine();
}
delete trend;
if(trends[i].low_bar.time==low_bar.time)
okansahin
16
okansahin  
whroeder1:
Why does that surprise you?
  1. You copy the valid pointer to the array.
  2. You use the valid pointer to draw a line
  3. You delete the object, invalidating all pointers to the object.
  4. You then try to use the invalid pointer in the array.
   UpTrends[UpTrendsCount] = trend;
   trend.drawTrendLine();
}
delete trend;

Thank you for answer.

 If i dont delete pointer it gives this message in journal

 

2017.01.03 15:30:39.599 2016.03.15 16:00:00  main EURUSD,H1: 13267 objects of type UpTrend left

2017.01.03 15:30:39.583 2016.03.15 16:00:00  main EURUSD,H1: 13267 undeleted objects left

 

how can i pass this? 


Alain Verleyen
Moderator
33341
Alain Verleyen  
okansahin:

Thank you for answer.

 If i dont delete pointer it gives this message in journal

 

2017.01.03 15:30:39.599 2016.03.15 16:00:00  main EURUSD,H1: 13267 objects of type UpTrend left

2017.01.03 15:30:39.583 2016.03.15 16:00:00  main EURUSD,H1: 13267 undeleted objects left

 

how can i pass this? 


You have to delete the pointers inside OnDeinit().

Or eventually when you really don't need it any more.

To add comments, please log in or register