Hi, I get a run-time error when I run the following code; std::string LocaleFormattedDateTime (::boost::posix_time::ptime pt) { std::string s; std::ostringstream ss; //::boost::posix_time::time_facet * tf = new ::boost::posix_time::time_facet; ::boost::posix_time::time_facet tf; //std::locale custom_locale (std::locale(""), tf); std::locale custom_locale (std::locale(""), &tf); // custom_locale takes ownership of the tf facet ss.imbue (custom_locale); //(*tf).format("%Y%m%d-%H%M%S-%f"); // date time tf.format("%Y%m%d-%H%M%S-%f"); // date time ss << pt; // don't explicitly delete tf - what's wrong with a stack variable? why this runtime error? What is the point of new() above and //what if I don't delete allocated memory here. return ss.str(); } void CurrentDateTimeWithMicroseconds(char s[], int sz = 100) { using namespace boost::posix_time; using namespace std; using boost::date_time::date_facet; ptime t = microsec_clock::local_time(); ostringstream ss; //time_facet *df = new time_facet(); time_facet df; //std::locale loc (std::locale(""), df); std::locale loc (std::locale(""), &df); ss << LocaleFormattedDateTime(t) ; memset(s,0,sz); strcpy(s,ss.str().c_str()); } Can you tell me the problem? I modified this code after copying this from some online source. Thanks in advance, Asif
The comment in your code indicates that the custom_locale object "owns" the facet you pass it. Therefore when the locale object goes out of scope, it will delete the facet. But if the facet was allocated on the stack, you aren't allowed to delete it. So that is an error. Stack-allocated objects are automatically destroyed when the stack frame is destroyed (and similarly with nested scopes...). You don't get to delete them explicitly. So don't pass their addresses into objects who will do that. Andy On 2/13/2012 6:01 AM, asif saeed wrote:
Hi,
I get a run-time error when I run the following code;
std::string LocaleFormattedDateTime (::boost::posix_time::ptime pt) { std::string s; std::ostringstream ss;
//::boost::posix_time::time_facet * tf = new ::boost::posix_time::time_facet; ::boost::posix_time::time_facet tf; //std::locale custom_locale (std::locale(""), tf); std::locale custom_locale (std::locale(""),&tf);
// custom_locale takes ownership of the tf facet ss.imbue (custom_locale); //(*tf).format("%Y%m%d-%H%M%S-%f"); // date time tf.format("%Y%m%d-%H%M%S-%f"); // date time ss<< pt; // don't explicitly delete tf - what's wrong with a stack variable? why this runtime error? What is the point of new() above and //what if I don't delete allocated memory here. return ss.str(); }
void CurrentDateTimeWithMicroseconds(char s[], int sz = 100) { using namespace boost::posix_time; using namespace std; using boost::date_time::date_facet;
ptime t = microsec_clock::local_time(); ostringstream ss; //time_facet *df = new time_facet(); time_facet df; //std::locale loc (std::locale(""), df); std::locale loc (std::locale(""),&df); ss<< LocaleFormattedDateTime(t) ; memset(s,0,sz); strcpy(s,ss.str().c_str()); }
Can you tell me the problem?
I modified this code after copying this from some online source.
Thanks in advance, Asif
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
asif saeed
-
Michael Chisholm