- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
It's widely known您可以使用 shared_ptr
来存储指向不完整类型的指针,只要在构造 shared_ptr
期间可以删除该指针(具有明确定义的行为) .例如,PIMPL 技术:
struct interface
{
interface(); // out-of-line definition required
~interface() = default; // public inline member, even if implicitly defined
void foo();
private:
struct impl; // incomplete type
std::shared_ptr<impl> pimpl; // pointer to incomplete type
};
[ main.cpp ]
int main()
{
interface i;
i.foo();
}
[接口(interface).cpp]
struct interface::impl
{
void foo()
{
std::cout << "woof!\n";
}
};
interface::interface()
: pimpl( new impl ) // `delete impl` is well-formed at this point
{}
void interface::foo()
{
pimpl->foo();
}
这作为
“删除对象”
“所有者对象”(*) 在 pimpl( new impl )
,并在类型删除后存储在 shared_ptr
的构造过程中创建shared_ptr
中。这个“所有者对象”稍后用于销毁指向的对象。这就是为什么提供 interface
的内联析构函数应该是安全的。
问题:标准在何处保证其安全?
(*) 不是标准中的删除器,见下文,但它调用自定义删除器或调用 delete-expression。该对象通常存储为簿记对象的一部分,应用类型删除并在虚函数中调用自定义删除器/删除表达式。此时,delete-expression 也应该是合式的。
引用github仓库中的最新草案(94c8fc71,修订N3797),[util.smartptr.shared.const]
template<class Y> explicit shared_ptr(Y* p);
3 Requires:
p
shall be convertible toT*
.Y
shall be a complete type. The expressiondelete p
shall be well formed, shall have well defined behavior, and shall not throw exceptions.4 Effects: Constructs a
shared_ptr
object that owns the pointerp
.5 Postconditions:
use_count() == 1 && get() == p
.6 Throws:
bad_alloc
, or an implementation-defined exception when a resource other than memory could not be obtained.
注意:对于这个 ctor,shared_ptr
不需要拥有删除器。对于deleter,标准似乎意味着自定义删除器,例如您在构造期间作为附加参数提供(或 shared_ptr
获取/共享一个来自另一个 shared_ptr
,例如通过复制分配)。另请参阅(另请参阅 [util.smartptr.shared.const]/9)。实现(boost、libstdc++、MSVC,我想每个理智的实现)总是存储一个“所有者对象”。
由于删除器是一个自定义删除器,shared_ptr
的析构函数是根据delete
( delete-expression) 如果没有自定义删除器:
[util.smartptr.shared.dest]
~shared_ptr();
1 Effects:
- If
*this
is empty or shares ownership with anothershared_ptr
instance (use_count() > 1
), there are no side effects.- Otherwise, if
*this
owns an objectp
and a deleterd
,d(p)
is called.- Otherwise,
*this
owns a pointerp
, anddelete p
is called.
我假设意图是需要一个实现来正确删除存储的指针,即使在shared_ptr
dtor 的范围内,delete-expression 是格式错误或会调用 UB。 (delete-expression 必须格式正确并且在构造函数中具有明确定义的行为。)所以,问题是
问题:这是哪里要求的?
(或者我是不是太挑剔了,很明显,实现需要使用“所有者对象”?)
最佳答案
Question: Where is this required?
如果不需要,析构函数将具有未定义的行为,并且标准不习惯要求未定义的行为:-)
如果满足构造函数的前提条件,那么析构函数将不会调用未定义的行为。实现如何确保未指定,但您可以假设它是正确的,并且您不需要知道如何做到这一点。如果不期望实现做正确的事,那么析构函数将有一个先决条件。
(Or am I just too nit-picky and it's obvious somehow that the implementations are required to use a "owner object"?)
是的,必须创建一些额外的对象来拥有指针,因为引用计数(或其他簿记数据)必须在堆上而不是任何特定 shared_ptr
实例的一部分,因为它可能需要比任何特定实例都长寿。所以是的,有一个额外的对象,它拥有指针,您可以将其称为所有者对象。如果用户没有提供删除器,则该所有者对象只调用 delete
。例如:
template<typename T>
struct SpOwner {
long count;
long weak_count;
T* ptr;
virtual void dispose() { delete ptr; }
// ...
};
template<typename T, typename Del>
struct SpOwnerWithDeleter : SpOwner<T> {
Del del;
virtual void dispose() { del(this->ptr); }
// ...
};
现在 shared_ptr
有一个 SpOwner*
并且当计数下降到零时它调用虚函数 dispose()
调用 delete
或调用删除器,具体取决于对象的构造方式。是构造SpOwner
还是构造SpOwnerWithDeleter
的决定是在构造shared_ptr
时做出的,并且当shared_ptr
被销毁,因此如果它需要处理拥有的指针,那么它将做正确的事。
关于c++ - shared_ptr 的 dtor 是否需要使用 "deleter"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846444/
当类具有普通构造函数和/或普通析构函数时,C++ 标准定义了一些非常具体的行为。 例如,根据标准的 §3.8/1: The lifetime of an object of type T ends w
我正在学习一些关于格式化字符串错误利用的教程,在某些时候他们谈到覆盖 dtors 表。但这是我在使用 nm 时发现的: 080495a8 d _DYNAMIC 0804969c d _GLOBAL_O
我正在学习一些关于格式化字符串错误利用的教程,在某些时候他们谈到覆盖 dtors 表。但这是我在使用 nm 时发现的: 080495a8 d _DYNAMIC 0804969c d _GLOBAL_O
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题?通过 editing this post 添加详细信息并澄清问题. 1年前关闭。 Improve this
在 C++ 中>=11,是否可以安全地从析构函数中抛出异常,即仅在没有异常处于事件状态时才抛出异常? 我试过: #include #include #include struct foo{
编译器给出链接错误,要求提供 Base 纯虚拟析构函数的定义。 class Base { public: virtual ~Base() = 0; }; class Derived : pub
我想确保析构函数的副作用保留在作为 RVO 候选者的函数中。我的目标是在进入和退出时对堆栈进行快照,并显示预期的堆栈变量。这段代码似乎适用于 C++11 而无需使用特定于编译器的选项,但我不知道如何在
我在 gcc 4.4.5 上尝试了以下代码。 如果成员“data”不存在,代码执行正常,但在它存在的情况下,它会崩溃。当派生类的 dtor 不是虚拟时,它也不会崩溃。 我知道在这两种情况下行为都是未定
我的代码中有以下类。换句话说,CTor中有一个创建线程的静态对象(单例),当它的DTor被调用时,它有一些工作要做在这个线程的上下文中(DTor为线程放置一些工作)。 我面临的问题是,当调用 B 的
我正在阅读《黑客,剥削的艺术》一书。书中有一节解释了.dtors和.ctors的使用。 我正在尝试重现书中的一个练习,但在我的可执行文件中我没有这些部分。起初我以为问题是我正在为 64 位编译,但现在
这个问题在这里已经有了答案: Move-ctor and copy-ctor not called (3 个答案) C++11 move constructor not called, defaul
正如我在Move constructor/operator=中询问的那样,过了一段时间,我同意并接受了这个问题的正确答案,我只是在想,是否有类似“移动析构函数” 这样的东西会在每次移动的对象上调用会有
Herb Sutter 在他的文章中 http://www.gotw.ca/gotw/047.htm指出我们不能在析构函数中使用uncaught_exception, // Why the wron
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭10
我正在尝试从接口(interface)向下转型到派生类,但我的虚拟 dtor 杀死了它? class IFOO { public: virtual ~IFOO(){}; virtual size_
我想通过如下初始化语句实现数组对象的初始化。 TestClass array[5] = { TestClass("test1"), TestClass("test2"), Te
在我正在调试的代码库中遇到了这个析构函数。 ManagerImpl::~ManagerImpl() { // don't go away if some thread is still hit
这个问题在这里已经有了答案: When to use virtual destructors? (20 个答案) 关闭 8 年前。 我正在尝试找出 C++ 中类继承的技巧,并且我构建了一个示例项目:
在下面的例子中,类 test1 没有 ctor/dotr 而 test2 有空的 ctor 和dtor. 在分配大量test1 和test2 时,空的ctor 和dtor 会降低性能吗? #inclu
我有一个非常复杂的软件,我随机崩溃(主要是在 Release模式下,我无法轻松构建 SSCCE),并在 boost::signal1 的析构函数中跟踪崩溃. 该信号对象由多个对象使用(订阅/取消订阅)
我是一名优秀的程序员,十分优秀!