Results 1 to 6 of 6

Thread: public timer sometimes crashes at runtime

  1. #1
    Join Date
    Feb 2016
    Posts
    8
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default public timer sometimes crashes at runtime

    I'm building a timer with an interval that can adjusted by a slider. Here's a shot of my ui:

    yooeye.jpg

    The upper lcdNumber counts up by one every second. I want to use the slider to speed it up and slow it down. Here is my header file

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17. QTimer *timer; // here the timer is made public
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21.  
    22. private slots:
    23. void inkup();
    24. void on_horizontalSlider_valueChanged(int value);
    25. };
    26.  
    27. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    And here is my application

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "qapplication.h"
    4. #include <QtWidgets>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. QTimer *timer = new QTimer(this);
    13. connect(timer, SIGNAL(timeout()), this, SLOT(inkup()));
    14. timer->start(1000);
    15. }
    16.  
    17. void MainWindow::inkup() {
    18. int eks = ui->lcdNumber->value();
    19. eks += 1;
    20. ui->lcdNumber->display(eks);
    21. }
    22.  
    23. MainWindow::~MainWindow()
    24. {
    25. delete ui;
    26. }
    27.  
    28. void MainWindow::on_horizontalSlider_valueChanged(int speed)
    29. {
    30. ui->lcdNumber_2->display(timer->remainingTime());
    31.  
    32. ui->lcdNumber_2->display(speed);
    33.  
    34. timer->setInterval(speed); // this causes a crash at run time
    35. }
    To copy to clipboard, switch view to plain text mode 

    In the horizontal slider event handler, I can display in the second lcdNumber the timer property remainingTime(). No problem. I can display the changed slider value so I know it's okay. But when I try and set the timer interval I get a run time crash. In fact most of the properties that pop up for the timer when I type timer-> also cause a crash. What am I doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: public timer sometimes crashes at runtime

    Quote Originally Posted by twoten View Post
    But when I try and set the timer interval I get a run time crash. In fact most of the properties that pop up for the timer when I type timer-> also cause a crash. What am I doing wrong?
    Because you are accessing an uninitialized pointer.
    Your member variable "timer" never gets initialized, so it points to some random memory address.

    Your constructor initializes a local variable with the same name, so you probably didn't want this to be a local variable.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2016
    Posts
    8
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: public timer sometimes crashes at runtime

    Can you give me an example of the correct way to do this?

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: public timer sometimes crashes at runtime

    Initialize the timer before the initialization ui.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: public timer sometimes crashes at runtime

    Quote Originally Posted by twoten View Post
    Can you give me an example of the correct way to do this?
    Instead of
    Qt Code:
    1. QTimer *timer = new QTimer(this);
    To copy to clipboard, switch view to plain text mode 
    you write
    Qt Code:
    1. timer = new QTimer(this);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  6. #6
    Join Date
    Feb 2016
    Posts
    8
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: public timer sometimes crashes at runtime

    Fantastic! Not only did it work but I also understand why! Thank you very much!

Similar Threads

  1. Replies: 6
    Last Post: 4th September 2014, 10:06
  2. Public routines
    By eltecprogetti in forum Qt Programming
    Replies: 10
    Last Post: 9th April 2012, 14:26
  3. Public variables
    By eltecprogetti in forum Qt Programming
    Replies: 8
    Last Post: 6th April 2012, 13:58
  4. timer problem(timer does not work)
    By masuk in forum Newbie
    Replies: 6
    Last Post: 14th February 2011, 06:00
  5. Replies: 1
    Last Post: 25th September 2010, 09:20

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.