title: How to fix "free(): double free detected in tcache 2" error?
body: <p>So, I have created a derived class template called unorderedSet that uses a pointer member variable to hold the location of an array on the heap (using new).</p>
  <p>I am trying to overload the arithmetic operators to find both the intersection and union of two different unorderedSet objects, and the code I have written so far (with the help of another user on this site) manages that. However, upon running the program I get the error in the title. I will post the relevant parts of my code below.</p>
  <pre><code>template &lt;class elemType&gt;
  class unorderedSet: public unorderedArrayListType&lt;elemType&gt;
  {
  public:
    void insertAt(int location, const elemType&amp; insertItem);
    void insertEnd(const elemType&amp; insertItem);
    void replaceAt(int location, const elemType&amp; repItem);
    const unorderedSet&lt;elemType&gt; operator+(const unorderedSet&lt;elemType&gt;&amp;);
    // Function to overload the binary operator + to find the union of a pair/group of sets
    // Postcondition: Finds the union of the sets
  
    const unorderedSet&lt;elemType&gt; operator-(const unorderedSet&lt;elemType&gt;&amp;);
    // Function to overload the binary operator - to find the intersection of a pair/group of sets
    // Postcondition: Finds the intersection of the sets
    
    unorderedSet(int size = 100);
    unorderedSet(const unorderedSet&lt;elemType&gt;&amp; otherSet);
    ~unorderedSet();
    
  protected:
    elemType *set;
    int length;
    int maxSize;
  };
  
  template &lt;class elemType&gt;
  const unorderedSet&lt;elemType&gt; unorderedSet&lt;elemType&gt;::operator+(const unorderedSet&lt;elemType&gt;&amp; otherSet)
  {
    unorderedSet&lt;elemType&gt; unSet(this-&gt;length + otherSet.length); // Initializes new set to hold values of the union set
  
    for (int i = 0; i &lt; this-&gt;length; i++)
      unSet.insertEnd(this-&gt;list[i]); // Assigns all values of the activating operand to the union set using insertEnd
    
    for (int i = 0; i &lt; otherSet.length; i++)
      unSet.insertEnd(otherSet.list[i]); // Calls insertEnd() to both check for duplicate values and add unique values to the union of the sets
  
    return unSet; // Should return the union set, but dumps the core at the moment
  } // end operator overload
  
  template &lt;class elemType&gt;
  unorderedSet&lt;elemType&gt;::unorderedSet(int size) : unorderedArrayListType&lt;elemType&gt;(size)
  {
    if (size &lt;= 0)
    {
      cout &lt;&lt; &quot;The array size must be positive. Creating an array of the size 100. &quot; &lt;&lt; endl;
  
      this-&gt;maxSize = 100;
    }
    else
      this-&gt;maxSize = size;
  
      this-&gt;length = 0;
  
      set = new elemType[this-&gt;maxSize];
  }
  
  template &lt;class elemType&gt;
  unorderedSet&lt;elemType&gt;::~unorderedSet()
  {
    delete [] set;
  }
  
  template &lt;class elemType&gt;
  unorderedSet&lt;elemType&gt;::unorderedSet(const unorderedSet&lt;elemType&gt;&amp; otherSet)
  {
    this-&gt;maxSize = otherSet.maxSize;
    this-&gt;length = otherSet.length;
  
    set = new elemType[this-&gt;maxSize];
  
    for (int j = 0; j &lt; length; j++)
      set[j] = otherSet.set[j];
  }
  </code></pre>
  <p>The following code comes from my test client program.</p>
  <pre><code>int main() 
  {
    int intArr1[6] = {0, 1, 2, 3, 4, 5};
    unorderedSet&lt;int&gt; testIntSet1;
  
    for (int i = 0; i &lt; (sizeof(intArr1) / sizeof(intArr1[0])); i++)
      testIntSet1.insertEnd(intArr1[i]);
    // Some more code before the function call
    
    int intArr2[6] = {0, 1, 3, 6, 7, 9};
    unorderedSet&lt;int&gt; testIntSet2, testIntSet3;
  
    for (int i = 0; i &lt; (sizeof(intArr2) / sizeof(intArr2[0])); i++)
      testIntSet2.insertEnd(intArr2[i]);
  
    testIntSet3 = testIntSet1 + testIntSet2;
    // Some more code
  }
  </code></pre>
  <p>I also have an assignment operator overload function in the base class of this class's base class. The code is as follows</p>
  <pre><code>template &lt;class elemType&gt;
  const arrayListType&lt;elemType&gt;&amp; arrayListType&lt;elemType&gt;::operator= (const arrayListType&lt;elemType&gt;&amp; otherList)
  {
    if (this != &amp;otherList)    //avoid self-assignment
    {
      delete [] list;
      this-&gt;maxSize = otherList.maxSize;
      this-&gt;length = otherList.length;
   
      list = new elemType[this-&gt;maxSize];
  
      for (int i = 0; i &lt; this-&gt;length; i++)
        list[i] = otherList.list[i];
    }
  
    return *this;
  }
  </code></pre>
  <p>I have attempted to create another version of this in my unorderedSet class, but when I run it the testIntSet3 variable outputs nothing (not even some random garbage). I decided to remove it since the code seemed to at least function properly before.</p>