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. qInstallMessageHandler has a segmentation fault?
QtWS25 Last Chance

qInstallMessageHandler has a segmentation fault?

Scheduled Pinned Locked Moved Solved General and Desktop
message handlersegfault
5 Posts 2 Posters 2.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.
  • C Offline
    C Offline
    CAD_coding
    wrote on 15 Jan 2016, 03:26 last edited by CAD_coding
    #1

    In my following code, the line marked with *** creates a segmentation fault when qInstallMessageHandler() is called. If it is commented out, there is no more segmentation fault. I want to handle the logging of the messages to be done in a separate class, hence the class Logger.

    Logger log;
    
    void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    {
        QStringList data;
        data.append(context.category);
        data.append(context.file);
        data.append(context.function);
        data.append(QString(context.line));
        data.append(QString(context.version));
        log.myMessageOutput(type, data, msg); //*** comment out this line to remove the segfault
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        a.setApplicationName("MyApp");
        a.setApplicationVersion("1.0.0");
    
        qInstallMessageHandler(myMessageOutput);
        log.turnOnDebug();
    ...
    }
    
    Logger.h
    
    class Logger : public QObject
    {
        Q_OBJECT
    public:
        void myMessageOutput(QtMsgType type, QStringList context, const QString msg);
        explicit Logger(QObject *parent = 0);
        ~Logger();
        static Logger *get_obj() {return ptr;}
        static void turnOnDebug() {get_obj()->debugMode = true;}
        static void turnOffDebug() {get_obj()->debugMode = false;}
        static void moveLogToSession(QString sessionName);
    
    private:
        QFile *logFile;
        QTextStream *stream;
        QMutex *mutex;
        bool debugMode;
        bool errorMsg;
        static Logger *ptr;
    
        void write(QString str);
    
    signals:
        void SaveSession();
        void FatalSaveSession();
    
    public slots:
    
    };
    
    In Logger.cpp
    
    void Logger::myMessageOutput(QtMsgType type, QStringList context, const QString msg)
    {
       mutex->lock();
       QString str = "";
        switch (type)
        {
            case QtDebugMsg:
            if(!debugMode)
            {
                mutex->unlock();
                return;
            }
            write(msg);
            break;
    
            case QtInfoMsg:
            str.append("\n*** Information ***\n");
            str.append(msg + "\n");
            str.append("Category: " + context.at(0) + "\n");
            str.append("File: " + context.at(1) + "\n");
            str.append("Function: " + context.at(2) + "\n");
            str.append("Line: " + context.at(3) + "\n");
            str.append("Version: " + context.at(4));
            str.append("\n*** Information Complete ***\n");
            write(str);
            break;
    
            case QtWarningMsg:
            if(!(context.at(2).contains("setGeometry")))
            {
                str.append("\n*** Warning ***\n");
                str.append(msg + "\n");
                str.append("Category: " + context.at(0) + "\n");
                str.append("File: " + context.at(1) + "\n");
                str.append("Function: " + context.at(2) + "\n");
                str.append("Line: " + context.at(3) + "\n");
                str.append("Version: " + context.at(4));
                str.append("\n*** Warning Complete ***\n");
                write(str);
                errorMsg = true;
                emit Logger::ptr->SaveSession();
            }
            break;
    
            case QtCriticalMsg:
            str.append("\n*** Critical ***\n");
            str.append(msg + "\n");
            str.append("Category: " + context.at(0) + "\n");
            str.append("File: " + context.at(1) + "\n");
            str.append("Function: " + context.at(2) + "\n");
            str.append("Line: " + context.at(3) + "\n");
            str.append("Version: " + context.at(4));
            str.append("\n*** Critical Complete ***\n");
            write(str);
            errorMsg = true;
            emit Logger::ptr->SaveSession();
            break;
    
            case QtFatalMsg:
            str.append("\n*** Fatal ***\n");
            str.append(msg + "\n");
            str.append("Category: " + context.at(0) + "\n");
            str.append("File: " + context.at(1) + "\n");
            str.append("Function: " + context.at(2) + "\n");
            str.append("Line: " + context.at(3) + "\n");
            str.append("Version: " + context.at(4));
            str.append("\n*** Fatal Complete ***\n");
            write(str);
            errorMsg = false;
            emit Logger::ptr->FatalSaveSession();
            QApplication::exit(-2);
        }
        Logger::mutex->unlock();
    }
    
    1. Why is Qt not allowing me to set my class as the message handler?
    2. What is the workaround if I want to use my Logger class?
    1 Reply Last reply
    0
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 15 Jan 2016, 05:14 last edited by
      #2

      Why do you use so many pointers (logFile, stream, mutex)? Why not just normal instance variables.
      Did you initialize all these pointers?

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

      C 1 Reply Last reply 15 Jan 2016, 05:20
      0
      • J jsulm
        15 Jan 2016, 05:14

        Why do you use so many pointers (logFile, stream, mutex)? Why not just normal instance variables.
        Did you initialize all these pointers?

        C Offline
        C Offline
        CAD_coding
        wrote on 15 Jan 2016, 05:20 last edited by
        #3

        @jsulm When I created these classes I felt that pointers are best way of manipulating class variables.
        Now I understand that I was incorrect.
        But now I have come to a stage where I have to use pointers because I am depending on addresses of class variables.

        That being said, all the variables are initialised appropriately.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 15 Jan 2016, 05:23 last edited by
          #4

          You can get the pointers to your instance variables when needed (& operator).
          Did you try to debug? Maybe your logger is called when you set it and something is wrong in your code.

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

          C 1 Reply Last reply 15 Jan 2016, 14:26
          0
          • J jsulm
            15 Jan 2016, 05:23

            You can get the pointers to your instance variables when needed (& operator).
            Did you try to debug? Maybe your logger is called when you set it and something is wrong in your code.

            C Offline
            C Offline
            CAD_coding
            wrote on 15 Jan 2016, 14:26 last edited by
            #5

            @jsulm thanks I fixed it now. It was a stupid mistake, not setting up the static variable.

            1 Reply Last reply
            0

            3/5

            15 Jan 2016, 05:20

            • Login

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