title: Memory leak, vector pushback c++
body: <p>I have a class <code>Bar</code>, its constructor initializes a <code>std::vector</code> of type <code>Foo</code> (another class).</p>
  
  <p>Bar.cpp</p>
  
  <pre><code>Bar::Bar(int n) {
   for(int i = 0; i &lt; n; i++) {
      Foo foo;
      foo.somefunction(i);
      vec.push_back(foo) //this should insert foo into the vector
    }
  }
  </code></pre>
  
  <p>Bar.h</p>
  
  <pre><code>class Foo;
  class Bar {
   std::vector&lt;Foo&gt; vec;
  };
  </code></pre>
  
  <p>When I debug, the first iteration of the construction works fine. <code>foo</code> is created, <code>foo.somefunction()</code> is run fine, and <code>foo</code> is pushed into <code>vec</code>.</p>
  
  <p>The second interation seems to work fine as well, but the program crashes when it goes back to start the third iteration.</p>
  
  <p>I get <code>_BLOCK_TYPE_IS_VALID(pHead-&gt;nBlockUse)</code> error and <code>HEAP CORRUPTION DETECTED</code>.</p>
  
  <p><code>Foo</code> is a class that contains an dynamically created array, nothing special.
  Something like this:</p>
  
  <pre><code>Foo::Foo() {
    solution = new float[size];
    // some function that initializes the elements of solution
  }
  </code></pre>
  
  <p>and a regular destructor <code>~Foo() {delete [] solution;}</code>. I don't think the problem comes from <code>Foo</code>.</p>