Invalid Pointer Access - But Why?

 
class CPivotManager : public CObject
  {

   CObjVector<CPivotCandidate>_highPivots;

   ...

void CPivotManager::Tidy()
  {

   for(int i=0;i<_highPivots.Total();i++)
     {

      CPivotCandidate *candidate=_highPivots[i]; <-- Invalid pointer access?

      if(candidate.GetIgnore() && candidate.GetConfirmationStatus()==ConfReversed)
        {
         _highPivots.Delete(i);

         if(_highPivots.Total()>0)
           {
            i=0;
            continue;
           }
        }

     }

   _highPivots.Sort(PM_SORT_ASCENDING);

   for(int i=0;i<_lowPivots.Total();i++)
     {

      CPivotCandidate *candidate=_lowPivots[i];

      if(candidate.GetIgnore() && candidate.GetConfirmationStatus()==ConfReversed)
        {
         _lowPivots.Delete(i);

         if(_lowPivots.Total()>0)
           {
            i=0;
            continue;
           }
        }

     }

   _lowPivots.Sort(PM_SORT_ASCENDING);

  }

}

This is where items are added:

void CPivotManager::AddCandidate(VbtpPattern hiLo,VbtpPatternType type,int pivot,int setup)
  {

   Print("Adding a new Pivot candidate of: ",EnumToString(hiLo),", Time: ",Time[pivot]);

   if(hiLo==TypeHigh)
     {
      _highPivots.Add(new CPivotCandidate(hiLo,type,pivot,setup));

     }
   else
     {

      _lowPivots.Add(new CPivotCandidate(hiLo,type,pivot,setup));

     }

  }

There are obviously items in the _highPivots collection because it finds a Total() value.

Since I originally posted this message, I modified the following method:

bool CPivotManager::_getPendingHighBefore(datetime priorToTime,datetime &timeFound)
  {

   int total=_highPivots.Total();
   bool result=false;
   //CObjVector<CPivotCandidate> pivots; <-- replaced referenced to "pivots" with private class variable _sortPivotCandidates
   _sortPivotCandidates.Clear();

   for(int i=0;i<total;i++)
     {

      if(_highPivots[i].GetIgnore()) continue;

      if(_highPivots[i].GetPivotTime()<priorToTime)
        {

         CPivotCandidate *sortCandidate=_highPivots[i];
         _sortPivotCandidates.Add(sortCandidate);

        }

     }

   if(_sortPivotCandidates.Total()>0)
     {

      _sortPivotCandidates.Sort(PM_SORT_ASCENDING);
      timeFound=_sortPivotCandidates[0].GetPivotTime();
      result=true;

     }

   return result;

  }


Using _sortPivotCandidates (defined as CObjVector<CPivotCandidate> _sortPivotCandidates), the problem disappears? I can only assume that adding the sortCandidate pointer to the method's (original) local "pivots" collection means that "pivots" goes out of scope at the end of the method and somehow, the pointers contained in it are affectedd? However, I thought that this was why pointers are used?

I've been trying to understand this issue for days and have run out of ideas. Any help would be very much appreciated.

Regards and thanks.

 
Geester:
   

? I can only assume that adding the sortCandidate pointer to the method's (original) local "pivots" collection means that "pivots" goes out of scope at the end of the method and somehow, the pointers contained in it are affectedd? However, I thought that this was why pointers are used?

I've been trying to understand this issue for days and have run out of ideas. Any help would be very much appreciated.

---

I see that time passed since your problem was announced here, but I will put my view anyway... such an error do not concern the scope, but really access to pointer (I showed here)... the correct way to get the obj that is behind the pointer outside the scope where the pointer was given the real object is smth like this (use GetPointer())

   
for  (int  i=0 ; i<arr.Total(); i++)      
   Print ("pop " , GetPointer (arr)[i].ToString());   // <<<<<<<<< MEMBER-METHOD CALL 

though I do not see the whole your code - but I do not need it at all... -- next time you'd better to make short compilable example reflecting the problem - and you will probably get the answer quicker than this time...

good luck

p.s. also see links from here

Trying to pass object to class from CArrayObj Object need help in understanding what I am doing wrong
Trying to pass object to class from CArrayObj Object need help in understanding what I am doing wrong
  • 2010.11.20
  • www.mql5.com
Hello Everyone, I am looking for some help with the below code, I am trying to pass an object to a different class from a CArrayObj class using the...
 

BTW, I also had "Invalid pointer access"- error -- when Copy-Constructor & Assignment operator= were not defined correctly ...  and even GetPointer didn't matter...

(do not know the whole your code, but you can check)

p.s.

and besides, creating new object dynamically in class_methods is a BAD design-idea (it increases coupling of classes & possible proException-places in code)... use "new" ONLY in secondary Constructors (like in Java) or primary Constructors (and delete in Destructors)... imho

Reason: