Qt: does “new without delete” cause memory leaks with controls?

前端 未结 5 1798
[愿得一人]
[愿得一人] 2020-12-14 18:24

I was looking at Qt example here:

and inside the constructor, they have:

 Window::Window()
 {
     editor = new QTextEdit();   // Memory leak?
     Q         


        
5条回答
  •  清歌不尽
    2020-12-14 19:20

    No, the addWidget() function will keep ownership of the widget. It will then destroy the widgets it owns.

    Additionally you can read here that:

    As with QObjects, QWidgets can be created with parent objects to indicate ownership, ensuring that objects are deleted when they are no longer used. With widgets, these parent-child relationships have an additional meaning: Each child widget is displayed within the screen area occupied by its parent widget. This means that when you delete a window widget, all the child widgets it contains are also deleted.

提交回复
热议问题