谁负责删除构面? [英] Who is responsible for deleting the facet?
问题描述
我有一个函数使用Boost.DateTime库生成当前GMT/UTC日期和时间字符串(
I have a function that uses the Boost.DateTime library for generating the current GMT/UTC date and 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();
}
这主要基于 Boost.DateTime的示例:
//example to customize output to be "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"
我的代码运行良好,但是现在由于包含new
的这些特殊行而让我感到不安:
My code worked nicely, but now 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"));
如您所见,Boost.DateTime中没有delete
,因此我以某种方式认为必须delete
date_facet
.我用std::unique_ptr
包裹了new
ed time_facet
对象.
As you can see, there is no delete
in Boost.DateTime's so I somehow presumed that it is imperative for me to delete
the date_facet
. I used std::unique_ptr
to wrap the new
ed 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"));
但是,我遇到了段错误,您可以在此处中看到.我还尝试过手动delete
ed new
ed指针,但仍然遇到相同的错误(对不起,无法在Coliru中重现错误).
However, I am getting segfault errors, as you can see in here. I have also tried manually delete
ing the new
ed pointer, and am still getting the same errors (sorry, can't reproduce error in Coliru).
time_facet
指针在构造std::locale
对象时作为参数传递,所以我很困惑谁是负责delete
构面的人.
The time_facet
pointer is passed as an argument when constructing an std::locale
object, so I'm confused who's the one responsible for delete
ing the facet.
这是我的问题的核心:
- 我需要
delete
time_facet
还是std::locale
对象负责delete
对其进行操作?
- Am I required to
delete
thetime_facet
or is thestd::locale
object responsible fordelete
ing it?
请注意,boost::posix_time::time_facet
是从boost::date_time::date_facet
派生的,而boost::date_time::date_facet
又是从std::locale::facet
派生的.尽管我的问题特定于time_facet
,但是这个问题可能会推广到std::locale::facet
.
Please note that boost::posix_time::time_facet
is derived from boost::date_time::date_facet
which is, in turn, derived from std::locale::facet
. This question might generalized to std::locale::facet
, though my problem is specific to time_facet
.
以下是有关std::locale
的构造函数的一些文档:
Here are some docs on std::locale
's constructors:
- MSDN
- cppreference.com
推荐答案
我需要删除time_facet还是std :: locale对象 负责删除它吗?
Am I required to delete the time_facet or is the std::locale object responsible for deleteing the it?
只要time_facet
是从std::locale::facet
派生的,就不需要删除time_facet
. std::locale::facet
是所有方面都应从该类派生的基类,该类实现了引用计数的形式.标准说:
You're not required to delete the time_facet
so long as time_facet
derives from std::locale::facet
, which it should. The std::locale::facet
is a base class that all facets should derive from that implement a form of reference counting. The standard says this:
§22.3.1.6
一旦通过调用从语言环境对象获得构面引用
use_facet<>
,该引用仍然可用,并且结果来自 它的成员函数可能会被缓存和重复使用,只要某些 语言环境对象是指该构面.
Once a facet reference is obtained from a locale object by calling
use_facet<>
, that reference remains usable, and the results from member functions of it may be cached and re-used, as long as some locale object refers to that facet.
一旦未使用构面的所有引用,std::locale
的析构函数将管理和删除对构面的引用(如果其引用计数为0).
Once all references of the facet are not being used, the destructor of std::locale
will manage and delete references to the facet if its ref count is 0.
所有这些都在C ++ 11标准的§22.3.1.1.2中指定.说明:
This is all specified in §22.3.1.1.2 in the C++11 standard. Where it states:
构造函数的refs参数用于生命周期管理.
The refs argument to the constructor is used for lifetime management.
-对于refs == 0
,实现将执行delete
static_cast<locale::facet*>(f)
(其中f是指向构面的指针)
当包含该构面的最后一个语言环境对象被销毁时;为了
refs == 1
,该实现永远不会破坏该方面.
— For refs == 0
, the implementation performs delete
static_cast<locale::facet*>(f)
(where f is a pointer to the facet)
when the last locale object containing the facet is destroyed; for
refs == 1
, the implementation never destroys the facet.
这篇关于谁负责删除构面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!