虚拟析构函数和未定义的行为
这个问题与“何时/为什么我应该使用虚拟
析构函数?”不同。
struct B {
virtual void foo ();
~B() {} // <--- not virtual
};
struct D : B {
virtual void foo ();
~D() {}
};
B *p = new D;
delete p; // D::~D() is not called
问题:
- 这是否可以归类为未定义行为(我们知道
~D()
肯定不会被调用)? - 如果
~D()
为空怎么办?它会以任何方式影响代码吗? - 将
new[]
/delete[]
与B* p;
一起使用时,~D()
肯定会不是 被调用,无论析构函数的虚拟性如何。是吗 未定义的行为还是明确定义的行为?
This question is different than 'When/why should I use a virtual
destructor?'.
struct B {
virtual void foo ();
~B() {} // <--- not virtual
};
struct D : B {
virtual void foo ();
~D() {}
};
B *p = new D;
delete p; // D::~D() is not called
Questions:
- Can this be classified as an undefined behavior (we are aware that
~D()
is not going to be called for sure)? - What if
~D()
is empty. Will it affect the code in any way? - Upon using
new[]
/delete[]
withB* p;
, the~D()
will certainly not
get called, irrespective ofvirtual
ness of the destructor. Is it
an undefined behavior or well defined behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
何时/为何应使用虚拟析构函数?
遵循 Herb Sutters 指南:
这可以归类为未定义的行为吗(我们知道〜D()肯定不会被调用)?
它根据标准,这是未定义的行为,这通常会导致派生类析构函数不被调用并导致内存泄漏,但推测未定义行为的后续影响是无关紧要的,因为标准不保证任何事情在这方面。
C++03 标准:5.3.5 删除
5.3.5/1:
5.3.5/3:
如果
~D( )
是空的。它会以任何方式影响代码吗?根据标准,它仍然是未定义的行为,派生类析构函数为空可能只会使您的程序正常工作,但这又是特定实现的实现定义方面,从技术上讲,它仍然是未定义的行为。
请注意,这里不能保证不将派生类析构函数设为虚拟就不会导致对派生类析构函数的调用,并且这种假设是不正确的。根据标准,一旦您进入“未定义行为”区域,所有投注均无效。
请注意他的标准对未定义行为的说法。
C++03 标准:1.3.12 未定义行为 [defns.undefined]
如果仅不调用派生析构函数,则由上面引用中的粗体文本控制,这显然对每个实现都是开放的。
when/why should I use a virtual destructor?
Follow Herb Sutters guideline:
Can this be classified as an undefined behavior (we are aware that ~D() is not going to be called for sure) ?
It is Undefined Behavior as per the standard, which usually results in the Derived class destructor not being called and resulting in a memory leak, but it is irrelevant to speculate on after effetcs of an Undefined Behavior because standard doesn't gaurantee anything in this regard.
C++03 Standard: 5.3.5 Delete
5.3.5/1:
5.3.5/3:
What if
~D()
is empty. Will it affect the code in any way ?Still it is Undefined Behavior as per the standard, The derived class destructor being empty may just make your program work normally but that is again implementation defined aspect of an particular implementation, technically, it is still an Undefined Behavior.
Note that there is no gaurantee here that not making the derived class destructor virtual just does not result in call to derived class destructor and this assumption is incorrect. As per the Standard all bets are off once you are crossed over in Undefined Behavior land.
Note what he standard says about Undefined Behavior.
The C++03 Standard: 1.3.12 undefined behavior [defns.undefined]
If only derived destructor will be not called is governed by the bold text in the above quote, which is clearly left open for each implementation.
在要继承的类中确实没有理由使用非虚拟公共析构函数。请参阅本文,指南#4。
使用受保护的非虚拟析构函数和shared_ptrs(它们具有静态链接)或公共虚拟析构函数。
There really is no reason for a non-virtual public destructor in a class that is meant to be inherited from. Look at this article, Guideline #4.
Use either a protected non-virtual destructor and shared_ptrs(they have static linking), or a public virtual destructor.
正如其他人所重申的,这是完全未定义的,因为 Base 的析构函数不是虚拟的,任何人都不能发表任何声明。请参阅此帖子以获取标准参考和进一步讨论。
(当然,各个编译器有权做出某些承诺,但在这种情况下我还没有听说过任何相关内容。)
不过我觉得很有趣,在这种情况下我认为
malloc
和 <在某些情况下,code>free 的定义比new
和delete
更好。也许我们应该使用它们:-)给定一个基类和一个派生类,它们都没有任何虚拟方法,定义如下:
如果 D 有复杂的额外内容,则可能会发生内存泄漏成员,但除此之外,这是定义的行为。
As reaffirmed by others this is totally undefined because the Base's destructor is not virtual, and no statements can be made by anybody. See this thread for a reference to the standard and further discussion.
(Of course, individual compilers are entitled to make certain promises, but I haven't heard anything about that in this case.)
I find it interesting though, that in this case I think that
malloc
andfree
are better defined in some cases thannew
anddelete
. Perhaps we should be using those instead :-)Given a base class and a derived class, neither of which have any virtual methods, the following is defined:
You might get a memory leak if D had complex extra members, but apart from this is is defined behaviour.
(我想我可能会删除我的其他答案。)
有关该行为的所有内容都是未定义的。如果您想要更好地定义行为,您应该研究
shared_ptr
,或者自己实现类似的东西。以下是定义的行为,无论任何东西的虚拟性如何:shared_ptr 的主要技巧是模板化构造函数。
(I think I might delete my other answer.)
Everything about that behaviour is undefined. If you want better defined behaviour, you should look into
shared_ptr
, or implement something similar yourself. The following is defined behaviour, regardless of the virtual-ness of anything:The main trick of shared_ptr is the templated constructor.