title: Understanding valgrind output: is it for ofstream or some other input?
body: <p>I am running a rather extensive piece of code for a simulation. I am getting some rather sporadic segfaults in my simulation, and they seem to be centered around the times I write to an external file.</p>
  <p>This is my valgrind output:</p>
  <pre><code>valgrind: m_mallocfree.c:307 (get_bszB_as_is): Assertion 'bszB_lo == bszB_hi' failed.
  valgrind: Heap block lo/hi size mismatch: lo = 31808, hi = 4.
  This is probably caused by your program erroneously writing past the
  end of a heap block and corrupting heap metadata.  If you fix any
  invalid writes reported by Memcheck, this assertion failure will
  probably go away.  Please try that before reporting this as a bug. 
  
  
  host stacktrace:
  ==29400==    at 0x5804C3A3: ??? (in /usr/libexec/valgrind/memcheck-amd64-linux)
  ==29400==    by 0x5804C4B7: ??? (in /usr/libexec/valgrind/memcheck-amd64-linux)
  ==29400==    by 0x5804C651: ??? (in /usr/libexec/valgrind/memcheck-amd64-linux)
  ==29400==    by 0x58055364: ??? (in /usr/libexec/valgrind/memcheck-amd64-linux)
  ==29400==    by 0x5800E05D: ??? (in /usr/libexec/valgrind/memcheck-amd64-linux)
  ==29400==    by 0x5800EA6B: ??? (in /usr/libexec/valgrind/memcheck-amd64-linux)
  ==29400==    by 0x580A64C7: ??? (in /usr/libexec/valgrind/memcheck-amd64-linux)
  ==29400==    by 0x580FF23A: ??? (in /usr/libexec/valgrind/memcheck-amd64-linux)
  
  sched status:
    running_tid=1
  
  Thread 1: status = VgTs_Runnable (lwpid 29400) 
  ==29400==    at 0x4C29F73: malloc (vg_replace_malloc.c:309)
  ==29400==    by 0x56C5C4C: __fopen_internal (iofopen.c:69)
  ==29400==    by 0x4EB34CF: std::__basic_file&lt;char&gt;::open(char const*, std::_Ios_Openmode, int) (basic_file.cc:238)
  ==29400==    by 0x4EEE7D9: std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;::open(char const*, std::_Ios_Openmode) (fstream.tcc:99)
  ==29400==    by 0x4EF0328: open (fstream:281)
  ==29400==    by 0x4EF0328: open (fstream:737)
  ==29400==    by 0x4EF0328: std::basic_ofstream&lt;char, std::char_traits&lt;char&gt; &gt;::basic_ofstream(std::string const&amp;, std::_Ios_Openmode) (fstream:663)
  ==29400==    by 0x40BC77: dumpOrientation(std::vector&lt;Polymer, std::allocator&lt;Polymer&gt; &gt;*, std::vector&lt;Particle, std::allocator&lt;Particle&gt; &gt;*, int, std::string, int, int, int) (misc.cpp:1641)
  ==29400==    by 0x405627: main (main.cpp:310)
  client stack range: [0x1FFEFFA000 0x1FFF000FFF] client SP: 0x1FFEFFE540
  valgrind stack range: [0x1008E7E000 0x1008F7DFFF] top usage: 11336 of 1048576
  </code></pre>
  <p>Looking at this output, I see that there is a problem with dumpOrientation.
  This is what dumpOrientation looks like:</p>
  <pre><code>void dumpOrientation( std::vector &lt;Polymer&gt;* PV, std::vector &lt;Particle&gt;* SV, int step, std::string filename, int x, int y, int z ) {
      
      std::ofstream dump_file (filename, std::ios::app); 
      dump_file &lt;&lt; &quot;START for Step &quot; &lt;&lt; step &lt;&lt; &quot;.\n&quot;;
      std::array &lt;int,3&gt; dummy_loc = {-1,-1,-1}; 
      std::cout &lt;&lt; &quot;about to go into the loops...&quot; &lt;&lt; std::endl;
      Particle t_particle (dummy_loc, &quot;monomer&quot;, 0);  
      for ( const Polymer&amp; pmer: (*PV) ) {
          for ( const Particle&amp; p: pmer.chain ) {
              std::cout &lt;&lt; &quot;particle is at &quot;; print(p.coords);  
              dump_file &lt;&lt; p.orientation &lt;&lt; &quot; | &quot;;
              std::array &lt;std::array&lt;int,3&gt; ,6&gt; ne_list = obtain_ne_list (p.coords, x, y, z) ; 
              std::cout &lt;&lt; &quot;neighbor list: &quot; &lt;&lt; std::endl;
              // print(ne_list); 
              for ( const std::array &lt;int,3&gt;&amp; ne: ne_list) {
                  std::cout &lt;&lt;&quot;Particle positions: &quot;; 
                  print (ne) ;
                  t_particle = ParticleReporter (PV, SV, ne); 
                  std::cout &lt;&lt; &quot;Reported~\n&quot;; 
                  if (t_particle.ptype == &quot;solvent&quot;){
                      dump_file &lt;&lt; t_particle.orientation &lt;&lt; &quot; | &quot;;  
                  }    
              }    
              dump_file &lt;&lt; &quot;\n&quot;; 
          }    
      }    
      
      dump_file &lt;&lt; &quot;END. \n&quot;;
      return;
  
  }
  </code></pre>
  <p>I have thrown in some couts to make know exactly what is being reached and what is not.
  Seems like the code is coming to a grinding halt at <code>std::ofstream</code> itself. I do not understand why it would come to a halt there. <code>filename</code> is a string that lets the dump function know which file to dump output to.</p>
  <p>My question is, is something going on with ofstream, or is there something worse wrong with my code?</p>
  <hr />
  <p><strong>EDIT:</strong></p>
  <p>Turns out the memory leak was hidden in a certain function. However, I do not understand what in the construction of this function was caused the leak.</p>
  <p>This is the function that has no leaks:
  I am going through a polymer, and finding all neighboring sites. Then perturbing those neighboring sites.</p>
  <pre><code>void OrientationFlipLocal ( std::vector &lt;Polymer&gt;* Polymers, std::vector &lt;Particle&gt;* Solvent, int x, int y, int z){
  
  
      // obtain the list of solvent particles neighboring the polymer 
      std::vector &lt;int&gt; solvent_indices;
      solvent_indices.reserve((*Solvent).size()); 
       
      for (const Polymer&amp; pmer: (*Polymers)) {
          for (const Particle&amp; p: pmer.chain) {
  
              std::array &lt;std::array &lt;int,3&gt;, 6&gt; ne_list = obtain_ne_list (p.coords, x, y, z);  
              for (const std::array &lt;int,3&gt;&amp; n: ne_list){ 
       
                  for (int j{0}; j&lt;static_cast&lt;int&gt;( (*Solvent).size() ); ++j){
                      if (n == (*Solvent)[j].coords){
                          solvent_indices.push_back(j); 
                      }    
                  }     
              }    
          }    
      }    
  
      // get rid of repeated indices
      std::sort ( solvent_indices.begin(), solvent_indices.end() ); 
      solvent_indices.erase ( std::unique ( solvent_indices.begin(), solvent_indices.end() ), solvent_indices.end() );
  
      // std::cout &lt;&lt; &quot;size of solvent_indices is &quot; &lt;&lt; solvent_indices.size() &lt;&lt; std::endl; 
  
      int stopping_idx = rng_uniform(0, static_cast&lt;int&gt;(solvent_indices.size() - 1) ); 
  
      // std::cout &lt;&lt; &quot;stopping_idx is &quot; &lt;&lt; stopping_idx &lt;&lt; std::endl;
      int counter = 0; 
      for (int idx: solvent_indices){
  
          if (counter == stopping_idx){
              break;
          }    
          // std::cout &lt;&lt; &quot;orientation is &quot; &lt;&lt; (*Solvent)[idx].orientation &lt;&lt;&quot;, location is: &quot;; 
          // print( (*Solvent)[idx].coords ); 
          (*Solvent)[idx].orientation = rng_uniform (0, 5);  
          // std::cout &lt;&lt; &quot;orientation is &quot; &lt;&lt; (*Solvent)[idx].orientation &lt;&lt; std::endl;
          ++counter;
  
      }
  
      return;
  
  }
  </code></pre>
  <p>However, I had implemented it in a different manner:</p>
  <pre><code>void OrientationFlipLocal ( std::vector &lt;Polymer&gt;* Polymers, std::vector &lt;Particle&gt;* Solvent, int x, int y, int z){
  
  
      // obtain the list of solvent particles neighboring the polymer 
      std::vector &lt;int&gt; solvent_indices; 
      for (Polymer&amp; pmer: (*Polymers)) {
          for (Particle&amp; p: pmer.chain) {
  
              std::array &lt;std::array &lt;int,3&gt;, 6&gt; ne_list = obtain_ne_list (p.coords, x, y, z); 
  
              for (const std::array&lt;int,3&gt;&amp; ne: ne_list){
                  if (IsSolvent (Polymers, ne)){
                      solvent_indices.push_back (SolventIndexReporter (Solvent, ne));
                  }
              }
          }
      }
  
      // get rid of repeated indices
      std::sort ( solvent_indices.begin(), solvent_indices.end() ); 
      solvent_indices.erase ( std::unique ( solvent_indices.begin(), solvent_indices.end() ), solvent_indices.end() );
  
      std::random_device rd;
      std::mt19937 g(rd());
  
      std::shuffle ( solvent_indices.begin(), solvent_indices.end(), g ); 
  
      // std::cout &lt;&lt; &quot;size of solvent_indices is &quot; &lt;&lt; solvent_indices.size() &lt;&lt; std::endl; 
  
      int stopping_idx = rng_uniform(0, static_cast&lt;int&gt;(solvent_indices.size() - 1) ); 
  
      // std::cout &lt;&lt; &quot;stopping_idx is &quot; &lt;&lt; stopping_idx &lt;&lt; std::endl;
      int counter = 0; 
      for (int idx: solvent_indices){
  
          if (counter == stopping_idx){
              break;
          }
          // std::cout &lt;&lt; &quot;orientation is &quot; &lt;&lt; (*Solvent)[idx].orientation &lt;&lt;&quot;, location is: &quot;; 
          // print( (*Solvent)[idx].coords ); 
          (*Solvent)[idx].orientation = rng_uniform (0, 5); 
          // std::cout &lt;&lt; &quot;orientation is &quot; &lt;&lt; (*Solvent)[idx].orientation &lt;&lt; std::endl;
          ++counter; 
  
      }
      return; 
  }
  
  int IsSolvent( std::vector &lt;Polymer&gt;* Polymers, std::array &lt;int,3&gt; to_check ){
  
      for (const Polymer&amp; pmer: (*Polymers) ){
          for (const Particle&amp; p: pmer.chain ){
  
              if (p.coords == to_check){
                  return 0;
              }
  
          }
      }
  
      return 1; 
  
  }
  
  int SolventIndexReporter ( std::vector &lt;Particle&gt;* Solvent, std::array &lt;int,3&gt; to_check){
  
      int idx {0}; 
      for (const Particle&amp; p: *Solvent){
          idx += 1; 
          if (p.coords == to_check){
              return idx;
          }
      }
  
      std::cout &lt;&lt;&quot;Something is profoundly fucked.&quot; &lt;&lt; std::endl;
      return -1; 
  
  }
  </code></pre>
  <p>In the above case, I see a memory leak. I am not sure what is causing this leak. What is the reason for the second implementation to lead to a leak, while the first one runs perfectly fine?</p>