qInstallMessageHandler has a segmentation fault?
-
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(); }
- Why is Qt not allowing me to set my class as the message handler?
- What is the workaround if I want to use my Logger class?
-
Why do you use so many pointers (logFile, stream, mutex)? Why not just normal instance variables.
Did you initialize all these pointers? -
@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.
-
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.