Boost logo

Boost Users :

Subject: [Boost-users] time_facet and stream destruction causes crash
From: Mark Stapper (stark_at_[hidden])
Date: 2009-08-04 10:31:02


Hello,

I am using the time_facet class to be able to output time and date (in
millisecond precision) in some weird format someone cooked up.
I have to generate a timestamp everytime I send something to another
application.
The following testprogram reproduces my error (using Visual Studio 2005):
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;
using namespace std;
void imbue_crash_test(time_facet* formatter, ptime outputTime ){
    stringstream tempStream;
    tempStream.imbue(locale(cout.getloc(), formatter));
    tempStream<<outputTime<<endl;
    cout<<tempStream.str();
}

int main(){
  time_facet* timeFormatter=new time_facet();
  timeFormatter->format("%Y %m %d %H %M %S %f");
  imbue_crash_test(timeFormatter, microsec_clock::universal_time());
  imbue_crash_test(timeFormatter, microsec_clock::universal_time());
  system("pause");
  return 0;
}
 
which gives me an access violation while writing the "outputTime" to the
stringstream the second time.
When i stop the app and check the "formatter" instance i notice that the
value of "m_time_duration_format" gives "<BadPtr>" probably indicating
that the formatter has been deleted.
Does anyone have a clue as to why this would be?
The problem is resolved by making sure the stream is in the same scope
as the time_facet instance, and thus making sure they are destroyed at
the same time.
Any idea why this is? Is this a bug, a feature, or am I being stupid again?
Greetz,
Mark




Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net