Fixed possible crash on exit

Cleaned up main() function
This commit is contained in:
Christophe Dumez 2010-07-21 10:40:46 +00:00
parent 864bb8285e
commit 1e21ac3d79
3 changed files with 66 additions and 105 deletions

View file

@ -279,10 +279,7 @@ GUI::~GUI() {
delete BTSession; delete BTSession;
// Deleting remaining top level widgets // Deleting remaining top level widgets
qDebug("Deleting remaining top level widgets"); qDebug("Deleting remaining top level widgets");
foreach (QWidget *win, QApplication::topLevelWidgets()) {
if(win && win != this)
delete win;
}
// May freeze for a few seconds after the next line // May freeze for a few seconds after the next line
// because the Bittorrent session proxy will // because the Bittorrent session proxy will
// actually be deleted now and destruction // actually be deleted now and destruction

View file

@ -1113,7 +1113,7 @@ QTorrentHandle Bittorrent::addTorrent(QString path, bool fromScanDir, QString fr
//Getting fast resume data if existing //Getting fast resume data if existing
std::vector<char> buf; std::vector<char> buf;
if(resumed) { if(resumed) {
const QString fastresume_path = torrentBackup.path()+QDir::separator()+hash+QString(".fastresume"); const QString fastresume_path = torrentBackup.absoluteFilePath(hash+QString(".fastresume"));
qDebug("Trying to load fastresume data: %s", qPrintable(fastresume_path)); qDebug("Trying to load fastresume data: %s", qPrintable(fastresume_path));
if (load_file(fastresume_path.toLocal8Bit().constData(), buf) == 0) { if (load_file(fastresume_path.toLocal8Bit().constData(), buf) == 0) {
fastResume = true; fastResume = true;

View file

@ -63,16 +63,6 @@
#include "misc.h" #include "misc.h"
#include "preferences.h" #include "preferences.h"
#ifdef DISABLE_GUI
QtSingleCoreApplication *app;
#else
#ifndef Q_WS_MAC
QtSingleApplication *app;
#else
QMacApplication *app;
#endif
#endif
class UsageDisplay: public QObject { class UsageDisplay: public QObject {
Q_OBJECT Q_OBJECT
@ -158,12 +148,11 @@ void sigabrtHandler(int) {
#endif #endif
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
void useStyle(QApplication *app, QString style){ void useStyle(QString style){
Q_UNUSED(app);
if(!style.isEmpty()) { if(!style.isEmpty()) {
QApplication::setStyle(QStyleFactory::create(style)); QApplication::setStyle(QStyleFactory::create(style));
} }
Preferences::setStyle(app->style()->objectName()); Preferences::setStyle(QApplication::style()->objectName());
} }
#endif #endif
@ -172,25 +161,17 @@ int main(int argc, char *argv[]){
// Create Application // Create Application
QString uid = misc::getUserIDString(); QString uid = misc::getUserIDString();
#ifdef DISABLE_GUI #ifdef DISABLE_GUI
app = new QtSingleCoreApplication("qBittorrent-"+uid, argc, argv); QtSingleCoreApplication app("qBittorrent-"+uid, argc, argv);
#else #else
#ifndef Q_WS_MAC #ifndef Q_WS_MAC
app = new QtSingleApplication("qBittorrent-"+uid, argc, argv); QtSingleApplication app("qBittorrent-"+uid, argc, argv);
#else #else
app = new QMacApplication("qBittorrent-"+uid, argc, argv); QMacApplication app("qBittorrent-"+uid, argc, argv);
#endif #endif
#endif #endif
// XXX: Workaround to avoid the following Qt bug:
// http://bugreports.qt.nokia.com/browse/QTBUG-7105
#ifndef DISABLE_GUI
// Force the creation of an input context to avoid
// a crash in QApplication destructor
app->inputContext();
#endif
// Check if qBittorrent is already running for this user // Check if qBittorrent is already running for this user
if(app->isRunning()) { if(app.isRunning()) {
qDebug("qBittorrent is already running for this user."); qDebug("qBittorrent is already running for this user.");
//Pass program parameters if any //Pass program parameters if any
QString message; QString message;
@ -204,7 +185,7 @@ int main(int argc, char *argv[]){
if(!message.isEmpty()) { if(!message.isEmpty()) {
qDebug("Passing program parameters to running instance..."); qDebug("Passing program parameters to running instance...");
qDebug("Message: %s", qPrintable(message)); qDebug("Message: %s", qPrintable(message));
app->sendMessage(message); app.sendMessage(message);
} }
return 0; return 0;
} }
@ -227,27 +208,25 @@ int main(int argc, char *argv[]){
}else{ }else{
qDebug("%s locale unrecognized, using default (en_GB).", qPrintable(locale)); qDebug("%s locale unrecognized, using default (en_GB).", qPrintable(locale));
} }
app->installTranslator(&translator); app.installTranslator(&translator);
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
if(locale.startsWith("ar")) { if(locale.startsWith("ar")) {
qDebug("Right to Left mode"); qDebug("Right to Left mode");
app->setLayoutDirection(Qt::RightToLeft); app.setLayoutDirection(Qt::RightToLeft);
} else { } else {
app->setLayoutDirection(Qt::LeftToRight); app.setLayoutDirection(Qt::LeftToRight);
} }
#endif #endif
app->setApplicationName(QString::fromUtf8("qBittorrent")); app.setApplicationName(QString::fromUtf8("qBittorrent"));
// Check for executable parameters // Check for executable parameters
if(argc > 1){ if(argc > 1){
if(QString::fromLocal8Bit(argv[1]) == QString::fromUtf8("--version")){ if(QString::fromLocal8Bit(argv[1]) == QString::fromUtf8("--version")){
std::cout << "qBittorrent " << VERSION << '\n'; std::cout << "qBittorrent " << VERSION << '\n';
delete app;
return 0; return 0;
} }
if(QString::fromLocal8Bit(argv[1]) == QString::fromUtf8("--help")){ if(QString::fromLocal8Bit(argv[1]) == QString::fromUtf8("--help")){
UsageDisplay::displayUsage(argv[0]); UsageDisplay::displayUsage(argv[0]);
delete app;
return 0; return 0;
} }
@ -284,8 +263,8 @@ int main(int argc, char *argv[]){
} }
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
useStyle(app, settings.value("Preferences/General/Style", "").toString()); useStyle(settings.value("Preferences/General/Style", "").toString());
app->setStyleSheet("QStatusBar::item { border-width: 0; }"); app.setStyleSheet("QStatusBar::item { border-width: 0; }");
QSplashScreen *splash = 0; QSplashScreen *splash = 0;
if(!no_splash) { if(!no_splash) {
splash = new QSplashScreen(QPixmap(QString::fromUtf8(":/Icons/skin/splash.png"))); splash = new QSplashScreen(QPixmap(QString::fromUtf8(":/Icons/skin/splash.png")));
@ -297,11 +276,10 @@ int main(int argc, char *argv[]){
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
delete splash; delete splash;
#endif #endif
delete app;
return 0; return 0;
} }
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
app->setQuitOnLastWindowClosed(false); app.setQuitOnLastWindowClosed(false);
#endif #endif
#if defined(Q_WS_X11) || defined(Q_WS_MAC) #if defined(Q_WS_X11) || defined(Q_WS_MAC)
signal(SIGABRT, sigabrtHandler); signal(SIGABRT, sigabrtHandler);
@ -310,41 +288,27 @@ int main(int argc, char *argv[]){
signal(SIGSEGV, sigsegvHandler); signal(SIGSEGV, sigsegvHandler);
#endif #endif
// Read torrents given on command line // Read torrents given on command line
QStringList torrentCmdLine = app->arguments(); QStringList torrentCmdLine = app.arguments();
// Remove first argument (program name) // Remove first argument (program name)
torrentCmdLine.removeFirst(); torrentCmdLine.removeFirst();
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
GUI *window = new GUI(0, torrentCmdLine); GUI window(0, torrentCmdLine);
if(!no_splash) { if(!no_splash) {
splash->finish(window); splash->finish(&window);
delete splash; delete splash;
} }
QObject::connect(app, SIGNAL(messageReceived(const QString&)), QObject::connect(&app, SIGNAL(messageReceived(const QString&)),
window, SLOT(processParams(const QString&))); &window, SLOT(processParams(const QString&)));
app->setActivationWindow(window); app.setActivationWindow(&window);
#else #else
// Load Headless class // Load Headless class
HeadlessLoader *loader = new HeadlessLoader(torrentCmdLine); HeadlessLoader loader(torrentCmdLine);
QObject::connect(app, SIGNAL(messageReceived(const QString&)), QObject::connect(&app, SIGNAL(messageReceived(const QString&)),
loader, SLOT(processParams(const QString&))); &loader, SLOT(processParams(const QString&)));
#endif
int ret = app->exec();
#if defined(Q_WS_X11) || defined(Q_WS_MAC)
// Application has exited, stop catching SIGINT and SIGTERM
signal(SIGINT, 0);
signal(SIGTERM, 0);
#endif #endif
#ifndef DISABLE_GUI int ret = app.exec();
delete window;
qDebug("GUI was deleted!");
#else
delete loader;
#endif
qDebug("Deleting app...");
delete app;
qDebug("App was deleted! All good.");
return ret; return ret;
} }