从析构函数体内部或外部调用叶类的成员函数有区别吗?

发布于 2024-12-07 03:06:07 字数 413 浏览 0 评论 0原文

我有一个特定的情况,很难在代码片段中隔离,但我可以解释它......

我有一个类 A : public B 和 A 有一个某种类型 foo * f 的成员指针。在 A 的虚拟析构函数中,我有类似的内容:

A::~A() { shutdown(); }

其中, shutdown 是非虚拟的,看起来像这样: void A::shutdown() {delete f;}

事实证明,在运行时这给了我一个“称为终止的纯虚拟方法,在没有活动异常中止(核心转储)的情况下调用”,但是如果我从主体中删除 shutdown()析构函数并直接调用它,然后让析构函数运行...我不再明白这一点...

什么可能导致这种行为?我尝试过使用 gdb 进行单步调试,但它太大了,我什至不知道要寻找什么。任何想法将不胜感激!

I have a specific case which is hard to isolate in a code snippet here but I can explain it...

I have a class A : public B and A has a member pointer of some type foo * f. In the virtual destructor for A, I have something like:

A::~A() { shutdown(); }

where, shutdown is nonvirtual and looks something like:
void A::shutdown() {delete f;}

it turns out at runtime this gives me a "pure virtual method called terminate called without an active exception Aborted (core dumped)" but if I remove shutdown() from the body of the destructor and call it directly, and then let the destructor run...I no longer get this...

What could possibly be causing behavior of this sort? I've tried stepping with gdb but it's huge and I'm not even sure what to look for. Any ideas would be very much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请持续率性 2024-12-14 03:06:07

shutdown 要么是继承树上某处的纯虚函数,要么它调用继承树上某处的纯虚函数。无论是直接的还是间接的,但这就是发生的事情。

检查执行路径以查看调用的位置并删除它。

Either shutdown is a pure virtual function somewhere up the inheritance tree, or it calls another member function which is pure virtual somewhere up the inheritance tree. Either directly or indirectly, but that's what happens.

Check the execution path to see where the call is made and get rid of it.

柠北森屋 2024-12-14 03:06:07

如果 A 同时具有 shutdown 方法和指针 f,则根本不调用 shutdown()。直接在析构函数中删除f即可。

If A has both the shutdown method AND the pointer f, don't call shutdown() at all. Just delete f in the destructor directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文