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. Using Singleton Class contains QComboxBox , crashed
Forum Updated to NodeBB v4.3 + New Features

Using Singleton Class contains QComboxBox , crashed

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 1.1k 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.
  • T Offline
    T Offline
    trreeee
    wrote on last edited by trreeee
    #1

    show the widget and select the item, program will crash on exit

    class SettingWidget : public QWidget
    {
        Q_OBJECT
    
    public:
        ~SettingWidget(){}
        static SettingWidget& instance(){
            static SettingWidget mInstance;
            return mInstance;
        }
    private:
        explicit SettingWidget(QWidget *parent = 0) :
            QWidget(parent)
        {
            cbox = new QComboBox(this);
            cbox->addItem("1");
        }
    
        QComboBox * cbox;
    
    };
    main.cpp
     QApplication a(argc, argv);
      SettingWidget::instance().show();
    return a.exec();
    

    console:

    QBasicTimer::start: QBasicTimer can only be used with threads started with QThread
    15:18:50: C:/Users/black/Documents/build-untitled29-Desktop_Qt_5_15_2_MinGW_64_bit-Debug/untitled29.exe crashed.

    sorry ,i do not how to post stack. I do not use thread
    9b77edc0-9904-4de0-8b95-68448f145216-image.png
    14867e8b-d4df-455f-891a-fd7fd87b5581-image.png

    jsulmJ JonBJ 4 Replies Last reply
    0
    • T trreeee

      show the widget and select the item, program will crash on exit

      class SettingWidget : public QWidget
      {
          Q_OBJECT
      
      public:
          ~SettingWidget(){}
          static SettingWidget& instance(){
              static SettingWidget mInstance;
              return mInstance;
          }
      private:
          explicit SettingWidget(QWidget *parent = 0) :
              QWidget(parent)
          {
              cbox = new QComboBox(this);
              cbox->addItem("1");
          }
      
          QComboBox * cbox;
      
      };
      main.cpp
       QApplication a(argc, argv);
        SettingWidget::instance().show();
      return a.exec();
      

      console:

      QBasicTimer::start: QBasicTimer can only be used with threads started with QThread
      15:18:50: C:/Users/black/Documents/build-untitled29-Desktop_Qt_5_15_2_MinGW_64_bit-Debug/untitled29.exe crashed.

      sorry ,i do not how to post stack. I do not use thread
      9b77edc0-9904-4de0-8b95-68448f145216-image.png
      14867e8b-d4df-455f-891a-fd7fd87b5581-image.png

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @trreeee How should anybody know why it crashes if you do not post relevant code? How does your instance() look like? Did you run through debugger to see where exactly it crashes and how the stack trace looks like?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • T trreeee

        show the widget and select the item, program will crash on exit

        class SettingWidget : public QWidget
        {
            Q_OBJECT
        
        public:
            ~SettingWidget(){}
            static SettingWidget& instance(){
                static SettingWidget mInstance;
                return mInstance;
            }
        private:
            explicit SettingWidget(QWidget *parent = 0) :
                QWidget(parent)
            {
                cbox = new QComboBox(this);
                cbox->addItem("1");
            }
        
            QComboBox * cbox;
        
        };
        main.cpp
         QApplication a(argc, argv);
          SettingWidget::instance().show();
        return a.exec();
        

        console:

        QBasicTimer::start: QBasicTimer can only be used with threads started with QThread
        15:18:50: C:/Users/black/Documents/build-untitled29-Desktop_Qt_5_15_2_MinGW_64_bit-Debug/untitled29.exe crashed.

        sorry ,i do not how to post stack. I do not use thread
        9b77edc0-9904-4de0-8b95-68448f145216-image.png
        14867e8b-d4df-455f-891a-fd7fd87b5581-image.png

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

        @trreeee said in Using singleton contain QComboxBox , crashed:

        static SettingWidget& instance();

        You may know C++ rules for this better than I, but although this is a static method returning a reference to a SettingWidget object you have not actually created any such static object for it to return. The code does not show the definition of instance().

        jsulmJ 1 Reply Last reply
        1
        • JonBJ JonB

          @trreeee said in Using singleton contain QComboxBox , crashed:

          static SettingWidget& instance();

          You may know C++ rules for this better than I, but although this is a static method returning a reference to a SettingWidget object you have not actually created any such static object for it to return. The code does not show the definition of instance().

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JonB said in Using singleton contain QComboxBox , crashed:

          you have not actually created any such static object for it to return

          It could be a static variable inside instance() - that's why I asked for its implementation :-)

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          JonBJ 1 Reply Last reply
          1
          • jsulmJ jsulm

            @JonB said in Using singleton contain QComboxBox , crashed:

            you have not actually created any such static object for it to return

            It could be a static variable inside instance() - that's why I asked for its implementation :-)

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

            @jsulm Yes I changed my reply to make this clear.

            1 Reply Last reply
            1
            • T trreeee

              show the widget and select the item, program will crash on exit

              class SettingWidget : public QWidget
              {
                  Q_OBJECT
              
              public:
                  ~SettingWidget(){}
                  static SettingWidget& instance(){
                      static SettingWidget mInstance;
                      return mInstance;
                  }
              private:
                  explicit SettingWidget(QWidget *parent = 0) :
                      QWidget(parent)
                  {
                      cbox = new QComboBox(this);
                      cbox->addItem("1");
                  }
              
                  QComboBox * cbox;
              
              };
              main.cpp
               QApplication a(argc, argv);
                SettingWidget::instance().show();
              return a.exec();
              

              console:

              QBasicTimer::start: QBasicTimer can only be used with threads started with QThread
              15:18:50: C:/Users/black/Documents/build-untitled29-Desktop_Qt_5_15_2_MinGW_64_bit-Debug/untitled29.exe crashed.

              sorry ,i do not how to post stack. I do not use thread
              9b77edc0-9904-4de0-8b95-68448f145216-image.png
              14867e8b-d4df-455f-891a-fd7fd87b5581-image.png

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

              @trreeee
              You keep altering your code, so I don't know how accurate to what you really have it is or when the updates are going to end....

              One thing: it may not be relevant, but I don't think you should go cbox->show(); during the constructor for SettingWidget, that is not a place to put show()s.

              T 1 Reply Last reply
              1
              • JonBJ JonB

                @trreeee
                You keep altering your code, so I don't know how accurate to what you really have it is or when the updates are going to end....

                One thing: it may not be relevant, but I don't think you should go cbox->show(); during the constructor for SettingWidget, that is not a place to put show()s.

                T Offline
                T Offline
                trreeee
                wrote on last edited by
                #7

                @JonB said in Using singleton contain QComboxBox , crashed:

                : it may not be rel

                sorry, This is the first time to ask a question here

                jsulmJ 1 Reply Last reply
                0
                • T trreeee

                  show the widget and select the item, program will crash on exit

                  class SettingWidget : public QWidget
                  {
                      Q_OBJECT
                  
                  public:
                      ~SettingWidget(){}
                      static SettingWidget& instance(){
                          static SettingWidget mInstance;
                          return mInstance;
                      }
                  private:
                      explicit SettingWidget(QWidget *parent = 0) :
                          QWidget(parent)
                      {
                          cbox = new QComboBox(this);
                          cbox->addItem("1");
                      }
                  
                      QComboBox * cbox;
                  
                  };
                  main.cpp
                   QApplication a(argc, argv);
                    SettingWidget::instance().show();
                  return a.exec();
                  

                  console:

                  QBasicTimer::start: QBasicTimer can only be used with threads started with QThread
                  15:18:50: C:/Users/black/Documents/build-untitled29-Desktop_Qt_5_15_2_MinGW_64_bit-Debug/untitled29.exe crashed.

                  sorry ,i do not how to post stack. I do not use thread
                  9b77edc0-9904-4de0-8b95-68448f145216-image.png
                  14867e8b-d4df-455f-891a-fd7fd87b5581-image.png

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

                  @trreeee
                  Did you at least test your code with SettingWidget sw; instead of SettingWidget::instance() to verify that it really is the static which is the issue?

                  console:
                  
                  QBasicTimer::start: QBasicTimer can only be used with threads started with QThread
                  15:18:50: C:/Users/black/Documents/build-untitled29-Desktop_Qt_5_15_2_MinGW_64_bit-Debug/untitled29.exe crashed.
                  

                  Now that you post this it looks like you are using threds and you issue lies there, nothing to do with static.... Is the code you are posting your whole program, or part of something else?

                  1 Reply Last reply
                  1
                  • T trreeee

                    @JonB said in Using singleton contain QComboxBox , crashed:

                    : it may not be rel

                    sorry, This is the first time to ask a question here

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @trreeee Please run through debugger and post stack trace here

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • Paul ColbyP Offline
                      Paul ColbyP Offline
                      Paul Colby
                      wrote on last edited by
                      #10

                      I tried out your sample code, and for me, it runs fine :) However, it does crash on exit, because SettingWidget::cbox is destroyed twice. Once, because its a child of the single SettingWidget instance (this is done by Qt), and then because its a static variable (this done by C++ specifications).

                      The fix is to unset the parent-child relationship in the SettingWidget destructor, like:

                         ~SettingWidget() {
                              if ((cbox) && (cbox->parent() == this)) {
                                  cbox->setParent(nullptr);
                              }
                          }
                      

                      The if conditions aren't really necessary here, but good practice anyway :)

                      Cheers.

                      T 1 Reply Last reply
                      3
                      • Paul ColbyP Paul Colby

                        I tried out your sample code, and for me, it runs fine :) However, it does crash on exit, because SettingWidget::cbox is destroyed twice. Once, because its a child of the single SettingWidget instance (this is done by Qt), and then because its a static variable (this done by C++ specifications).

                        The fix is to unset the parent-child relationship in the SettingWidget destructor, like:

                           ~SettingWidget() {
                                if ((cbox) && (cbox->parent() == this)) {
                                    cbox->setParent(nullptr);
                                }
                            }
                        

                        The if conditions aren't really necessary here, but good practice anyway :)

                        Cheers.

                        T Offline
                        T Offline
                        trreeee
                        wrote on last edited by
                        #11

                        @Paul-Colby said in Using singleton contain QComboxBox , crashed:

                        if ((cbox) && (cbox->parent() == this)) {
                        cbox->setParent(nullptr);
                        }

                        thank you, I crash on exit also. I tried out "if code", the program still crash.

                        if I do not select the QComboBox 's item, the program can exit normally

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          trreeee
                          wrote on last edited by trreeee
                          #12

                          I solved , I used exit(0) to exit program. the program can exit normally, and ~SettingWidget(){} can be called normally,
                          return a.exec() crashed when ~SettingWidget(){} be called, qApp->exit() same as return a.exe()

                          JonBJ 1 Reply Last reply
                          0
                          • T trreeee

                            I solved , I used exit(0) to exit program. the program can exit normally, and ~SettingWidget(){} can be called normally,
                            return a.exec() crashed when ~SettingWidget(){} be called, qApp->exit() same as return a.exe()

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

                            @trreeee said in Using Singleton Class contains QComboxBox , crashed:

                            exit(0) to exit program. the program can exit normally, and ~SettingWidget(){} can be called normally,

                            Are you sure ~SettingWidget() is called in this case? Sounds more to me like you are just avoiding the (incorrect) destruction behaviour this way? But up to you.

                            My 2 cents? If you must have this singleton widget, I would have newed a static SettingWidget *mInstance; rather than having a static SettingWidget. Your way the (static) destruction comes after the parent has been destroyed, not a good idea.

                            T 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @trreeee said in Using Singleton Class contains QComboxBox , crashed:

                              exit(0) to exit program. the program can exit normally, and ~SettingWidget(){} can be called normally,

                              Are you sure ~SettingWidget() is called in this case? Sounds more to me like you are just avoiding the (incorrect) destruction behaviour this way? But up to you.

                              My 2 cents? If you must have this singleton widget, I would have newed a static SettingWidget *mInstance; rather than having a static SettingWidget. Your way the (static) destruction comes after the parent has been destroyed, not a good idea.

                              T Offline
                              T Offline
                              trreeee
                              wrote on last edited by trreeee
                              #14

                              @JonB said in Using Singleton Class contains QComboxBox , crashed:

                              @trreeee said in Using Singleton Class contains QComboxBox , crashed:

                              exit(0) to exit program. the program can exit normally, and ~SettingWidget(){} can be called normally,

                              Are you sure ~SettingWidget() is called in this case? Sounds more to me like you are just avoiding the (incorrect) destruction behaviour this way? But up to you.

                              My 2 cents? If you must have this singleton widget, I would have newed a static SettingWidget *mInstance; rather than having a static SettingWidget. Your way the (static) destruction comes after the parent has been destroyed, not a good idea.

                              Thank you, I tested it, it did work
                              I also found this way

                              #include <QCoreApplication>
                              static Widget* instance(){
                                  static Widget* impl = new Widget;
                              	connect(qApp, &QCoreApplication::aboutToQuit, impl, &Widget::deleteLater);
                              	return impl;
                              }
                              
                              JonBJ 1 Reply Last reply
                              1
                              • T trreeee

                                @JonB said in Using Singleton Class contains QComboxBox , crashed:

                                @trreeee said in Using Singleton Class contains QComboxBox , crashed:

                                exit(0) to exit program. the program can exit normally, and ~SettingWidget(){} can be called normally,

                                Are you sure ~SettingWidget() is called in this case? Sounds more to me like you are just avoiding the (incorrect) destruction behaviour this way? But up to you.

                                My 2 cents? If you must have this singleton widget, I would have newed a static SettingWidget *mInstance; rather than having a static SettingWidget. Your way the (static) destruction comes after the parent has been destroyed, not a good idea.

                                Thank you, I tested it, it did work
                                I also found this way

                                #include <QCoreApplication>
                                static Widget* instance(){
                                    static Widget* impl = new Widget;
                                	connect(qApp, &QCoreApplication::aboutToQuit, impl, &Widget::deleteLater);
                                	return impl;
                                }
                                
                                JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by
                                #15

                                @trreeee Good, I think that is much better!

                                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