Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set

double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set

Scheduled Pinned Locked Moved Solved General and Desktop
qdialogdeletelater
21 Posts 7 Posters 3.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J.HilkJ J.Hilk

    @LRDPRDX you're right.

    it's more sinister.

    the QDialog implementation of delete on close is a delete this call. 😱 very naughty, even if allocated on the heap.

    so its not double free call, it's corruption.

    class MyPDialog : public QDialog
    {
    public:
        MyPDialog(QWidget *parent = nullptr) : QDialog(parent) {}
    
        void deleteOnClose() {delete this;}
    
        ~MyPDialog()
        {
            qInfo() << "~MyPDialog()";
        }
    };
    
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QApplication app(argc, argv);
    
        MyPDialog d;
        d.deleteOnClose();
    //    d.setAttribute( Qt::WA_DeleteOnClose, true );
    //    int result = d.exec();
        qInfo() << "After exec" /*<< result*/;
    
        return app.exec();
    }
    
    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by kshegunov
    #7

    @J-Hilk said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

    it's more sinister.

    There's nothing sinister, the OP already provided the answer.

    @LRDPRDX said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

    I suspect that the deleteLater() function is not allowed to be called on a stack variable but to confirm I (probably) need to go through the QCoreApplication::postEvent() function which is too complicated to me as a user and not a Qt-developer.

    No, delete is not allowed on a stack variable. You don't need to be a Qt developer to know this, and it has nothing to do with how you call the delete -- from the outside, from the inside (i.e. deleting this), through a custom event or without.

    Read and abide by the Qt Code of Conduct

    JonBJ J.HilkJ L 3 Replies Last reply
    2
    • kshegunovK kshegunov

      @J-Hilk said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

      it's more sinister.

      There's nothing sinister, the OP already provided the answer.

      @LRDPRDX said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

      I suspect that the deleteLater() function is not allowed to be called on a stack variable but to confirm I (probably) need to go through the QCoreApplication::postEvent() function which is too complicated to me as a user and not a Qt-developer.

      No, delete is not allowed on a stack variable. You don't need to be a Qt developer to know this, and it has nothing to do with how you call the delete -- from the outside, from the inside (i.e. deleting this), through a custom event or without.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #8

      @kshegunov said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

      No, delete is allowed on a stack variable

      Do you perchance mean delete is not allowed on a stack variable?

      Oh, you updated your post after I posted this :)

      kshegunovK 1 Reply Last reply
      0
      • JonBJ JonB

        @kshegunov said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

        No, delete is allowed on a stack variable

        Do you perchance mean delete is not allowed on a stack variable?

        Oh, you updated your post after I posted this :)

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #9

        @JonB said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

        Do you perchance mean delete is not allowed on a stack variable?

        Indeed, it was a typo.

        Read and abide by the Qt Code of Conduct

        JonBJ 1 Reply Last reply
        0
        • kshegunovK kshegunov

          @JonB said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

          Do you perchance mean delete is not allowed on a stack variable?

          Indeed, it was a typo.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #10

          @kshegunov
          A "typo" is a misspelling, not a complete reversal of a comment's intention! :D

          And I agree it's about time somebody said you just must not delete a stack variable, period. [Whether WA_DeleteOnClose docs might mention that is what this does, and so should only be used on heap instances, is another matter. I was never clear just what exactly WA_DeleteOnClose did, though I used it.

          kshegunovK 1 Reply Last reply
          0
          • kshegunovK kshegunov

            @J-Hilk said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

            it's more sinister.

            There's nothing sinister, the OP already provided the answer.

            @LRDPRDX said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

            I suspect that the deleteLater() function is not allowed to be called on a stack variable but to confirm I (probably) need to go through the QCoreApplication::postEvent() function which is too complicated to me as a user and not a Qt-developer.

            No, delete is not allowed on a stack variable. You don't need to be a Qt developer to know this, and it has nothing to do with how you call the delete -- from the outside, from the inside (i.e. deleting this), through a custom event or without.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #11

            @kshegunov said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

            There's nothing sinister, the OP already provided the answer.

            no matter what, telling a class to commit suicide is sinister in my books :P


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @LRDPRDX you're right.

              it's more sinister.

              the QDialog implementation of delete on close is a delete this call. 😱 very naughty, even if allocated on the heap.

              so its not double free call, it's corruption.

              class MyPDialog : public QDialog
              {
              public:
                  MyPDialog(QWidget *parent = nullptr) : QDialog(parent) {}
              
                  void deleteOnClose() {delete this;}
              
                  ~MyPDialog()
                  {
                      qInfo() << "~MyPDialog()";
                  }
              };
              
              
              int main(int argc, char *argv[])
              {
                  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                  QApplication app(argc, argv);
              
                  MyPDialog d;
                  d.deleteOnClose();
              //    d.setAttribute( Qt::WA_DeleteOnClose, true );
              //    int result = d.exec();
                  qInfo() << "After exec" /*<< result*/;
              
                  return app.exec();
              }
              
              L Offline
              L Offline
              LRDPRDX
              wrote on last edited by
              #12

              @J-Hilk Cool -_-

              the QDialog implementation of delete on close is a delete this call

              Just to ensure : do you know this from this (line 613) ?

              J.HilkJ 1 Reply Last reply
              0
              • L LRDPRDX

                @J-Hilk Cool -_-

                the QDialog implementation of delete on close is a delete this call

                Just to ensure : do you know this from this (line 613) ?

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #13

                @LRDPRDX yes, its exactly where my debugger stopped.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • JonBJ JonB

                  @kshegunov
                  A "typo" is a misspelling, not a complete reversal of a comment's intention! :D

                  And I agree it's about time somebody said you just must not delete a stack variable, period. [Whether WA_DeleteOnClose docs might mention that is what this does, and so should only be used on heap instances, is another matter. I was never clear just what exactly WA_DeleteOnClose did, though I used it.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #14

                  @JonB said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

                  A "typo" is a misspelling, not a complete reversal of a comment's intention! :D

                  I added the comma after the fact, which introduced the missing negation, so *shrug* happens.

                  @J-Hilk said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

                  no matter what, telling a class to commit suicide is sinister in my books :P

                  Your books are wrong. We are a free-haven! ;)

                  Read and abide by the Qt Code of Conduct

                  JonBJ 1 Reply Last reply
                  3
                  • kshegunovK kshegunov

                    @JonB said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

                    A "typo" is a misspelling, not a complete reversal of a comment's intention! :D

                    I added the comma after the fact, which introduced the missing negation, so *shrug* happens.

                    @J-Hilk said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

                    no matter what, telling a class to commit suicide is sinister in my books :P

                    Your books are wrong. We are a free-haven! ;)

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #15

                    @kshegunov said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

                    I added the comma

                    Eats, Shoots & Leaves ;-)

                    1 Reply Last reply
                    2
                    • M Offline
                      M Offline
                      mpergand
                      wrote on last edited by mpergand
                      #16

                      At the end of the QDialog:exec there are:

                      if (deleteOnClose)
                              delete this;
                      return res;
                      

                      On Mac i got:
                      malloc: *** error for object 0x7ffeefbffa60: pointer being freed was not allocated
                      so it crashes at the delete instruction and never returns.

                      1 Reply Last reply
                      2
                      • JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #17

                        @LRDPRDX OOI, which compiler, compiler options (debug/release) & platform are you on for your error message?

                        1 Reply Last reply
                        0
                        • kshegunovK kshegunov

                          @J-Hilk said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

                          it's more sinister.

                          There's nothing sinister, the OP already provided the answer.

                          @LRDPRDX said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

                          I suspect that the deleteLater() function is not allowed to be called on a stack variable but to confirm I (probably) need to go through the QCoreApplication::postEvent() function which is too complicated to me as a user and not a Qt-developer.

                          No, delete is not allowed on a stack variable. You don't need to be a Qt developer to know this, and it has nothing to do with how you call the delete -- from the outside, from the inside (i.e. deleting this), through a custom event or without.

                          L Offline
                          L Offline
                          LRDPRDX
                          wrote on last edited by
                          #18

                          @kshegunov said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

                          You don't need to be a Qt developer to know this, and it has nothing to do with how you call the delete -- from the outside, from the inside (i.e. deleting this), through a custom event or without.

                          Agreed, but how do I know what the Qt::WA_DeleteOnClose actually does? One can guess it causes the call of delete on the variable but that was just not enough to me to be sure (and obviously I was looking for an answer in the wrong place in the source code). Anyway, now I've got the answer. Thank you.

                          kshegunovK 1 Reply Last reply
                          1
                          • L LRDPRDX

                            @kshegunov said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

                            You don't need to be a Qt developer to know this, and it has nothing to do with how you call the delete -- from the outside, from the inside (i.e. deleting this), through a custom event or without.

                            Agreed, but how do I know what the Qt::WA_DeleteOnClose actually does? One can guess it causes the call of delete on the variable but that was just not enough to me to be sure (and obviously I was looking for an answer in the wrong place in the source code). Anyway, now I've got the answer. Thank you.

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by kshegunov
                            #19

                            @LRDPRDX said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

                            Agreed, but how do I know what the Qt::WA_DeleteOnClose actually does?

                            If you feel the name doesn't give it up, or isn't clear enough, I'd've suggested looking up in the documentation:
                            https://doc.qt.io/qt-5/qt.html#WidgetAttribute-enum

                            It's sort of the point of having documentation - so you don't need to go browsing through the code, and thankfully Qt has the best one I've ever seen.

                            Read and abide by the Qt Code of Conduct

                            L 1 Reply Last reply
                            1
                            • kshegunovK kshegunov

                              @LRDPRDX said in double free or corruption (out) when closing a QDialog with the WA_DeleteOnClose attribute set:

                              Agreed, but how do I know what the Qt::WA_DeleteOnClose actually does?

                              If you feel the name doesn't give it up, or isn't clear enough, I'd've suggested looking up in the documentation:
                              https://doc.qt.io/qt-5/qt.html#WidgetAttribute-enum

                              It's sort of the point of having documentation - so you don't need to go browsing through the code, and thankfully Qt has the best one I've ever seen.

                              L Offline
                              L Offline
                              LRDPRDX
                              wrote on last edited by
                              #20

                              @kshegunov Yes, ofc, I read that. For some reason I interpret the word "delete" (when it's not formatted :) ) as a general way to destroy an object. -_-

                              1 Reply Last reply
                              0
                              • L LRDPRDX

                                @jsulm I don't think so. As you see the output "After exec" is not present. So the error occurs before leaving the scope..

                                H Offline
                                H Offline
                                habeebo
                                wrote on last edited by habeebo
                                #21

                                @LRDPRDX

                                In terminal, "double free or corruption (out)" message is printed after the destructor has been called.

                                Update: Sorry for bothering you. I was having similar issue. I saw your post in stackoverflow and then my problem solved when I allocated my QMainWindow object in heap instead of the stack.

                                Many thanks!

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved