invalid pointer access

 

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;

  }  

 
okansahin:

Hello,

 Can you help me please? 

...

Could you show us the code snippet where you filled the UpTrends[] array?
 
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++;
        }

     }
  }
 
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)
 
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? 


 
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.

Reason: