title: C++: Freeing the memory from vector of objects
body: <p>I have this situation where my program starts to slow down and eventually halts. I am guessing it is due to not claiming memory in a right way. Can anyone please help me what is the correct way to free memory in this case?</p>
  
  <p>simplified definitions:</p>
  
  <pre><code>typedef struct {
      std::string name;
      std::vector&lt;metric_t&gt; metrics;
  } region_t;
  
  
  typedef struct {
      std::string name;
      std::vector&lt;region_t&gt; regions;
  } data_t;
  
  
  typedef struct {
      std::string name;
      std::vector&lt;double&gt; means;
  } metric_t;
  </code></pre>
  
  <p>Main loop:</p>
  
  <pre><code>for(int i = 0; i &lt; 100; i++)
  {
      data_t data;
      prepare_data(&amp;data);
  
      /* Usage of data here */
  }
  </code></pre>
  
  <p>prepare data function:</p>
  
  <pre><code>void prepare_data(data_t * data)
  {
      region_t new_region;
  
      data-&gt;regions.push_back(new_region);
  
      for(int j=0; j&lt; 100000; j++)
      {
          metric_t new_metric;
          /* put some data in new_metric */
          data-&gt;regions.back().metrics.push_back(new_metric);
      }
  }
  </code></pre>