title: c++ sort vector of pointers error
body: <p>I have a vector vec that I need to sort every time I put an element inside it</p>
  
  <p>so when I put the first Upgrade* inside the vector I have no problems</p>
  
  <p>but when I put the second Upgrade* inside it and the sort routine is called I have a runtime error</p>
  
  <p>this is how I put elements and call sort every time I insert</p>
  
  <pre><code>std::vector&lt;Upgrade*&gt; stack = getStack();
  
  stack.push_back(element);
  
  std::sort(stack.begin(), stack.end(), CostBenefitUpgradeOrder());
  </code></pre>
  
  <p>and this is my comparator</p>
  
  <pre><code>struct CostBenefitUpgradeOrder {
      bool operator ()(const Upgrade * u1, const Upgrade * u2) const {
  
          const UpgradeType upgradeType1 = u1-&gt;getUpgradeType();
          const UpgradeType upgradeType2 = u2-&gt;getUpgradeType();
  
          int price1 = PriceUtil::getPrice(upgradeType1);
          int price2 = PriceUtil::getPrice(upgradeType2);
  
          if (price2 &lt; price1)
              return true;
          else
              return false;
      }
  }
  </code></pre>
  
  <p>and this is the error </p>
  
  <p><img src="https://i.stack.imgur.com/ELext.png" alt="runtime error"></p>
  
  <p><strong>I have noticed that it only happens when I execute the program in Debug mode!!</strong></p>