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

前端 未结 5 1771
[愿得一人]
[愿得一人] 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:13

    If there is an exception thrown between new and addWidget then yes there is a memory leak. Otherwise the parent control takes ownership of the memory.

    QHBoxLayout *buttonLayout = new QHBoxLayout();  // Memory leak?
    //make sure you don't throw here
    buttonLayout->addWidget(sendButton);
    

提交回复
热议问题