title: Destructor for linked list invoked twice causing "pointer being freed was not allocated" error
body: <p>I have defined a print function in my main file. For what ever reason,
  if I call this function then my destructor appears to be called twice and then i get the "pointer being freed was not allocated" error</p>
  
  <p>here is main</p>
  
  <pre><code>//sandbox.cc
  //this file is for playing around with idea and modules
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "list.hpp"
  using namespace std;
  
  
  
  void print_list(list L){
      const listNode* temp = L.getHead();
      while (temp != nullptr){
          cout &lt;&lt; temp-&gt;index &lt;&lt; endl;
          temp = temp-&gt;next;
      }
  }
  
  int main(int argc, char** argv)
  {
  
    std::string buf("AABAABBBBCABBBBBBD");
    size_t LAB_index = 10;
    size_t max_match = 5;
  
    list ll = list(); 
    //all matches ar for AB
    ll.insert(1); 
    ll.insert(4);
    print_list(ll); //if you comment this line out there is no problem
  
    return 0;
  }
  </code></pre>
  
  <p>here is  header file for list</p>
  
  <pre><code>//list.hpp
  #ifndef LIST_HPP
  #define LIST_HPP
  #include &lt;stddef.h&gt; //for size_t
  #include &lt;utility&gt; //for std::pair
  #include &lt;string&gt;
  
  //A listNode is the core component in a list. its primary 
  //function is to store an index into the input buffer
  struct listNode{
          ////@: index into the input buffer
          listNode* next;
          size_t index;
  };
  
  //A list is a linked list composed of listNodes. It will be used by search buffer
  //for to track all the occurences of character sequences
  
  class list{
      private:
          //@:tail a pointer to listNode that is the tail of the list
          listNode* tail;
          //@:head a pointer to a listNode that is the head of the list
          listNode* head;
          //@:len is number of listNodes in the list
          size_t len;
      public:
          //#: inits list with head and tail pointing to null
          list( );
  
          //#: deletes each node in the list
          ~list();
  
  
          //@:i is an integer reprsenting an index 
          //#:will create a new list node with key set to i
          //#:and insert it at the end of the list. tail
          //#:points to this new element. and len incremented by one 
          void insert(size_t i);
  
          //#:removes the listNode pointed to by head 
          //#:and makes head point to the next of the recently
          //#:deleted head. Deletes the recently removed node
          void remove();
  
          //returns the list node pointed to by head
          const listNode* getHead();
  
          //@:LAB_index is the index where match begins in the LAB
          //@:L is the match length 
          //@:input_buf&amp; is a reference to the input string buffer
          //
          //#:search the entire list for the ListNode
          //#:whose index when scanend has the longest match
          //#:up to L.
          //#:returns a pair where first entry is match index 
          //and the second is the length of the match
          //return a pair (0,0) if no match found
          std::pair&lt;size_t,size_t&gt; get_longest_match(size_t LAB_index,\
                                              size_t max_match,\
                                              const std::string&amp; input_buf);
  
  
  };
  
  //SECTION: Helper Functions
  
  //This function helps get_longest_match
  //@:index specifies the index of the start of the match in the list
  //@:L
  //@:buf_index
  //
  //#:using brute force pattern matching, match as many characters
  //#:as possible starting at matchindex+2 and buf_index+2 since
  //#:these are assumed to have already been matched
  //#:return the length of the match (include the two characters matched initially)
  //#:thus lowest return value is 2
  std::pair&lt;size_t,size_t&gt;  matchindex(size_t index,\
                     size_t LAB_index, size_t max_match,\
                     const std::string&amp; input_buf);
  
  #endif
  </code></pre>
  
  <p>here is implementation. please focus in particulart on the ~list.
  As you can see it will print "___" upon being invoked. When the print statement reffered to above in main is left uncommented, this line is printed twice. I am wondering why? And how can i get rid of this behavior?</p>
  
  <pre><code>#include "list.hpp"
  #include &lt;iostream&gt;
  
  list::list()
  {
      head = nullptr;
      tail = nullptr;
      len = 0;
  }
  
  list::~list()
  {
      listNode* temp = head ;
      std::cout &lt;&lt; "____" &lt;&lt; std::endl;
      while (temp != nullptr){
          listNode* node = temp-&gt;next;
          std::cout &lt;&lt; temp-&gt;index &lt;&lt; std::endl;
          delete temp;
          temp = node;
      }
      head = nullptr;
  }
  
  void list::insert(size_t i){
      if (head == nullptr){
          head = new listNode{nullptr,i};
          tail = head;
          ++len;
      }
      else{
          listNode* new_node = new listNode{nullptr,i};
          tail-&gt;next = new_node;
          tail = new_node;
      }
  }
  
  void list::remove()
  {
      if (head != nullptr){
          listNode* new_head = head-&gt;next;
          delete head;
          head = new_head;
          --len;
      }
  }
  
  const listNode* list::getHead()
  {
      return head;
  }
  
  std::pair&lt;size_t,size_t&gt; list::get_longest_match(size_t LAB_index,\
                                             size_t max_match,\
                                             const std::string&amp; \
                                             input_buf)
  {
      std::pair&lt;size_t,size_t&gt; match_pair(0,0), canidate_match;
      //Case1 no match
      if (head == nullptr)
          return match_pair;
      listNode* temp = head;
      while (temp != nullptr){
          canidate_match = matchindex(temp-&gt;index,\
                              LAB_index,max_match,input_buf);
          if (canidate_match.second &gt; match_pair.second)
              match_pair = canidate_match;
          temp = temp-&gt;next;
      }
      return match_pair;
  }
  
  //SECTION: Helper Functions
  
  std::pair&lt;size_t,size_t&gt; matchindex(size_t index,\
            size_t LAB_index,size_t max_match, const std::string&amp; input_buf)
  {
      std::pair&lt;size_t,size_t&gt; match_pair;
      size_t matchlength = 2;
      while (matchlength &lt; max_match){
          if (input_buf[index] == input_buf[LAB_index]){
              ++matchlength;
              ++index;
              ++LAB_index;
          }
          else
              break;
      }
      match_pair = std::make_pair(index,matchlength);
      return match_pair;
  }
  </code></pre>