Qt如何处理堆栈分配的对象
How does Qt handle stack-allocated objects
我不明白Qt如何删除所有QObject的孩子没有双重删除的东西,如果它是静态分配的。
基本上,如果我按照通常的方式做,它看起来像这样:
QWidget Window(nullptr);
QPushButton* button = new QPushButton(&Window);
Window.show();
return App.exec();
//When app ends, Window gets deleted
//because it was statically allocated
//And then, Window deletes button because it's its child.
但是我也可以这样做而不会崩溃:
QWidget Window(nullptr);
QPushButton button(&Window);
Window.show();
return App.exec();
//When app ends, button then Window get deleted
//because they were statically allocated
//And then, Window (should) delete button AGAIN because it's its child, thus crashing
//the program. But it doesn't. Why ?
Qt知道我是如何创建QPushButton的,还是我错过了什么?
当一个QObject
被销毁时,它会从它的父级(如果它有父级)中移除自己。因此,当Window
被破坏时,它不会试图破坏QPushButton
,因为按钮不再在窗口的子列表中。
相关文档如下。它还提到,如果对象的声明顺序与父/子关系顺序不匹配,则可能确实导致对象被销毁两次。这是件坏事。
http://doc.qt.io/qt-5/objecttrees.html相关文章:
- 从堆栈分配的原始指针构造智能指针
- 在什么情况下,两个堆栈分配的结构对象的 this 点指向同一个地址?
- 如何模板化堆栈分配的多态指针数组到接口,包括派生类型的相应点?
- 堆栈分配的类类型.为什么两个 ID 实例的地址相同?
- C++析构函数调用两次,堆栈分配的复合对象
- 了解通过引用传递取消引用指针时C++堆/堆栈分配
- C++,在对象内分配多个数据时,堆栈分配是否更有效? 在下面的程序中,类A_Heap的效率会更低吗?
- 何时在函数中声明堆栈分配变量?
- 使用 std::map 的递归堆栈分配如何工作?
- tcmalloc 与纯堆栈分配性能有多接近
- 安全分配堆栈分配的阵列
- C++ 中的黑白堆分配对象和堆栈分配对象的性能差异
- 跟踪(堆栈分配)对象
- 堆栈分配的确切时间
- 未初始化的值是由堆栈分配 - Qt - C++创建的
- 用于堆栈分配对象的C++虚拟析构函数内联
- C STD :: BAD_ALLOC来自堆栈分配
- 是使用COM创建的对象,将采用分配或堆栈分配的内存
- C++ 中针对大型的堆栈分配
- 堆栈分配的向量如何在 C++ 中扩展