title: Segfault when imbueing stringstream with custom locale
body: <p>Based on the answers <a href="https://stackoverflow.com/questions/7276826/c-format-number-with-commas">here</a> to the question of how to format numbers with a comma, I am using the following code:</p>
  
  <pre><code>#include &lt;locale&gt;
  #include &lt;stringstream&gt;
  
  namespace
  {
  
  class comma_numpunct : public std::numpunct&lt;char&gt;
  {
    protected:
      virtual char do_thousands_sep() const
      {
          return ',';
      }
  
      virtual std::string do_grouping() const
      {
          return "\03";
      }
  };
  
  }
  
  
  /* Convert number to string using a comma as the thousands separator. */
  string thousands(const int x) {
  
      /* custom locale to ensure thousands separator is comma */
      comma_numpunct* comma_numpunct_ptr = new comma_numpunct();
      std::locale comma_locale(std::locale(), comma_numpunct_ptr);
  
      stringstream ss;
      ss.imbue(comma_locale); // apply locale to stringstream
      ss &lt;&lt; x;
  
      /* garbage collection */
      delete comma_numpunct_ptr;
  
      return ss.str();
  }
  </code></pre>
  
  <p>GDB gives the following backtrace:</p>
  
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  0x0000000000000021 in ?? ()
  (gdb) bt
  #0  0x0000000000000021 in ?? ()
  #1  0x00007ffff7701535 in std::locale::_Impl::~_Impl() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
  #2  0x00007ffff770166d in std::locale::~locale() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
  #3  0x000000000044b93e in thousands (x=358799) at ../globals_utilities.cpp:104
  #4  0x000000000045650d in main (argc=1, argv=0x7fffffffdf58) at ../main.cpp:67
  </code></pre>
  
  <p>So, the problem is (I believe) with my attempt at freeing the <code>new</code>'d memory. But I don't know how to work around this.  (I can't use <code>std::unique_ptr</code> because I'm not always compiling with C++11 support.)</p>
  
  <p>How can I fix the segfault without leaking memory?</p>