title: vector of list memory leak in C++ on OSX 10.8
body: <p>I'm having memory problems with in implementing a vector&lt; list&lt; size_t> >. Here's the code:</p>
  
  <pre><code>struct structure{
      vector&lt; list&lt;size_t&gt;  &gt; Ar;
  
      structure(int n){
          Ar.reserve(n);
          for(size_t i =0; (int) i&lt;  n;i++){
              list&lt;size_t&gt; L;
              Ar.push_back(L);
          }
      }
  
      ~structure(){
         vector&lt;list&lt;size_t&gt; &gt;::iterator it = Ar.begin();
         while(it &lt; Ar.end()){
             (*it).clear();
             ++it;
         }
         Ar.clear();
      }
  
  
  };
  
  int main() {
  
      for(size_t k = 0; k &lt;100 ; k++){    
          structure test_s = structure(1000*(100 - k));
      }
      return 0;
  }
  </code></pre>
  
  <p>The physical memory allocation to this program should be going down as time progresses, because less and less memory is being allocated to test_s, through the use of 100 - k in the constructor. This isn't what I'm observing! Rather, the physical memory increases around half way through the run! </p>
  
  <p>Since I am using this code in a bigger program that eats up a huge amount of memory, this is a bit of a catastrophe!</p>
  
  <p>There are two details that I find strange: </p>
  
  <p>Firstly, there is no progressive increment in the physical memory usage, even though the size of the object changes at every stage of the for loop, rather the memory increases suddenly at around the 50th iteration of the for loop. This happens consistently each run I do (and I've done many!). The iteration at which the memory increases is not random! </p>
  
  <p>Secondly, when I pass a static size_t (e.g. 10000) to the structure(size_t) constructor, I don't get the problem anymore. As you can probably guess, a static value isn't very useful for my code, as I need to be able to dynamically allocate the size of the structure object.</p>
  
  <p>I am compiling with g++ on macos 10.8.3. I haven't tried compiling on another platform, as I would prefer to keep working on my Mac. </p>
  
  <p>All of the memory management tools I have tried (Apple Instruments and Valgrind) haven't been particularly helpful. Valgrind only returns references to libraries and not to the program itself.</p>
  
  <p>Any help would be much appreciated!!</p>
  
  <p>Cheers,
  Plamen</p>