c++ - Who is responsible for deleting the facet? -


i have function uses boost.datetime library generating current gmt/utc date , time string (live example).

std::string get_curr_date() {     auto date = boost::date_time::second_clock<boost::posix_time::ptime>::universal_time();      boost::posix_time::time_facet* facet = new boost::posix_time::time_facet("%a, %d %b %y %h:%m:%s gmt");      std::ostringstream os;     os.imbue(std::locale(os.getloc(), facet));     os << date;      return os.str(); } 

this based on boost.datetime's example:

//example customize output "longweekday longmonthname day, year" //                                  "%a %b %d, %y" date d(2005,jun,25); date_facet* facet(new date_facet("%a %b %d, %y")); std::cout.imbue(std::locale(std::cout.getloc(), facet)); std::cout << d << std::endl; // "saturday june 25, 2005" 

my code worked nicely, i'm feeling uneasy because of these particular lines containing new:

  • boost::posix_time::time_facet* facet = new boost::posix_time::time_facet("%a, %d %b %y %h:%m:%s gmt");

  • date_facet* facet(new date_facet("%a %b %d, %y"));

as can see, there no delete in boost.datetime's somehow presumed imperative me delete date_facet. used std::unique_ptr wrap newed time_facet object.

std::unique_ptr<boost::posix_time::time_facet> facet(new boost::posix_time::time_facet("%a, %d %b %y %h:%m:%s gmt")); 

however, getting segfault errors, can see in here. have tried manually deleteing newed pointer, , still getting same errors (sorry, can't reproduce error in coliru).

the time_facet pointer passed argument when constructing std::locale object, i'm confused who's 1 responsible deleteing facet.

so here core of question:

  • am required delete time_facet or std::locale object responsible deleteing it?

please note boost::posix_time::time_facet derived boost::date_time::date_facet is, in turn, derived std::locale::facet. question might generalized std::locale::facet, though problem specific time_facet.

here docs on std::locale's constructors:

am required delete time_facet or std::locale object responsible deleteing it?

you're not required delete time_facet long time_facet derives std::locale::facet, should. std::locale::facet base class facets should derive implement form of reference counting. standard says this:

§ 22.3.1.6

once facet reference obtained locale object calling use_facet<>, reference remains usable, , results member functions of may cached , re-used, long locale object refers facet.

once references of facet not being used, destructor of std::locale manage , delete references facet if ref count 0.

this specified in §22.3.1.1.2 in c++11 standard. states:

the refs argument constructor used lifetime management.

refs == 0, implementation performs delete static_cast<locale::facet*>(f) (where f pointer facet) when last locale object containing facet destroyed; refs == 1, implementation never destroys facet.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -