从具有非虚拟父级的虚拟类继承的正确方法继续
我的问题是建立在这个问题的基础上的: 从具有非虚拟父级的虚拟类继承的正确方法。
我的理解是否正确,在问题中描述的情况下,新分配对象的三部分和二部分正在泄漏,因为它们没有被破坏?
来源:
#include <iostream>
struct One
{
~One() {
std::cout << "~One()\n";
}
};
struct Two : One
{
virtual ~Two() {
std::cout << "~Two()\n";
}
virtual void test() = 0;
};
struct Three : Two
{
virtual ~Three() {
std::cout << "~Three()\n";
}
virtual void test() {
std::cout << "Three::test()\n";
}
};
int main()
{
Two* two = new Three;
two->test();
One* one = two;
delete one;
}
My question is building on this question: Correct way to inherit from a virtual class with non-virtual parent.
Is my understanding right that in the case which is described in the question, the Three and Two part of new allocated object are leaking because they are not destructed?
Source:
#include <iostream>
struct One
{
~One() {
std::cout << "~One()\n";
}
};
struct Two : One
{
virtual ~Two() {
std::cout << "~Two()\n";
}
virtual void test() = 0;
};
struct Three : Two
{
virtual ~Three() {
std::cout << "~Three()\n";
}
virtual void test() {
std::cout << "Three::test()\n";
}
};
int main()
{
Two* two = new Three;
two->test();
One* one = two;
delete one;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是正确的。内存泄漏的定义是您无法删除您创建的某些内容(因此您负责管理其生命周期)的情况。
正如该问题的答案所示,
删除一个
会调用未定义的行为(在大多数情况下会可能会转化为常规的旧内存泄漏,但情况可能会像 鼻恶魔)因为指定对象的运行时类型与其静态(声明的)类型不匹配,并且静态类型没有虚拟析构函数。C++ 标准中适用的部分是:
解决方案是要么将所有析构函数声明为虚拟的,要么不通过指向 One 的指针删除对象。
Yes, that's correct. The definition of a memory leak is a situation where you are unable to delete something that you created (and whose lifetime you are therefore responsible for managing).
As the answers to that question indicate,
delete one
invokes undefined behavior (which in most cases will probably translate to a regular old memory leak, but things could be as bad as nasal demons) because the runtime type of the specified object does not match its static (declared) type, and the static type does not have a virtual destructor.The applicable section from the C++ standard is this one:
The solution is either to declare all of the destructors
virtual
, or not to delete the objects through a pointer toOne
.