title: Segmentation fault on vector of objects
body: <p>I'm currently trying to programm a neural network header file to train neural networks, the NN.h file contains all the neural network stuff.
  NN.h also includes another header file I have programmed myself, named Matrix.h which contains a class to handle all the matrix stuff with memory management.</p>
  
  <p>The memory management works fine when I don't use vectors for the Matrix objects but as soon as I use them with vectors I get Segmentation fault error.</p>
  
  <p>This is the program output:</p>
  
  <pre><code>Matrix destruction started
  0 0 0 
  Matrix destruction started
  0 0 0 
  Matrix destruction started
  0 0 0 
  0 0 0 
  0 0 0 
  Matrix destruction started
  2 2 2 
  Matrix destruction started
  10 10 10 
  Matrix destruction started
  9 
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>NN.h:</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;stdlib.h&gt;
  #include "matrix.h"
  #include &lt;time.h&gt;
  #include &lt;string&gt;
  class NN{
    private:
      std::vector&lt;int&gt; info;
      std::vector&lt;Matrix&lt;double&gt; &gt; layer;
      std::vector&lt;Matrix&lt;double&gt; &gt; weights;
      std::vector&lt;Matrix&lt;double&gt; &gt; bias;
      std::vector&lt;Matrix&lt;double&gt; &gt; result;
      std::vector&lt;Matrix&lt;double&gt; &gt; temp;
      double learnigRate;
  public:
      NN(std::vector&lt;int&gt; &amp;layers){
          info = layers;
          layer.reserve(layers.size());
          for (size_t i = 0;i &lt; layers.size();i++){
              layer.push_back(Matrix&lt;double&gt; (layers[i],1,0));
          }
          weights.reserve(layers.size()-1);
          for (size_t i = 0;i &lt; (layers.size()-1);i++){
              weights.push_back(Matrix&lt;double&gt; (layers[i],layers[i+1],0));
          }
          bias.reserve(layer.size()-1);
          for (size_t i= 0; i &lt; (layers.size()-1);i++){
              bias.push_back(Matrix&lt;double&gt; (layers[i+1],1,2));
          }
          result.push_back(Matrix&lt;double&gt; (layers[layers.size()-1],1,10));
  
          temp.push_back(Matrix&lt;double&gt; (1,1,9));
      }
      ~NN(){
          for (size_t i = 0;i &lt; info.size();i++){
              delete &amp;layer[i];
          }
          for (size_t i = 0;i &lt; (info.size()-1);i++){
              delete &amp;weights[i];
          }
          for (size_t i= 0; i &lt; (info.size()-1);i++){
              delete &amp;bias[i];
          }
          delete &amp;result[0];
          delete &amp;temp[0];
      }
  };
  </code></pre>
  
  <p>Matrix.h:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;exception&gt;
  template &lt;class W&gt; class Matrix{
  private:
      int state = 0;
      int printError(Matrix&lt;W&gt; &amp;a){
          if(state != 0 &amp;&amp; a.state != 0){
              std::cerr &lt;&lt;"matrix.h error : " &lt;&lt; "Both matrices not correctly initialized!" &lt;&lt; std::endl;
              return 1;
          }
          if(state != 0){
              std::cerr &lt;&lt;"matrix.h error : " &lt;&lt; "Matrix 1 not correctly initialized!" &lt;&lt; std::endl;
              return 1;
          }
          if(a.state != 0){
              std::cerr &lt;&lt;"matrix.h error : " &lt;&lt; "Matrix 2 not correctly initialized!" &lt;&lt; std::endl;
              return 1;
          }
          return 0;
      }
  public:
  W ** matrix;
  int size_x,size_y;
  template &lt;typename X&gt; Matrix(int size_tx,int size_ty,X value){
    W temp = (W) value;
    size_x = size_tx;
    size_y = size_ty;
    int x = 0;
    try{
     matrix = new W*[size_y];
    }
    catch(const std::bad_alloc&amp;)
    {
     std::cerr &lt;&lt; "Could not allocate matrix: bad_alloc()" &lt;&lt; std::endl;
     state = 1;
     return;
    }
    try{
    for(x = 0; x &lt; size_y;x++){
      matrix[x] = new W[size_x];
    }
    }
    catch (const std::bad_alloc&amp;)
    {
      std::cerr &lt;&lt; "Could not allocate matrix: bad_alloc()" &lt;&lt; std::endl;
      for(int i = 0; i &lt; x-1;i++){
          delete [] matrix[i];
      }
      state = 1;
      return;
    }
    for(int i = 0; i &lt; size_y; i++){
      for(int j = 0;j&lt; size_x;j++){
          matrix[i][j] = temp;
      }
    }
    state = 0;
  }
  ~Matrix(){
    if (state == 0)
      freeMatrix();
    }
  int freeMatrix(){
      std::cout &lt;&lt; "Matrix destruction started" &lt;&lt; std::endl;
      printMatrix();
      for(int i = 0; i &lt; size_y;i++){
          delete [] matrix[i];
      }
      delete [] matrix;
      return 0;
  }
  int printMatrix(){
    for(int i = 0; i &lt; size_y; i++){
      for(int j = 0;j&lt; size_x;j++){
        std::cout &lt;&lt; matrix[i][j] &lt;&lt; " ";
      }
      printf("\n");
    }
  return 0;
  }
  };
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include "matrix.h"
  #include "NN.h"
  using namespace std;
  
  int main(){
    vector&lt;int&gt; net (3);
    net = {3,3};
    NN n(net);
    return 0;
  }
  </code></pre>
  
  <p>These are shortened versions of my code, but the relevant parts should still be there.</p>
  
  <p>There is still the same error when I delete the destructor of the NN class,
  however when I remove the Matrix destructor there is not error, but I would like to solve the problem with a clean solution that respects memory management.</p>
  
  <p>If you need more information feel free to ask me.</p>