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. C++ class to auto delete itself?

C++ class to auto delete itself?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 6 Posters 4.1k 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 18 Jun 2019, 17:47 last edited by
    #1

    I've written a class "clsAlert" this is derived from QDialog. In the class I have a signal which is emitted when the one and only button in the dialog is clicked and in the constructor I connect this signal to a slot in the same class.

        connect(this, SIGNAL(closeClicked()), this, SLOT(cleanup()));
    

    In the cleanup slot:

        void clsAlert::cleanup() {
            delete ui;
            delete this;
        }
    

    In my slot that is attached to the close button:

        void clsAlert::on_btnClose_clicked() {
            close();
            emit closeClicked();
        }
    

    This all seems to work ok, however I have other connections to the same signal so the application is notified when the closeClicked signal is emitted. The problem I'm having is that the signals and slots are performed in the order they were connected, which means that as the first signal connects to the cleanup, no other slots get called.

    Is there any way I can fix this, I'm doing this because I need to know the alert dialogs are cleaned up properly.

    Thank you

    Kind Regards,
    Sy

    R V 2 Replies Last reply 18 Jun 2019, 17:57
    0
    • S SPlatten
      18 Jun 2019, 17:47

      I've written a class "clsAlert" this is derived from QDialog. In the class I have a signal which is emitted when the one and only button in the dialog is clicked and in the constructor I connect this signal to a slot in the same class.

          connect(this, SIGNAL(closeClicked()), this, SLOT(cleanup()));
      

      In the cleanup slot:

          void clsAlert::cleanup() {
              delete ui;
              delete this;
          }
      

      In my slot that is attached to the close button:

          void clsAlert::on_btnClose_clicked() {
              close();
              emit closeClicked();
          }
      

      This all seems to work ok, however I have other connections to the same signal so the application is notified when the closeClicked signal is emitted. The problem I'm having is that the signals and slots are performed in the order they were connected, which means that as the first signal connects to the cleanup, no other slots get called.

      Is there any way I can fix this, I'm doing this because I need to know the alert dialogs are cleaned up properly.

      Thank you

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 18 Jun 2019, 17:57 last edited by
      #2

      @SPlatten said in C++ class to auto delete itself?:

      The problem I'm having is that the signals and slots are performed in the order they were connected, which means that as the first signal connects to the cleanup, no other slots get called.

      for such purposes there is the deleteLater() slot

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      6
      • F Offline
        F Offline
        fcarney
        wrote on 18 Jun 2019, 18:43 last edited by
        #3

        @SPlatten said in C++ class to auto delete itself?:

        delete this;

        This line of code makes me pause. Is it safe to delete an object within itself? I would have never thought of doing that.

        C++ is a perfectly valid school of magic.

        J K 2 Replies Last reply 18 Jun 2019, 19:12
        0
        • F fcarney
          18 Jun 2019, 18:43

          @SPlatten said in C++ class to auto delete itself?:

          delete this;

          This line of code makes me pause. Is it safe to delete an object within itself? I would have never thought of doing that.

          J Offline
          J Offline
          JonB
          wrote on 18 Jun 2019, 19:12 last edited by JonB
          #4

          @fcarney
          It can be OK if you're careful (e.g. http://www.cs.technion.ac.il/users/yechiel/c++-faq/delete-this.html), but I don't think it's ever safe in a slot....

          1 Reply Last reply
          3
          • F fcarney
            18 Jun 2019, 18:43

            @SPlatten said in C++ class to auto delete itself?:

            delete this;

            This line of code makes me pause. Is it safe to delete an object within itself? I would have never thought of doing that.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 18 Jun 2019, 20:32 last edited by kshegunov
            #5

            @fcarney said in C++ class to auto delete itself?:

            This line of code makes me pause. Is it safe to delete an object within itself? I would have never thought of doing that.

            Yes, it's valid and allowed. Whether it's safe depends on the actual code.

            @JonB said in C++ class to auto delete itself?:

            I don't think it's ever safe in a slot....

            You're correct, it never is. Leads to all kinds of beautiful crashes.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • S SPlatten
              18 Jun 2019, 17:47

              I've written a class "clsAlert" this is derived from QDialog. In the class I have a signal which is emitted when the one and only button in the dialog is clicked and in the constructor I connect this signal to a slot in the same class.

                  connect(this, SIGNAL(closeClicked()), this, SLOT(cleanup()));
              

              In the cleanup slot:

                  void clsAlert::cleanup() {
                      delete ui;
                      delete this;
                  }
              

              In my slot that is attached to the close button:

                  void clsAlert::on_btnClose_clicked() {
                      close();
                      emit closeClicked();
                  }
              

              This all seems to work ok, however I have other connections to the same signal so the application is notified when the closeClicked signal is emitted. The problem I'm having is that the signals and slots are performed in the order they were connected, which means that as the first signal connects to the cleanup, no other slots get called.

              Is there any way I can fix this, I'm doing this because I need to know the alert dialogs are cleaned up properly.

              Thank you

              V Offline
              V Offline
              VRonin
              wrote on 18 Jun 2019, 20:47 last edited by
              #6

              delete this;

              No. Just no!

              I'm doing this because I need to know the alert dialogs are cleaned up properly

              Allocate the dialog to the stack and make ui a smart pointer (std::unique_ptr).

              As a side note, normally dialogs are closed by calling either accept() or reject() not by deleting itself.

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              K 1 Reply Last reply 18 Jun 2019, 20:54
              0
              • V VRonin
                18 Jun 2019, 20:47

                delete this;

                No. Just no!

                I'm doing this because I need to know the alert dialogs are cleaned up properly

                Allocate the dialog to the stack and make ui a smart pointer (std::unique_ptr).

                As a side note, normally dialogs are closed by calling either accept() or reject() not by deleting itself.

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 18 Jun 2019, 20:54 last edited by
                #7

                @VRonin said in C++ class to auto delete itself?:

                delete this;

                No. Just no!

                It's fine in general, not for QObjects slots.

                @VRonin said in C++ class to auto delete itself?:

                Allocate the dialog to the stack and make ui a smart pointer (std::unique_ptr).

                Not even gonna touch the useless pointer wrapper here. In fact you should always take the Ui::WhateverClass on the stack. Otherwise you're allocating about 10 pointers or so, so when you call setupUi, you can allocate 10 times by one pointer and only then they are going to allocate the 10 objects. That's just stupid, sorry. What you can even do is not to keep the ui object at all, if you'd designed the widget well and have tied everything in the constructor with signals and slots.

                Read and abide by the Qt Code of Conduct

                V 1 Reply Last reply 18 Jun 2019, 21:25
                0
                • K kshegunov
                  18 Jun 2019, 20:54

                  @VRonin said in C++ class to auto delete itself?:

                  delete this;

                  No. Just no!

                  It's fine in general, not for QObjects slots.

                  @VRonin said in C++ class to auto delete itself?:

                  Allocate the dialog to the stack and make ui a smart pointer (std::unique_ptr).

                  Not even gonna touch the useless pointer wrapper here. In fact you should always take the Ui::WhateverClass on the stack. Otherwise you're allocating about 10 pointers or so, so when you call setupUi, you can allocate 10 times by one pointer and only then they are going to allocate the 10 objects. That's just stupid, sorry. What you can even do is not to keep the ui object at all, if you'd designed the widget well and have tied everything in the constructor with signals and slots.

                  V Offline
                  V Offline
                  VRonin
                  wrote on 18 Jun 2019, 21:25 last edited by
                  #8

                  @kshegunov said in C++ class to auto delete itself?:

                  In fact you should always take the Ui::WhateverClass on the stack

                  What's the difference between a stack allocated Ui::WhateverClass and a std::unique_ptr<Ui::WhateverClass>? I mean in real performance terms it's just a bunch of pointers, right? (I don't use designer so not that familiar with this)

                  so when you call setupUi, you can allocate 10 times by one pointer and only then they are going to allocate the 10 objects

                  I'm not sure I follow

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  K 1 Reply Last reply 18 Jun 2019, 21:31
                  0
                  • V VRonin
                    18 Jun 2019, 21:25

                    @kshegunov said in C++ class to auto delete itself?:

                    In fact you should always take the Ui::WhateverClass on the stack

                    What's the difference between a stack allocated Ui::WhateverClass and a std::unique_ptr<Ui::WhateverClass>? I mean in real performance terms it's just a bunch of pointers, right? (I don't use designer so not that familiar with this)

                    so when you call setupUi, you can allocate 10 times by one pointer and only then they are going to allocate the 10 objects

                    I'm not sure I follow

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 18 Jun 2019, 21:31 last edited by kshegunov
                    #9

                    Let me illustrate with an excerpt:

                    
                    class Ui_LoginDialog
                    {
                    public:
                        QVBoxLayout *verticalLayout;
                        QLabel *titleLabel;
                        QGroupBox *credentialsGroupBox;
                    
                    // ...
                        void setupUi(QDialog *LoginDialog)
                        {
                            if (LoginDialog->objectName().isEmpty())
                                LoginDialog->setObjectName(QStringLiteral("LoginDialog"));
                            LoginDialog->resize(450, 224);
                            verticalLayout = new QVBoxLayout(LoginDialog);
                            titleLabel = new QLabel(LoginDialog);
                            credentialsGroupBox = new QGroupBox(LoginDialog);
                    // ...
                    

                    setupUi already does exactly what you'd do manually. You don't need to allocate the pointers to the objects on the heap too; at least that's one new you can spare.

                    I'm not sure I follow

                    Referring to the public -> private object allocations.
                    Basically you new a bunch of pointers. Then for each one you allocate one pointer, and after that you allocate the actual data (i.e. the private object).

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0

                    1/9

                    18 Jun 2019, 17:47

                    • Login

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