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

Avoid dangling QTimer object

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtimerstop
12 Posts 6 Posters 2.2k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #3

    Hi,

    Why do you need to delete your timer ?
    From the looks of it, handling it with start and stop seems like it would be enough.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    H 1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      Why do you need to delete your timer ?
      From the looks of it, handling it with start and stop seems like it would be enough.

      H Offline
      H Offline
      hbatalha
      wrote on last edited by
      #4

      @SGaist said in Avoid dangling QTimer object:

      From the looks of it, handling it with start and stop seems like it would be enough.

      Because I know that the MainWindow deletes it as it is its parent but I always avoid doing so when the object can be created dynamically multiple times and that what happens in my app, the timer can be created with new dynamically as long as the app stays open, thus , even if it is small, it will be consuming memory.

      Christian EhrlicherC 1 Reply Last reply
      0
      • jsulmJ jsulm

        @hbatalha said in Avoid dangling QTimer object:

        connect(timer, &QTimer::timeout, this, &MainWindow::taskUpdate);

        You can connect a lambda to the slot and pass the timer as parameter to lambda and then to the actual slot:

        connect(timer, &QTimer::timeout, [this, timer]() { MainWindow::taskUpdate(timer)});
        
        H Offline
        H Offline
        hbatalha
        wrote on last edited by
        #5

        @jsulm said in Avoid dangling QTimer object:

        connect(timer, &QTimer::timeout, this, timer { MainWindow::taskUpdate(timer)});

        Nice suggestion, I had thought about lambda but it was in a different way.
        Thank you, I will probably change my code to this.

        1 Reply Last reply
        0
        • H hbatalha

          @SGaist said in Avoid dangling QTimer object:

          From the looks of it, handling it with start and stop seems like it would be enough.

          Because I know that the MainWindow deletes it as it is its parent but I always avoid doing so when the object can be created dynamically multiple times and that what happens in my app, the timer can be created with new dynamically as long as the app stays open, thus , even if it is small, it will be consuming memory.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #6

          @hbatalha said in Avoid dangling QTimer object:

          the timer can be created with new dynamically as long as the app stays open, thus , even if it is small, it will be consuming memory.

          Do you really think the downsides (make sure to delete it every time you don't need it, continuous memory (de)allocation) is it worth for ~20bytes? A member which is allocated one time would be much easier here.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          H 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @hbatalha said in Avoid dangling QTimer object:

            the timer can be created with new dynamically as long as the app stays open, thus , even if it is small, it will be consuming memory.

            Do you really think the downsides (make sure to delete it every time you don't need it, continuous memory (de)allocation) is it worth for ~20bytes? A member which is allocated one time would be much easier here.

            H Offline
            H Offline
            hbatalha
            wrote on last edited by hbatalha
            #7

            Sorry for the late reply!

            @Christian-Ehrlicher said in Avoid dangling QTimer object:

            Do you really think the downsides (make sure to delete it every time you don't need it, continuous memory (de)allocation) is it worth for ~20bytes?

            Actually that never crossed my mind, thanks. Are there any best practices guide for memory management in Qt?

            A member which is allocated one time would be much easier here.

            Does the same apply for Dialogs? Just an example:

            void MainWindow::onPushButtonClicked()
            {
                SomeDialog* diag = new SomeDialog(this);
            
                diag->setWindowFlags(Qt::WindowStaysOnTopHint);
                diag->setWindowFlags(Qt::Dialog);
            
                diag->exec();
            
                m_name = add->lineEdit->text();
                m_password = lineEdit2->text();
            }
            

            Should I just do it like this:

             SomeDialog*diag.;
            ...
            
             diag.exec();
            

            Since I don't need SomeDialog outside that block of code.

            Edit: I researched a little and found that since .exec() is blocking the object will not run out of scope so I should put them on stack.

            1 Reply Last reply
            0
            • JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by
              #8

              void MainWindow::foo()
              {
              ...

              if(checkTaskCompletion == true)
              {
                   // timer is declared in the mainwindow class
                  if ( nullptr ==  timer ) {
                     timer = new QTimer(this);
                    connect(timer, &QTimer::timeout, this, &MainWindow::taskUpdate);
                  }
                 
                 if ( !timer->isActive() ) { 
                  timer->start(2'000);
                }
              }
              

              }

              you can call foo n times.

              JonBJ 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                void MainWindow::foo()
                {
                ...

                if(checkTaskCompletion == true)
                {
                     // timer is declared in the mainwindow class
                    if ( nullptr ==  timer ) {
                       timer = new QTimer(this);
                      connect(timer, &QTimer::timeout, this, &MainWindow::taskUpdate);
                    }
                   
                   if ( !timer->isActive() ) { 
                    timer->start(2'000);
                  }
                }
                

                }

                you can call foo n times.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #9

                @JoeCFD , @ anyone
                Why not have just one QTimer timer instance as a class member? No newing, no deleteing.

                See also https://forum.qt.io/topic/124404/class-pointer-and-non-pointer-member-function-question !

                JoeCFDJ 1 Reply Last reply
                0
                • JonBJ JonB

                  @JoeCFD , @ anyone
                  Why not have just one QTimer timer instance as a class member? No newing, no deleteing.

                  See also https://forum.qt.io/topic/124404/class-pointer-and-non-pointer-member-function-question !

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #10

                  @JonB I assume s/he has only one. It may be a good practice to write code like that. No more worries about crash or multiple calls to foo.

                  JonBJ 1 Reply Last reply
                  0
                  • JoeCFDJ JoeCFD

                    @JonB I assume s/he has only one. It may be a good practice to write code like that. No more worries about crash or multiple calls to foo.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #11

                    @JoeCFD
                    Yes, and? Sorry I don't understand what point you are making?

                    1 Reply Last reply
                    0
                    • JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by JoeCFD
                      #12

                      Both QTimer timer and QTimer * timer{} are ok. The only difference is QTimer header file is included or not in the class header file.
                      This is not an issue. If C++ is used, new and delete is not a problem. You do it all day long.

                      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