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. Segmentation Fault with Q_Object when used in a static library [Linux]
Forum Updated to NodeBB v4.3 + New Features

Segmentation Fault with Q_Object when used in a static library [Linux]

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.2k 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.
  • J Offline
    J Offline
    johnyang
    wrote on last edited by johnyang
    #1

    I am currently trying to build a GUI project linking to a static library. I'm working in Centos8 stream with g++ (GCC) 8.5.0 20210514 (Red Hat 8.5.0-10), c++17 and Qt5.15.2. My program throws a segmentation fault before main at run time when I try to use any class in the static library inherited to a Qt Object and has Q_Object marco. If I comment out Q_Object and it run perfectly fine. If I move this class out from the static library and place it in the main GUI project, it also runs fine even with Q_Object. An example code is as follows:

    mywindow.h

    #include <QMainWindow>
    
    class MyWindow : public QMainWindow
    {
        Q_OBJECT // comment out this will resolve segmentation fault
      public:
        MyWindow();
        ~MyWindow();
    
      private:
        void InitWindow();
    };
    

    mywindow.cpp

    #include <QHBoxLayout>
    
    MyWindow::MyWindow()
    {
        InitWindow();
    }
    
    MyWindow::~MyWindow() {}
    
    void
    MyWindow::InitWindow()
    {
        QWidget* window = new QWidget;
        QHBoxLayout* mainLayout = new QHBoxLayout;
        window->setLayout(mainLayout);
        setCentralWidget(window);
    }
    

    main.cpp

    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
        app.setApplicationName("LibAudio Demo App");
    
        MyWindow myapp;
        myapp.show();
    
        return app.exec();
    }
    

    So if mywindow.h and mywindow.cpp are in a static library, the program crashed with segmentation fault before main. If I move them to the main project, it runs fine. Commenting out Q_Object also solves the issue. However I do want Q_Object for signal and slots implementation.

    Anyone has any idea why this is happening and how to resolve this? (e.g. I miss any compile option when creating static library?)

    Update 1:
    I removed everything else in my static library and just have mywindows.h/cpp files. Surprisingly it runs fines. So how can Q_Object macro gets affected by other implementation?

    Update 2:
    gdb suggests that the seg fault happens at QString::arg(QString const&, int, QChar) const () from /lib64/libQt5Core.so.5

    Christian EhrlicherC 1 Reply Last reply
    0
    • J johnyang

      I am currently trying to build a GUI project linking to a static library. I'm working in Centos8 stream with g++ (GCC) 8.5.0 20210514 (Red Hat 8.5.0-10), c++17 and Qt5.15.2. My program throws a segmentation fault before main at run time when I try to use any class in the static library inherited to a Qt Object and has Q_Object marco. If I comment out Q_Object and it run perfectly fine. If I move this class out from the static library and place it in the main GUI project, it also runs fine even with Q_Object. An example code is as follows:

      mywindow.h

      #include <QMainWindow>
      
      class MyWindow : public QMainWindow
      {
          Q_OBJECT // comment out this will resolve segmentation fault
        public:
          MyWindow();
          ~MyWindow();
      
        private:
          void InitWindow();
      };
      

      mywindow.cpp

      #include <QHBoxLayout>
      
      MyWindow::MyWindow()
      {
          InitWindow();
      }
      
      MyWindow::~MyWindow() {}
      
      void
      MyWindow::InitWindow()
      {
          QWidget* window = new QWidget;
          QHBoxLayout* mainLayout = new QHBoxLayout;
          window->setLayout(mainLayout);
          setCentralWidget(window);
      }
      

      main.cpp

      int main(int argc, char* argv[])
      {
          QApplication app(argc, argv);
          app.setApplicationName("LibAudio Demo App");
      
          MyWindow myapp;
          myapp.show();
      
          return app.exec();
      }
      

      So if mywindow.h and mywindow.cpp are in a static library, the program crashed with segmentation fault before main. If I move them to the main project, it runs fine. Commenting out Q_Object also solves the issue. However I do want Q_Object for signal and slots implementation.

      Anyone has any idea why this is happening and how to resolve this? (e.g. I miss any compile option when creating static library?)

      Update 1:
      I removed everything else in my static library and just have mywindows.h/cpp files. Surprisingly it runs fines. So how can Q_Object macro gets affected by other implementation?

      Update 2:
      gdb suggests that the seg fault happens at QString::arg(QString const&, int, QChar) const () from /lib64/libQt5Core.so.5

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @johnyang said in Segmentation Fault with Q_Object when used in a static library [Linux]:

      So how can Q_Object macro gets affected by other implementation?

      It does not, it's by accident

      Do you create a global static QObject-based class somewhere? Use a debugger and look at the backtrace of your crash.

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

      J 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @johnyang said in Segmentation Fault with Q_Object when used in a static library [Linux]:

        So how can Q_Object macro gets affected by other implementation?

        It does not, it's by accident

        Do you create a global static QObject-based class somewhere? Use a debugger and look at the backtrace of your crash.

        J Offline
        J Offline
        johnyang
        wrote on last edited by
        #3

        @Christian-Ehrlicher ah yes. I found a static QString that is initialised by QString::arg. After changing that, gdb is suggesting the segmentation fault somewhere else at std::atomic::load. That means it's moving away from QString issue. However I commented out everything related to std::atomic in my code but I'm still at the same spot.

        It sounds like the problem is no longer related to Qt stuff, but thanks for the help.

        Christian EhrlicherC 1 Reply Last reply
        0
        • J johnyang

          @Christian-Ehrlicher ah yes. I found a static QString that is initialised by QString::arg. After changing that, gdb is suggesting the segmentation fault somewhere else at std::atomic::load. That means it's moving away from QString issue. However I commented out everything related to std::atomic in my code but I'm still at the same spot.

          It sounds like the problem is no longer related to Qt stuff, but thanks for the help.

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @johnyang And what is the backtrace of your current thread? I would guess it comes from a global static QObject.

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

          J 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @johnyang And what is the backtrace of your current thread? I would guess it comes from a global static QObject.

            J Offline
            J Offline
            johnyang
            wrote on last edited by johnyang
            #5

            @Christian-Ehrlicher it only says seg fault at QString::arg(QString const&, int, QChar) const () from /lib64/libQt5Core.so.5 nothing else. So this is happening before main. However I'm not sure why I can't initialize a global static QString like:
            const QString somestring = QString("%1 %2").arg(QString("something")).arg(QString("something"));

            This code works in Windows but not in linux?

            JonBJ Christian EhrlicherC 2 Replies Last reply
            0
            • J johnyang

              @Christian-Ehrlicher it only says seg fault at QString::arg(QString const&, int, QChar) const () from /lib64/libQt5Core.so.5 nothing else. So this is happening before main. However I'm not sure why I can't initialize a global static QString like:
              const QString somestring = QString("%1 %2").arg(QString("something")).arg(QString("something"));

              This code works in Windows but not in linux?

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

              @johnyang said in Segmentation Fault with Q_Object when used in a static library [Linux]:

              const QString somestring = QString("%1 %2").arg(QString("something")).arg(QString("something"));

              I believe this particular one should be OK as it only uses QString and that is not a QObject. However you could temporarily try const QString somestring = "" and see if the error goes away. Do you have other QString static variables, or other types, or something which calls something more involved than QString?

              1 Reply Last reply
              0
              • J johnyang

                @Christian-Ehrlicher it only says seg fault at QString::arg(QString const&, int, QChar) const () from /lib64/libQt5Core.so.5 nothing else. So this is happening before main. However I'm not sure why I can't initialize a global static QString like:
                const QString somestring = QString("%1 %2").arg(QString("something")).arg(QString("something"));

                This code works in Windows but not in linux?

                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #7

                @johnyang said in Segmentation Fault with Q_Object when used in a static library [Linux]:

                This code works in Windows but not in linux?

                By accident. I would guess it's not "something" but another QString

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

                J 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @johnyang said in Segmentation Fault with Q_Object when used in a static library [Linux]:

                  This code works in Windows but not in linux?

                  By accident. I would guess it's not "something" but another QString

                  J Offline
                  J Offline
                  johnyang
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher Yes. I think it's to do with that something. It is also a global static QString somewhere. Maybe it's the order of definition.

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • J johnyang

                    @Christian-Ehrlicher Yes. I think it's to do with that something. It is also a global static QString somewhere. Maybe it's the order of definition.

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @johnyang said in Segmentation Fault with Q_Object when used in a static library [Linux]:

                    Maybe it's the order of definition.

                    Not maybe, it is. The order of the initialization is undefined. Don't do it, you won't need it. Also you should take a look at QStringLiteral - there no initialization is needed

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

                    1 Reply Last reply
                    1

                    • Login

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