1. <i id='XKXXS'><tr id='XKXXS'><dt id='XKXXS'><q id='XKXXS'><span id='XKXXS'><b id='XKXXS'><form id='XKXXS'><ins id='XKXXS'></ins><ul id='XKXXS'></ul><sub id='XKXXS'></sub></form><legend id='XKXXS'></legend><bdo id='XKXXS'><pre id='XKXXS'><center id='XKXXS'></center></pre></bdo></b><th id='XKXXS'></th></span></q></dt></tr></i><div id='XKXXS'><tfoot id='XKXXS'></tfoot><dl id='XKXXS'><fieldset id='XKXXS'></fieldset></dl></div>
      • <bdo id='XKXXS'></bdo><ul id='XKXXS'></ul>

      <small id='XKXXS'></small><noframes id='XKXXS'>

      <legend id='XKXXS'><style id='XKXXS'><dir id='XKXXS'><q id='XKXXS'></q></dir></style></legend>
    2. <tfoot id='XKXXS'></tfoot>

      1. Qt:是否“新建而不删除"?使用控件导致内存泄漏?

        Qt: does quot;new without deletequot; cause memory leaks with controls?(Qt:是否“新建而不删除?使用控件导致内存泄漏?)
            <tbody id='aF2q1'></tbody>
          <i id='aF2q1'><tr id='aF2q1'><dt id='aF2q1'><q id='aF2q1'><span id='aF2q1'><b id='aF2q1'><form id='aF2q1'><ins id='aF2q1'></ins><ul id='aF2q1'></ul><sub id='aF2q1'></sub></form><legend id='aF2q1'></legend><bdo id='aF2q1'><pre id='aF2q1'><center id='aF2q1'></center></pre></bdo></b><th id='aF2q1'></th></span></q></dt></tr></i><div id='aF2q1'><tfoot id='aF2q1'></tfoot><dl id='aF2q1'><fieldset id='aF2q1'></fieldset></dl></div>
          • <tfoot id='aF2q1'></tfoot>
              • <bdo id='aF2q1'></bdo><ul id='aF2q1'></ul>
                <legend id='aF2q1'><style id='aF2q1'><dir id='aF2q1'><q id='aF2q1'></q></dir></style></legend>
              • <small id='aF2q1'></small><noframes id='aF2q1'>

                  本文介绍了Qt:是否“新建而不删除"?使用控件导致内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在查看 Qt 示例 此处::>

                  在构造函数中,它们有:

                   Window::Window(){编辑器 = 新 QTextEdit();//内存泄漏?QPushButton *sendButton = new QPushButton(tr("&Send message"));//内存泄漏?连接(发送按钮,信号(点击()),这个,插槽(发送消息()));QHBoxLayout *buttonLayout = new QHBoxLayout();//内存泄漏?buttonLayout->addStretch();buttonLayout->addWidget(sendButton);buttonLayout->addStretch();QVBoxLayout *layout = new QVBoxLayout(this);//内存泄漏?布局-> addWidget(编辑器);布局-> addLayout(buttonLayout);setWindowTitle(tr("自定义类型发送"));}

                  那些带有注释的行

                  //内存泄漏?

                  那些不是内存泄漏吗?

                  如果是这样,既然 Window 类没有构造函数,那么我应该让所有这些变量(编辑器已经是)Window 成员变量?

                  或者..Qt 是否会在超出范围时在内部删除"这些成员变量?

                  解决方案

                  从 C++14 开始,您可以使用 std::make_unique() 方便的函数模板,用于创建 std::unique_ptr<> 拥有小部件的独家所有权.然后,在将小部件传递给 addLayout() 的那一刻,您通过调用 release() 使智能指针放弃所有权:

                  auto buttonLayout = std::make_unique();//...//这里可能会抛出异常//...布局-> addLayout(buttonLayout.release());

                  I was looking at Qt example here:

                  and inside the constructor, they have:

                   Window::Window()
                   {
                       editor = new QTextEdit();   // Memory leak?
                       QPushButton *sendButton = new QPushButton(tr("&Send message")); // Memory leak?
                  
                       connect(sendButton, SIGNAL(clicked()), this, SLOT(sendMessage()));
                  
                       QHBoxLayout *buttonLayout = new QHBoxLayout();  // Memory leak?
                       buttonLayout->addStretch();
                       buttonLayout->addWidget(sendButton);
                       buttonLayout->addStretch();
                  
                       QVBoxLayout *layout = new QVBoxLayout(this);    // Memory leak?
                       layout->addWidget(editor);
                       layout->addLayout(buttonLayout);
                  
                       setWindowTitle(tr("Custom Type Sending"));
                   }
                  

                  Those lines with comments

                  // Memory leak?
                  

                  aren't those memory leaks?

                  If so, since the Window class has no constructor, then I should make all of those variables (editor already is) Window member variables ?

                  Or..does Qt internally "delete" those member variables when it goes out of scope?

                  解决方案

                  As of C++14 you can use the std::make_unique() convenience function template for creating an std::unique_ptr<> that has exclusive ownership of the widget. Then, at the moment of passing the widget to addLayout(), you make the smart pointer give up ownership by calling release():

                  auto buttonLayout = std::make_unique<QHBoxLayout>(); 
                  // ...
                  // an exception could be thrown here
                  // ...
                  layout->addLayout(buttonLayout.release());
                  

                  这篇关于Qt:是否“新建而不删除"?使用控件导致内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                    <tbody id='YBB9X'></tbody>

                  <small id='YBB9X'></small><noframes id='YBB9X'>

                  <tfoot id='YBB9X'></tfoot>
                    • <bdo id='YBB9X'></bdo><ul id='YBB9X'></ul>
                      • <legend id='YBB9X'><style id='YBB9X'><dir id='YBB9X'><q id='YBB9X'></q></dir></style></legend>

                          <i id='YBB9X'><tr id='YBB9X'><dt id='YBB9X'><q id='YBB9X'><span id='YBB9X'><b id='YBB9X'><form id='YBB9X'><ins id='YBB9X'></ins><ul id='YBB9X'></ul><sub id='YBB9X'></sub></form><legend id='YBB9X'></legend><bdo id='YBB9X'><pre id='YBB9X'><center id='YBB9X'></center></pre></bdo></b><th id='YBB9X'></th></span></q></dt></tr></i><div id='YBB9X'><tfoot id='YBB9X'></tfoot><dl id='YBB9X'><fieldset id='YBB9X'></fieldset></dl></div>