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
Forum Updated to NodeBB v4.3 + New Features

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 4.3k Views 2 Watching
  • 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 jsulm
    8 Nov 2021, 11:45

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

    Why the error occurs at the first time the ~QDialog() is called ?

    I don't think it happens in the destructor. It happens after the destructor, when Qt::WA_DeleteOnClose causes the second delete.

    L Offline
    L Offline
    LRDPRDX
    wrote on 8 Nov 2021, 12:05 last edited by
    #4

    @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 1 Reply Last reply 10 Feb 2023, 00:26
    0
    • J J.Hilk
      8 Nov 2021, 12:03

      @LRDPRDX

      I don't hink its unexpected,

      you see the first deletion in your console output

      ~MyDialog()

      after that the program tries to call delete the object that is already deleted.

      -> the destructor is of course not called again (no instance to call it upon) but you get the double free exception

      L Offline
      L Offline
      LRDPRDX
      wrote on 8 Nov 2021, 12:16 last edited by
      #5

      @J-Hilk I would agree with you if I had seen the "After exec" line in the output.

      J 1 Reply Last reply 8 Nov 2021, 12:29
      0
      • L LRDPRDX
        8 Nov 2021, 12:16

        @J-Hilk I would agree with you if I had seen the "After exec" line in the output.

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 8 Nov 2021, 12:29 last edited by
        #6

        @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();
        }
        

        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.

        K L 2 Replies Last reply 8 Nov 2021, 12:47
        1
        • J J.Hilk
          8 Nov 2021, 12:29

          @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();
          }
          
          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 8 Nov 2021, 12:47 last edited by kshegunov 11 Aug 2021, 12:48
          #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

          J J L 3 Replies Last reply 8 Nov 2021, 12:48
          2
          • K kshegunov
            8 Nov 2021, 12:47

            @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 Offline
            J Offline
            JonB
            wrote on 8 Nov 2021, 12:48 last edited by JonB 11 Aug 2021, 12:49
            #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 :)

            K 1 Reply Last reply 8 Nov 2021, 12:48
            0
            • J JonB
              8 Nov 2021, 12:48

              @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 :)

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 8 Nov 2021, 12:48 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

              J 1 Reply Last reply 8 Nov 2021, 12:49
              0
              • K kshegunov
                8 Nov 2021, 12:48

                @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.

                J Offline
                J Offline
                JonB
                wrote on 8 Nov 2021, 12:49 last edited by JonB 11 Aug 2021, 12:52
                #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.

                K 1 Reply Last reply 8 Nov 2021, 12:52
                0
                • K kshegunov
                  8 Nov 2021, 12:47

                  @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 Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 8 Nov 2021, 12:51 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 J.Hilk
                    8 Nov 2021, 12:29

                    @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 8 Nov 2021, 12:51 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 1 Reply Last reply 8 Nov 2021, 12:52
                    0
                    • L LRDPRDX
                      8 Nov 2021, 12:51

                      @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 Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 8 Nov 2021, 12:52 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
                      • J JonB
                        8 Nov 2021, 12:49

                        @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.

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 8 Nov 2021, 12:52 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

                        J 1 Reply Last reply 8 Nov 2021, 12:54
                        3
                        • K kshegunov
                          8 Nov 2021, 12:52

                          @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! ;)

                          J Offline
                          J Offline
                          JonB
                          wrote on 8 Nov 2021, 12:54 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 8 Nov 2021, 12:56 last edited by mpergand 11 Aug 2021, 13:04
                            #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
                            • J Offline
                              J Offline
                              JonB
                              wrote on 8 Nov 2021, 12:58 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
                              • K kshegunov
                                8 Nov 2021, 12:47

                                @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 8 Nov 2021, 13:02 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.

                                K 1 Reply Last reply 8 Nov 2021, 13:09
                                1
                                • L LRDPRDX
                                  8 Nov 2021, 13:02

                                  @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.

                                  K Offline
                                  K Offline
                                  kshegunov
                                  Moderators
                                  wrote on 8 Nov 2021, 13:09 last edited by kshegunov 11 Aug 2021, 13:16
                                  #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 8 Nov 2021, 13:17
                                  1
                                  • K kshegunov
                                    8 Nov 2021, 13:09

                                    @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 8 Nov 2021, 13:17 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
                                      8 Nov 2021, 12:05

                                      @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 10 Feb 2023, 00:26 last edited by habeebo 2 Oct 2023, 01:12
                                      #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