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. The program has unexpectedly finished
Qt 6.11 is out! See what's new in the release blog

The program has unexpectedly finished

Scheduled Pinned Locked Moved General and Desktop
23 Posts 4 Posters 14.2k Views 1 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.
  • F Offline
    F Offline
    Flurite
    wrote on last edited by
    #1

    Hi,

    I am working on a timer widget program, and have received this error:

    The program has unexpectedly finished

    And the application's GUIs do not load, and the application builds fine. This is my code:

    stopwatch_definitions.h:
    @
    #ifndef STOPWATCH_DEFINITIONS_H
    #define STOPWATCH_DEFINITIONS_H

    #include <QObject>
    #include <QTimer>

    class Stopwatch : public QObject
    {
    Q_OBJECT
    public:
    Stopwatch();
    private slots:
    void timer_Start();
    void timer_Pause();
    void timer_Reset();

    void timer_changeTime();
    

    private:
    QTimer* stopwatchTimer;
    };

    #endif // STOPWATCH_DEFINITIONS_H
    @

    stopwatch.cpp:
    @
    #include "stopwatch_definitions.h"
    #include "ui_clock_application.h"

    Ui::Clock_Application* ui;

    Stopwatch::Stopwatch()
    {
    stopwatchTimer = 0;
    }

    void Stopwatch::timer_Reset()
    {
    timer_Pause();
    ui->Stopwatch_Output->setText("00:00:00");
    }

    void Stopwatch::timer_Pause()
    {
    stopwatchTimer->stop();
    }

    void Stopwatch::timer_Start()
    {
    if (!stopwatchTimer)
    {
    stopwatchTimer = new QTimer();
    connect(stopwatchTimer, SIGNAL(timeout()), this, SLOT(timer_changeTime()));
    stopwatchTimer->start(1000);
    }
    }

    void Stopwatch::timer_changeTime()
    {
    /* timer change code */
    }
    @

    widget.cpp:
    @

    #include "widget_definitions.h"
    #include "ui_clock_application.h"
    #include "stopwatch_definitions.h"

    Clock_Application::Clock_Application(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Clock_Application)
    {
    ui->setupUi(this);

    Stopwatch* connectorClass;
    connect(ui->Stopwatch_Start, SIGNAL(clicked()), connectorClass, SLOT(timer_Start()));
    connect(ui->Stopwatch_Pause, SIGNAL(clicked()), connectorClass, SLOT(timer_Pause()));
    connect(ui->Stopwatch_Reset, SIGNAL(clicked()), connectorClass, SLOT(timer_Reset()));
    

    }

    Clock_Application::~Clock_Application()
    {
    delete ui;
    }
    @

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Line 11 in your widget.cpp
      @
      Stopwatch* connectorClass;
      @
      It is a wild pointer which point to random memory.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Flurite
        wrote on last edited by
        #3

        Thanks for pointing that out. This is my new implementation of my class:

        @
        #include "widget_definitions.h"
        #include "ui_clock_application.h"
        #include "stopwatch_definitions.h"

        Clock_Application::Clock_Application(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::Clock_Application)
        {
        ui->setupUi(this);

        Stopwatch* connectorClass = new Stopwatch;
        connect(ui->Stopwatch_Start, SIGNAL(clicked()), connectorClass, SLOT(timer_Start()));
        connect(ui->Stopwatch_Pause, SIGNAL(clicked()), connectorClass, SLOT(timer_Pause()));
        connect(ui->Stopwatch_Reset, SIGNAL(clicked()), connectorClass, SLOT(timer_Reset()));
        delete connectorClass;
        

        }

        Clock_Application::~Clock_Application()
        {
        delete ui;
        }
        @

        When I click any of the buttons, the program becomes unresponsive.. this is unusual, as my slot functions were working completely fine before I decided to make a separate class for my timer

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dbzhang800
          wrote on last edited by
          #4

          [quote author="Flurite" date="1335478572"]

          When I click any of the buttons, the program becomes unresponsive.. this is unusual, as my slot functions were working completely fine before I decided to make a separate class for my timer[/quote]

          If so, somewhere else must have a problem too.

          In addition, lines 11-15 in you widget.cpp do nothing now.

          @
          Stopwatch* connectorClass = new Stopwatch;
          connect(ui->Stopwatch_Start, SIGNAL(clicked()), connectorClass, SLOT(timer_Start()));
          connect(ui->Stopwatch_Pause, SIGNAL(clicked()), connectorClass, SLOT(timer_Pause()));
          connect(ui->Stopwatch_Reset, SIGNAL(clicked()), connectorClass, SLOT(timer_Reset()));
          delete connectorClass;
          @

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Flurite
            wrote on last edited by
            #5

            Oh yeah, that delete was pointless. Anyhow, where do you suspect the problem is?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MuldeR
              wrote on last edited by
              #6

              You are doing strange things there: First you create a new Stopwatch object on the heap (with "new" command) and store the pointer in a local variable. That is okay, so far. Then you connect some signals to the newly created Stopwatch object by using your local pointer. Still okay. But then you delete the object? Huh? In the best case Qt will automatically remove the connection you just created when the object is destroyed. In the worst case it does not and you have a connection to a non-existing object. This is going to cause unexpected behavior and may even lead to application crash...

              [EDIT] Sorry, 1+1=2 was faster ;-) [/EDIT]

              My OpenSource software at: http://muldersoft.com/

              Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

              Go visit the coop: http://youtu.be/Jay...

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MuldeR
                wrote on last edited by
                #7

                [quote author="Flurite" date="1335479359"]Oh yeah, that delete was pointless. Anyhow, where do you suspect the problem is?[/quote]

                You can't just remove the delete. When the function goes out of scope, you'll loose the pointer to your Stopwatch object, because connectorClass is a local variable. The object still exists, but you don't have any pointer to it. This will cause a memory leak, because you won't be able to delete it later...

                I'd recommend to make connectorClass a member variable. And don't forget the delete in the deconstructor!

                My OpenSource software at: http://muldersoft.com/

                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                Go visit the coop: http://youtu.be/Jay...

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  Flurite
                  wrote on last edited by
                  #8

                  Sorry, I think I was posting the wrong code. The program becomes unresponsive when I actually include DO NOT include the delete statement. When I do include the delete statement, the connections disconnect (I think..) and the program just doesn't do anything.

                  Late post: Alright, I will give your most recent post a shot

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MuldeR
                    wrote on last edited by
                    #9

                    Please post full code...

                    My OpenSource software at: http://muldersoft.com/

                    Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                    Go visit the coop: http://youtu.be/Jay...

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dbzhang800
                      wrote on last edited by
                      #10

                      Line 4 in your stopwatch.cpp:
                      @
                      Ui::Clock_Application* ui;
                      @

                      is a wild pointer too.


                      I don't know why your application designed in such pattern. If you delete the class Stopwatch, and implement such function in Clock_Application, it will become very simple and natural.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        MuldeR
                        wrote on last edited by
                        #11

                        I think Ui::Clock_Application* ui is initialized and deleted correctly.

                        @Clock_Application::Clock_Application(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::Clock_Application) // <---
                        {
                        ui->setupUi(this);

                        ....@
                        

                        My OpenSource software at: http://muldersoft.com/

                        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                        Go visit the coop: http://youtu.be/Jay...

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          dbzhang800
                          wrote on last edited by
                          #12

                          [quote author="MuldeR" date="1335480007"]I think Ui::Clock_Application* ui is initialized and deleted correctly.
                          [/quote]

                          Yes, but it has nothing with the ui used in your stopwatch.cpp.

                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            Flurite
                            wrote on last edited by
                            #13

                            [quote author="1+1=2" date="1335479811"]Line 4 in your stopwatch.cpp:
                            @
                            Ui::Clock_Application* ui;
                            @

                            is a wild pointer too.


                            I don't know why your application designed in such pattern. If you delete the class Stopwatch, and implement such function in Clock_Application, it will become very simple and natural.[/quote]

                            I am going to have more slots in my application, and I just do not want to clutter up the Clock_Application class.. I think by dividing it into multiple files will make it simpler. Plus, does ui need to be defined if it's only used to access widgets?

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              MuldeR
                              wrote on last edited by
                              #14

                              The problem is, that the "ui" in your Stopwatch class, that you defined as global variable, is not the same "ui" as the one in your Clock_Application application class, which is a member variable of the Clock_Application class itself. Consequently the value of the "ui" in your Stopwatch class is just an un-initialized pointer. You never assigned a value to it! Any access to that "ui" will cause crash or undefined behavior...

                              Make the "ui" in your Stopwatch class a member variable of the Stopwatch class, rather than a global variable. Also you have to assign it to a value, so you have to pass through the "ui" from Clock_Application in the constructor of Stopwatch and then store it in the member variable...

                              @class Stopwatch : public QObject
                              {
                              Q_OBJECT

                              ...
                              

                              private:
                              QTimer* stopwatchTimer;
                              Ui::Clock_Application *ui; //here we'll store the actual ui-Pointer
                              };@

                              @Stopwatch::Stopwatch(Ui::Clock_Application *ui)
                              {
                              this->ui = ui; //Now store the ui-Pointer we got from the Clock_Application!
                              stopwatchTimer = 0;
                              }@

                              And in Clock_Application we do:

                              @ Clock_Application::Clock_Application(QWidget *parent) :
                              QMainWindow(parent),
                              ui(new Ui::Clock_Application)
                              {
                              ui->setupUi(this);

                                  myStopwatch = new Stopwatch(ui); // <--- pass ui-Pointer over
                              
                                  ....@
                              

                              My OpenSource software at: http://muldersoft.com/

                              Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                              Go visit the coop: http://youtu.be/Jay...

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                dbzhang800
                                wrote on last edited by
                                #15

                                [quote author="Flurite" date="1335480406"]

                                I am going to have more slots in my application, and I just do not want to clutter up the Clock_Application class.. I think by dividing it into multiple files will make it simpler.
                                [/quote]

                                That's OK.

                                But Stopwatch need to accept an private memeber of another Class now, so this is a bad design.

                                [quote author="Flurite" date="1335480406"]
                                Plus, does ui need to be defined if it's only used to access widgets?
                                [/quote]
                                No, Seems what you need is some basic knowledge of C++ ;-).

                                1 Reply Last reply
                                0
                                • F Offline
                                  F Offline
                                  Flurite
                                  wrote on last edited by
                                  #16

                                  [quote author="1+1=2" date="1335480901"]
                                  [quote author="Flurite" date="1335480406"]

                                  I am going to have more slots in my application, and I just do not want to clutter up the Clock_Application class.. I think by dividing it into multiple files will make it simpler.
                                  [/quote]

                                  That's OK.

                                  But Stopwatch need to accept an private memeber of another Class now, so this is a bad design.

                                  [quote author="Flurite" date="1335480406"]
                                  Plus, does ui need to be defined if it's only used to access widgets?
                                  [/quote]
                                  No, Seems what you need is some basic knowledge of C++ ;-).[/quote]

                                  I think you are trying to say that I am dereferencing a garbage pointer? I saw that problem from the start, but I didn't know how to fix it. When I tried to replicate how Qt implemented the ui pointer, I never saw a definition for it, so that's why I thought I didn't need one for mine. Anyhow, I think I see the definition in the widget.cpp file now.

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    MuldeR
                                    wrote on last edited by
                                    #17

                                    [quote author="Flurite" date="1335481107"]I think you are trying to say that I am dereferencing a garbage pointer? I saw that problem from the start, but I didn't know how to fix it.[/quote]

                                    You can do it, for example, like the code in my previous post:
                                    http://qt-project.org/forums/viewthread/16697/#83748

                                    My OpenSource software at: http://muldersoft.com/

                                    Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                                    Go visit the coop: http://youtu.be/Jay...

                                    1 Reply Last reply
                                    0
                                    • F Offline
                                      F Offline
                                      Flurite
                                      wrote on last edited by
                                      #18

                                      [quote author="MuldeR" date="1335481183"][quote author="Flurite" date="1335481107"]I think you are trying to say that I am dereferencing a garbage pointer? I saw that problem from the start, but I didn't know how to fix it.[/quote]

                                      You can do it, for example, like the code in my previous post:
                                      http://qt-project.org/forums/viewthread/16697/#83748[/quote]

                                      Darn good idea. It's working now.. should have remembered using the this pointer to clarify some scope problems. Thanks!

                                      1 Reply Last reply
                                      0
                                      • M Offline
                                        M Offline
                                        MuldeR
                                        wrote on last edited by
                                        #19

                                        Actually you could just have used other names:

                                        @class Stopwatch : public QObject
                                        {
                                        Q_OBJECT

                                        public:
                                        Stopwatch(Ui::Clock_Application *bar);

                                        ...
                                        

                                        private:
                                        QTimer* stopwatchTimer;
                                        Ui::Clock_Application *m_foo; //here we'll store the actual ui-Pointer
                                        };

                                        .......

                                        Stopwatch::Stopwatch(Ui::Clock_Application *bar)
                                        {
                                        m_foo = bar; //Now store the ui-Pointer we got from the Clock_Application!
                                        stopwatchTimer = 0;
                                        }

                                        .......

                                        void Stopwatch::timer_Reset()
                                        {
                                        timer_Pause();
                                        m_foo->Stopwatch_Output->setText("00:00:00");
                                        }@

                                        @ Clock_Application::Clock_Application(QWidget *parent) :
                                        QMainWindow(parent),
                                        ui(new Ui::Clock_Application)
                                        {
                                        ui->setupUi(this);

                                            myStopwatch = new Stopwatch(ui); // <--- pass ui-Pointer over
                                        
                                            ....@
                                        

                                        The name of your pointer variables doesn't matter. It matters that you store the right pointer!

                                        My OpenSource software at: http://muldersoft.com/

                                        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                                        Go visit the coop: http://youtu.be/Jay...

                                        1 Reply Last reply
                                        0
                                        • F Offline
                                          F Offline
                                          Flurite
                                          wrote on last edited by
                                          #20

                                          Yes, yes, I understand that. In fact, I thought of that right after I replied to your post. I don't know why, but I just have this need for a unique consistency in all my codes.

                                          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