title: Dynamic array in template class destructor crashing program
body: <p>I have a template class which contains a private, dynamic array of the template's type. In main I have a while loop that continues based on initial conditions input by the user and this all works fine (reading from/manipulating the array the whole time). </p>
  
  <p>Then after the while loop ends, the array isn't used anymore. But my program still does my last 2 couts and text file writes, then runs a simple lambda function (which doesn't use the array or its class), but then crashes before finishing:</p>
  
  <pre><code>newFile.close();
  return 0;
  </code></pre>
  
  <p>This doesn't actually take away any functionality but I can't figure out why it crashes instead of just ending, according to the debugger the program counter stops at the end of the array's destructor.</p>
  
  <p>My queueType.h:</p>
  
  <pre><code>template&lt;class Type&gt;
  class QueueType {
  public:
      QueueType();
      ~QueueType();
      QueueType(const QueueType&amp; other);
      Type&amp; getFront() {return queueArray[front];}
      void reposition();
      void addElement(Type);
      bool isEmpty() const {return numElements == 0;}
      bool isFull() const {return SIZE == numElements;}
      void updateWaitTimes(Type*&amp;, int&amp;, int&amp;);
  
      QueueType&lt;Type&gt;&amp; operator=(const QueueType other);
  
      friend void swap(QueueType&amp; first, QueueType&amp; second) {
          using std::swap;
          swap(first.front, second.front);
          swap(first.back, second.back);
          swap(first.numElements, second.numElements);
          swap(first.queueArray, second.queueArray);
      }
  private:
      static const int SIZE = 25;
      int front, back, numElements;
      Type *queueArray;
  };
  </code></pre>
  
  <p>I'm also implementing the rule of 3 (plus constructor shown) in this class as follows:</p>
  
  <pre><code>template&lt;class Type&gt;
  QueueType&lt;Type&gt;::QueueType() {
      queueArray = new Type[SIZE];
      front = back = numElements = 0;
  }
  
  template&lt;class Type&gt;
  QueueType&lt;Type&gt;::~QueueType() {
      delete [] queueArray;
  }
  
  template&lt;class Type&gt;
  QueueType&lt;Type&gt;::QueueType(const QueueType&amp; other) {
      front = other.front;
      back = other.back;
      numElements = other.numElements;
      std::copy(other.queueArray, other.queueArray + SIZE, queueArray);
  }
  
  template&lt;class Type&gt;
  QueueType&lt;Type&gt;&amp; QueueType&lt;Type&gt;::operator=(const QueueType other) {
      swap(*this, other);
      return *this;
  }
  </code></pre>
  
  <p>Using this main:</p>
  
  <pre><code>typedef void(*Action)();
  void iterateQueue(Action action) {
      action();
  }
  
  int main(int argc, char** argv) {
      QueueType&lt;CustomerType&gt; Line;
          while (... &lt; ...) {
              //various functions operate on Line, which produce no problems
          }
      cout &lt;&lt; ...
      newFile &lt;&lt; ...
      iterateQueue([] () {cout &lt;&lt; endl &lt;&lt; "I'm a lambda function!" &lt;&lt; endl;});
      cout &lt;&lt; "this never prints";
      return 0;
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> code added</p>