从基类析构函数调用派生类方法
Calling derived class method from base class destructor
>我在基类"SceneNode"中有以下析构函数代码片段,它试图清除其成员向量并删除从 SceneNode 派生的向量对象,但在删除子>到字符串行时出现错误,您能否指出正确的方向:
SceneNode::~SceneNode() {
LOG(DEBUG)<< "Node children size: " + to_string(children.size()) + " [" + toString() + "]";
// clean children nodes
for (SceneNode* child : children) {
LOG(DEBUG) << "Deleting ";
LOG(DEBUG) << "Deleting " + child->toString();
delete child;
LOG(DEBUG) << "Deleted " + child->toString();
}
当我调试时,它在那里向我显示一个正确的派生对象:
Name : child
Details:0x3204b40
Default:0x3204b40
Decimal:52448064
Hex:0x3204b40
Binary:11001000000100101101000000
Octal:0310045500
Name : lib::SceneNode
Details:{_vptr.SceneNode = 0x4e3430 <vtable for lib::ImageSceneNode+16>, id = {static npos = <optimized out>, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x3204c08 "New Node"}}, position = {x = 0, y = 0}, size = {x = 1920, y = 1080}, center = {x = 960, y = 540}, rotation = 0, alpha = 255, enabled = true, visible = true, deleted = false, parent = 0x22fca0, children = {<std::_Vector_base<lib::SceneNode*, std::allocator<lib::SceneNode*> >> = {_M_impl = {<std::allocator<lib::SceneNode*>> = {<__gnu_cxx::new_allocator<lib::SceneNode*>> = {<No data fields>}, <No data fields>}, _M_start = 0x0, _M_finish = 0x0, _M_end_of_storage = 0x0}}, <No data fields>}, animators = {<std::_Vector_base<lib::NodeAnimator*, std::allocator<lib::NodeAnimator*> >> = {_M_impl = {<std::allocator<lib::NodeAnimator*>> = {<__gnu_cxx::new_allocator<lib::NodeAnimator*>> = {<No data fields>}, <No data fields>}, _M_start = 0x0, _M_finish = 0x0, _M_end_of_storage = 0x0}}, <No data fields>}, sceneManager = 0x22fc98}
Default:{...}
Decimal:{...}
Hex:{...}
Binary:{...}
Octal:{...}
日志:
2015-03-09 15:33:39,685 DEBUG [default] [] [virtual lib::SceneNode::~SceneNode()] [..srclibscenenodeSceneNode.cpp:30] Node children size: 1 [SceneNode]
2015-03-09 15:33:39,685 DEBUG [default] [] [virtual lib::SceneNode::~SceneNode()] [..srclibscenenodeSceneNode.cpp:34] Deleting
2015-03-09 15:33:39,685 FATAL [default] CRASH HANDLED; Application has crashed due to [SIGSEGV] signal
谢谢!
首先更改以下内容:
LOG(DEBUG) << "Deleting " + child->toString();
对此:
LOG(DEBUG) << "Deleting " << child->toString();
然后删除此行,因为此时该变量已被销毁,您无法访问它(这是崩溃的原因):
LOG(DEBUG) << "Deleted " + child->toString();
相关文章:
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- 从基类实例调用派生类方法而不进行强制转换
- C++ 使用派生类方法更改基类数据成员
- 将派生类方法与基类unique_ptr一起使用
- 使用派生类C++方法的超类
- 从库类扩展,然后如何让库调用派生类方法
- 当应该调用派生类方法时,为什么要调用基类方法
- 抽象类的需求是什么?为什么要通过其基类访问派生类方法?在C++
- 如何轻松地将 Base 方法重定向到相同的派生类方法
- Gmock Base类方法中的派生类方法
- 如何调用精确派生类方法?
- 为什么可以从实例化基类对象的投射指针调用非静态派生类方法
- C++使用基类指针访问派生类方法
- 调用派生类方法时出现分段错误
- 从基类析构函数调用派生类方法
- 基类的指针数组:如何调用唯一的派生类方法
- 派生类方法的默认参数的最佳实践
- 从基类实例调用派生类方法
- C++ 继承:指向基类的派生类指针调用派生类方法
- 虚函数未解析为大多数派生类方法