Download MetaTrader 5

Another CList issue

To add comments, please log in or register
williamwong
632
williamwong  

In CList, when freemode is set to false, my program is responsible to free the objects and it did according. 

void CList::Clear()
  {
   m_free_mode=true;
   GetFirstNode();
   while(m_data_total!=0) DeleteCurrent();
  }

But in ~CList it calls Clear() which set freemode = true, why? 

What makes the matter worse, it then goes into indefinite loop because DeleteCurrent() calls DetachCurrent() which check the pointer m_curr_node (which is deleted by my program).  So m_data_total never gets decreased and it goes into infinite loop!

bool CList::DeleteCurrent()
  {
   CObject *result=DetachCurrent();
//--- checking
   if(result==NULL) return(false);
//--- complete the processing of element removed from the list
   if(m_free_mode)
     {
      //--- delete it "physically"
      delete  result;
     }
//---
   return(true);
  }
CObject *CList::DetachCurrent()
  {
   CObject *tmp_node,*result=NULL;
//--- checking
   if(!CheckPointer(m_curr_node)) return(result);
//--- "exploding" list
   result=m_curr_node;
   m_curr_node=NULL;
//--- if the deleted item was not the last one, pull up the "tail" of the list
   if((tmp_node=result.Next())!=NULL)
     {
      tmp_node.Prev(result.Prev());
      m_curr_node=tmp_node;
     }
//--- if the deleted item was not the first one, pull up "head" list
   if((tmp_node=result.Prev())!=NULL)
     {
      tmp_node.Next(result.Next());
      //--- if "last_node" is removed, move the current pointer to the end of the list
      if(m_curr_node==NULL)
        {
         m_curr_node=tmp_node;
         m_curr_idx=m_data_total-1;
        }
     }
   m_data_total--;
//--- if necessary, adjust the settings of the first and last elements
   if(m_first_node==result) m_first_node=result.Next();
   if(m_last_node==result)  m_last_node=result.Prev();
//--- complete the processing of element removed from the list
//--- remove references to the list
   result.Prev(NULL);
   result.Next(NULL);
//---
   return(result);
  }
Alexey Da
Moderator
6568
Alexey Da  

It was fixed.

void CList::Clear()
  {
   GetFirstNode();
   while(m_data_total!=0) DeleteCurrent();
  }

Wait for new build.

Thank you.

To add comments, please log in or register