I'm going to post the LinkedList class below. I found the error to occur while clearing a list with 1 element. If I ignore the fact that the integer value of the element seems anomalous, and for some reason the element next & previous pointers point to DDDDDDDD, the actual address of the element is 003B6070 which is, I think, a normal address for something allocated to the free store.
The error occurs during the line that is supposed to delete the element:
PopFront():
if (First_ == Last_){ delete First_; //CRASH First_ = 0; Last_ = 0; Length_ = 0; }
I've double-checked all the insert/removal methods of the LinkedList and I can't find the problem.
In PopFront or PopBack, the ends of the list are set to null terminators. The only other places where elements are deleted without calling the PopFront or PopBack functions are in the RemoveAt() and Remove() functions which remove at an index or a particular value respectively. When they determine the element is not on the front or the end of the list, they remove it (delete it) and tie the preceding and following elements together.
Here's the element and the list classes:
class ListElem{public: unsigned int Value()const{return Value_;} ListElem* Next()const{return Next_;} ListElem* Prev()const{return Prev_;} void SetValue(const unsigned int& value_){ Value_ = value_; } void SetPrev(ListElem* prev_){ Prev_ = prev_; } void SetNext(ListElem* next_){ Next_ = next_; } void Isolate(){ Next_=0; Prev_=0; } bool IsIsolated(){ if ((Next_!=0) && (Prev_!=0)) { return false; } else return true; } void PrintMe()const{ std::cout << "List Elem PrintMe: Address: " << this << std::endl; if(this == 0){ return; } else{ std::cout << "Value: " << Value_ << std::endl; std::cout << "Next: " << Next_ << std::endl; std::cout << "Prev: " << Prev_ << std::endl; } } ListElem(const unsigned int& value_):Value_(value_),Next_(0),Prev_(0){} ListElem(const unsigned int& value_, ListElem* prev_):Value_(value_),Next_(0),Prev_(prev_){} ListElem(const unsigned int& value_, ListElem* prev_, ListElem* next_):Value_(value_),Prev_(prev_),Next_(next_){} ListElem(ListElem* next_, const unsigned int& value_):Value_(value_),Next_(next_),Prev_(0){}private: unsigned int Value_; ListElem* Next_; ListElem* Prev_;};class LinkedList{public: LinkedList& Insert(const unsigned int& value_){ if (Length_ == 0){ First_ = new ListElem(value_); Last_ = First_; } else if (First_ == Last_){ if(value_<=First_->Value()){ First_ = new ListElem(Last_, value_); Last_->SetPrev(First_); } else{ Last_ = new ListElem(value_, First_); First_->SetNext(Last_); } } else{ if(value_ <= First_->Value()){ First_->SetPrev(new ListElem(First_, value_)); First_ = First_->Prev(); } else if(value_ >= Last_->Value()){ Last_->SetNext(new ListElem(value_, Last_)); Last_ = Last_->Next(); } else{ ListElem* curr_ = First_->Next(); while (value_ > curr_->Value()){ curr_ = curr_->Next(); } curr_->Prev()->SetNext(new ListElem(value_, curr_->Prev(), curr_)); curr_->SetPrev(curr_->Prev()->Next()); } } ++Length_; return *this; } LinkedList& Remove(const unsigned int& value_){ ListElem* curr_ = Find(value_); if(curr_==0){ std::cout << "LinkedList Remove Err: Value not found" << std::endl; } else if (curr_==First_){ PopFront(); } else if (curr_==Last_) { PopBack(); } else { curr_->Prev()->SetNext(curr_->Next()); curr_->Next()->SetPrev(curr_->Prev()); delete curr_; --Length_; } return *this; } unsigned int RemoveAt(const unsigned int& index_){ unsigned int val_out = MAX_UINT; if(index_>=Length_){ std::cout << "LinkedList RemoveAt Err: Index out of bounds or list empty" << std::endl; return val_out; } else if (index_==0){ val_out = First_->Value(); PopFront(); return val_out; } else if (index_==(Length_-1)){ val_out = Last_->Value(); PopBack(); return val_out; } else{ ListElem* curr_ = First_; for(unsigned int i = 0; i < index_; ++i){ curr_ = First_->Next(); } curr_->Prev()->SetNext(curr_->Next()); curr_->Next()->SetPrev(curr_->Prev()); val_out = curr_->Value(); delete curr_; --Length_; return val_out; } } LinkedList& PopFront(){ if (First_ == 0){ std::cout << "LinkedList.PopFront() ERROR: First element points to NULL" << std::endl; return *this; } else{ if (First_ == Last_){ std::cout << "First==Last!!" << std::endl; delete First_; First_ = 0; Last_ = 0; Length_ = 0; } else{ First_ = First_->Next(); delete First_->Prev(); First_->SetPrev(0); --Length_; } return *this; } } LinkedList& PopBack(){ if (Length_ == 0){ std::cout << "LinkedList.PopBack() ERROR: Empty list" << std::endl; return *this; } else{ if (First_ == Last_){ delete Last_; First_ = 0; Last_ = 0; Length_ = 0; } else{ Last_ = Last_->Prev(); delete Last_->Next(); Last_->SetNext(0); --Length_; } return *this; } } ListElem* First(){ return First_; } ListElem* InspFirst()const{ return First_; } unsigned int FirstValue()const{ return First_->Value(); } ListElem* Last(){ return Last_; } ListElem* InspLast()const{ return Last_; } ListElem* Find(const unsigned int& value_){ if ( Length_ == 0 ){ std::cout << "Find Err: empty list" << std::endl; return 0; } ListElem* curr_ = First_; if ( (Last_->Value() - value_) < (value_ - First_->Value()) ){ curr_ = Last_; while ( (curr_ != First_) && (curr_->Value() != value_) ){ curr_ = curr_->Prev(); } if (curr_->Value() == value_){ return curr_; } else{ std::cout << "Find Err: value_ not found" << std::endl; return 0; } } else{ while ( (curr_ != Last_) && (curr_->Value() != value_) ){ curr_ = curr_->Next(); } if (curr_->Value() == value_){ return curr_; } else{ std::cout << "Find Err: value_ not found" << std::endl; return 0; } } } unsigned int Size()const{return Length_;} LinkedList& Clear(){ while(First_ != 0){ PopFront(); } std::cout << "Done clearing. Printme: " << std::endl; PrintMe(); return *this; } void PrintMe()const{ ListElem* elem_ = First_; std::cout << "LinkedList PrintMe Function: " << std::endl; std::cout << "List: "; for (unsigned int i = 0; i < Length_; elem_ = elem_->Next(), ++i){ std::cout << elem_->Value() << " "; } std::cout << "Length: " << Length_ << std::endl; std::cout << "First Pointer: " << First_ << std::endl; First_->PrintMe(); std::cout << "Last Pointer: " << Last_ << std::endl; Last_->PrintMe(); std::cout << "LinkedList PrintMe End" << std::endl; std::cout << std::endl; } LinkedList():Length_(0),First_(0),Last_(0){} ~LinkedList(){ std::cout << "List dtor PrintMe called: " << std:: endl; PrintMe(); Clear(); }private: unsigned int Length_; ListElem* First_; ListElem* Last_;};