从析构函数体内部或外部调用叶类的成员函数有区别吗?
我有一个特定的情况,很难在代码片段中隔离,但我可以解释它......
我有一个类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
如果 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.