Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix: Close logfile only after the disabling logging to file
Browse files Browse the repository at this point in the history
This fixes a segfault when closing qTox, because the logfile was closed
before the last message was written.
  • Loading branch information
sudden6 committed Mar 26, 2016
1 parent 631c37a commit de48789
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ int main(int argc, char *argv[])
QDir(logFileDir).mkpath(".");

QString logfile = logFileDir + "qtox.log";
FILE * tmpLogFilePtr = fopen(logfile.toLocal8Bit().constData(), "a");
FILE * mainLogFilePtr = fopen(logfile.toLocal8Bit().constData(), "a");

// Trim log file if over 1MB
if (QFileInfo(logfile).size() > 1000000)
Expand All @@ -176,17 +176,17 @@ int main(int argc, char *argv[])
qCritical() << "Unable to move logs";

// close old logfile
if(tmpLogFilePtr)
fclose(tmpLogFilePtr);
if(mainLogFilePtr)
fclose(mainLogFilePtr);

// open a new logfile
tmpLogFilePtr = fopen(logfile.toLocal8Bit().constData(), "a");
mainLogFilePtr = fopen(logfile.toLocal8Bit().constData(), "a");
}

if(!tmpLogFilePtr)
if(!mainLogFilePtr)
qCritical() << "Couldn't open logfile" << logfile;

logFileFile.store(tmpLogFilePtr); // atomically set the logFile
logFileFile.store(mainLogFilePtr); // atomically set the logFile
#endif

// Windows platform plugins DLL hell fix
Expand Down Expand Up @@ -288,13 +288,14 @@ int main(int argc, char *argv[])
// Run
int errorcode = a.exec();

#ifdef LOG_TO_FILE
fclose(logFileFile);
#endif

Nexus::destroyInstance();
CameraSource::destroyInstance();
Settings::destroyInstance();
qDebug() << "Clean exit with status" << errorcode;

#ifdef LOG_TO_FILE
logFileFile.store(nullptr); // atomically disable logging to file
fclose(mainLogFilePtr);
#endif
return errorcode;
}

0 comments on commit de48789

Please sign in to comment.