title: Desctuction of object is causing the crash of the program
body: <p>Here is a snippet of a considerably big C++ program:</p>
  
  <pre><code>map&lt;int, int&gt;::iterator class_it = docsCountPerClass.begin();
  for( ;class_it !=  docsCountPerClass.end(); class_it++) {
      cout &lt;&lt; "classid: " &lt;&lt; class_it-&gt;first &lt;&lt; '\n';
      vector&lt;term_importance&gt; ti;
      ti.reserve(termids.size());
      vector&lt;int&gt;::iterator term_it = termids.begin();
      for( ;(term_it != termids.end()); term_it++) {
          term_importance tmp;
          tmp.termid = *term_it;
          tmp.importnaceMeasure = chiSquareTest(*term_it, class_it-&gt;first);
          ti.push_back(tmp);
      }
      if(ti.size() != 0)
          std::sort(ti.begin(), ti.end());
      for(int i = 0; i &lt; ti.size(); i++)
          cout &lt;&lt; (ti.at(i)).termid &lt;&lt; " -- " &lt;&lt; (ti.at(i)).importnaceMeasure &lt;&lt; '\n';
      int ti_size_tmp = ti.size();
      for(int i = 0; i &lt; std::min(maxFeaturesPerClass, int(ti.size()) ); i++) {
          cout &lt;&lt; "* index access: " &lt;&lt; ti_size_tmp - 1 - i &lt;&lt; '\n';
          usefulTerms[class_it-&gt;first].push_back( (ti.at(ti_size_tmp - 1 - i)).termid );
      }
  </code></pre>
  
  <p>The problem I am facing when the loop completes its cycle and returns the vector ti should destruct. It does but while cleaning up the objects inside it it crashes, and it reports that its trying to call free() on invalid pointer.. I am unable to reproduce the problem using simpler code. Here is the backtrace using gdb:</p>
  
  <pre><code>#0  0x0012d422 in __kernel_vsyscall ()
  #1  0x00341651 in *__GI_raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
  #2  0x00344a82 in *__GI_abort () at abort.c:92
  #3  0x0037849d in __libc_message (do_abort=2, fmt=0x44cf98 "*** glibc detected *** %s: %s: 0x%s ***\n") at ../sysdeps/unix/sysv/linux/libc_fatal.c:189
  #4  0x00382591 in malloc_printerr (action=&lt;value optimized out&gt;, str=0x6 &lt;Address 0x6 out of bounds&gt;, ptr=0x81dc5e0) at malloc.c:6264
  #5  0x00383de8 in _int_free (av=&lt;value optimized out&gt;, p=&lt;value optimized out&gt;) at malloc.c:4792
  #6  0x00386ecd in *__GI___libc_free (mem=0x81dc5e0) at malloc.c:3738
  #7  0x00297741 in operator delete(void*) () from /usr/lib/libstdc++.so.6
  #8  0x08054377 in __gnu_cxx::new_allocator&lt;NaiveBayesClassifier::term_importance&gt;::deallocate (this=0xbffff194, __p=0x81dc5e0)
      at /usr/include/c++/4.4/ext/new_allocator.h:95
  #9  0x080522db in std::_Vector_base&lt;NaiveBayesClassifier::term_importance, std::allocator&lt;NaiveBayesClassifier::term_importance&gt; &gt;::_M_deallocate (this=0xbffff194, 
      __p=0x81dc5e0, __n=4433) at /usr/include/c++/4.4/bits/stl_vector.h:146
  #10 0x0805219e in ~_Vector_base (this=0xbffff194, __in_chrg=&lt;value optimized out&gt;) at /usr/include/c++/4.4/bits/stl_vector.h:132
  #11 0x08050139 in ~vector (this=0xbffff194, __in_chrg=&lt;value optimized out&gt;) at /usr/include/c++/4.4/bits/stl_vector.h:313
  #12 0x0804d32d in NaiveBayesClassifier::buildModel (this=0xbffff28c, maxFeaturesPerClass=100) at NaiveBayesClassifier.cpp:106
  #13 0x0805c357 in main (argc=2, argv=0xbffff3d4) at nbClassifyMain.cpp:15
  </code></pre>
  
  <p><strong>Edit 1:</strong>
  NaiveBayesClassifier.cpp : <a href="http://paste.bradleygill.com/index.php?paste_id=49445" rel="nofollow noreferrer">http://paste.bradleygill.com/index.php?paste_id=49445</a>
  NaiveBayesClassifier.h   : <a href="http://paste.bradleygill.com/index.php?paste_id=49446" rel="nofollow noreferrer">http://paste.bradleygill.com/index.php?paste_id=49446</a></p>
  
  <p><strong>Edit 2:</strong>
  I commented out <code>std::sort(ti.begin(), ti.end());</code> and found the error is gone. Now I am confused how std::sort() works.</p>