title: Destructor causes the program to crash
body: <p>I am really struggling with the concept of using destructors with copy constructors. If I don't use a destructor the code works fine, as it does it automatically. If I do, I get an error saying 'Debug Assertion Failed!' and 'Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse). </p>
  
  <p>But I want to be able to understand how to use destructors. Here is the code below, I would very much appreciate help explaining what I have done incorrectly or need to do!<br>
      class Matrix {</p>
  
  <pre><code>private:
      int M;
      int N;
      double *data;
  
  public:
      Matrix();
      int getM() const { return M; }
      int getN() const { return N; }
  
      //CONSTRUCTOR
      Matrix(int sizeR, int sizeC,double * input_data)
      {
          M = sizeR; //Rows
          N = sizeC; //Columns
  
          data = new double[M*N]; //creation of 1D array, uses m&amp;n values
  
          cout &lt;&lt; "\nMatrix::Matrix(int sizeR, int sizeC, double * data_value) is invoked...\n\n";
  
          //ENTER DATA INTO MATRIX HERE:
          for(int i=0; i &lt; M*N; i++) //Loops for every data entry into 1D array, uses r&amp;c as referenece to
              data[i] = input_data[i];//Accesses each value at specific location, inputs value 'val'
          for(int i = 0; i &lt; M*N; i++) //Loops for every data entry into 1D array, uses r&amp;c as referenece to size
              cout &lt;&lt; data[i] &lt;&lt; " ";
      }
  
      //get function uses row and column from user
      double get(int i, int j)
      {
          return data[i*N+j];
      }
  
      double set(int i, int j, double val)
      {
          data[i*N+j] = val;
  
          cout &lt;&lt; "\n\nNEW MATRIX: ";
          for(int i = 0; i &lt; M*N; i++)//Loops for every data entry into 1D array, uses r&amp;c as referenece to size
              cout &lt;&lt; data[i] &lt;&lt; " ";
  
          return val;
      }
  
      Matrix(const Matrix&amp; oldMatrix)
      {
          cout¸&lt;&lt; "\nMatrix::Matrix(const Matrix&amp;) is invoked....";
          M = oldMatrix.getM();
          N = oldMatrix.getN();
          data = oldMatrix.data;
  
          cout &lt;&lt; "\n\n";
  
          //ENTER DATA INTO MATRIX HERE:
  
          for(int i = 0; i &lt; M*N; i++)//Loops for every data entry into 1D array, uses r&amp;c as referenece to size
              cout &lt;&lt; data[i] &lt;&lt; " ";
  
      }
  
      //DESTRUCTOR
      ~Matrix()
      {
          //delete data
          delete [] data;
          data = NULL;
          cout &lt;&lt; "\n\nMatrix::~Matrix() is invoked...\n\n";
      }
  
  
  
  };
  
  int main()
  {
      int sizeR, sizeC;
      double val;
  
      cout &lt;&lt; "Enter No. Rows: ";
      cin &gt;&gt; sizeR;
  
      cout &lt;&lt; "Enter No. Columns: ";
      cin &gt;&gt; sizeC;
  
      double * input_data;
  
  
      input_data = new double[sizeR*sizeC];
  
      //INPUTS VALUES TO ARRAY
      for(int i = 0; i &lt; sizeR*sizeC; i++)//Loops for every row
          input_data[i] = i;
  
      Matrix M1(sizeR, sizeC, input_data);
  
      cout &lt;&lt; "Enter row that value you are after is in: ";
      cin &gt;&gt; sizeR;
      cout &lt;&lt; " &amp; now the column that it is in: ";
      cin &gt;&gt; sizeC;
  
  
      cout &lt;&lt; "Change value: " &lt;&lt; M1.get(sizeR, sizeC) &lt;&lt; " to:";
      cin &gt;&gt; val;
      M1.set(sizeR, sizeC, val);
  
      //calls copy constructor
      M1 = Matrix(M1);
  }
  </code></pre>