Browse Source

searchengine: fix crash when closing tab with running search

If a tab is closed, the search process gets terminated. This can take
a while and by the time searchFinished() is executed, activeSearchTab
is null, leading to a crash. Fix this by waiting for the process to
terminate and make sure activeSearchTab is not null when used.
adaptive-webui-19844
Gabriele 9 years ago
parent
commit
ee8a492954
  1. 33
      src/searchengine/searchengine.cpp

33
src/searchengine/searchengine.cpp

@ -208,13 +208,16 @@ void SearchEngine::on_search_button_clicked()
search_stopped = true; search_stopped = true;
if (searchTimeout->isActive()) if (searchTimeout->isActive())
searchTimeout->stop(); searchTimeout->stop();
searchProcess->waitForFinished(1000);
if (search_button->text() != tr("Search")) { if (search_button->text() != tr("Search")) {
search_button->setText(tr("Search")); search_button->setText(tr("Search"));
return; return;
} }
allTabsSetActiveState(false); allTabsSetActiveState(false);
} }
searchProcess->waitForFinished();
// Reload environment variables (proxy) // Reload environment variables (proxy)
searchProcess->setEnvironment(QProcess::systemEnvironment()); searchProcess->setEnvironment(QProcess::systemEnvironment());
@ -250,7 +253,7 @@ void SearchEngine::on_search_button_clicked()
nb_search_results = 0; nb_search_results = 0;
search_result_line_truncated.clear(); search_result_line_truncated.clear();
// Changing the text of the current label // Changing the text of the current label
currentSearchTab->getCurrentLabel()->setText(tr("Results") + " <i>(0)</i>:"); activeSearchTab->getCurrentLabel()->setText(tr("Results") + " <i>(0)</i>:");
// Launch search // Launch search
searchProcess->start(Utils::Misc::pythonExecutable(), params, QIODevice::ReadOnly); searchProcess->start(Utils::Misc::pythonExecutable(), params, QIODevice::ReadOnly);
searchTimeout->start(180000); // 3min searchTimeout->start(180000); // 3min
@ -302,7 +305,7 @@ void SearchEngine::searchStarted()
{ {
// Update SearchEngine widgets // Update SearchEngine widgets
activeSearchTab->status = tr("Searching..."); activeSearchTab->status = tr("Searching...");
search_status->setText(activeSearchTab->status); search_status->setText(currentSearchTab->status);
search_status->repaint(); search_status->repaint();
search_button->setText(tr("Stop")); search_button->setText(tr("Stop"));
} }
@ -322,7 +325,6 @@ void SearchEngine::readSearchOutput()
search_result_line_truncated = lines_list.takeLast().trimmed(); search_result_line_truncated = lines_list.takeLast().trimmed();
foreach (const QByteArray &line, lines_list) foreach (const QByteArray &line, lines_list)
appendSearchResult(QString::fromUtf8(line)); appendSearchResult(QString::fromUtf8(line));
if (activeSearchTab)
activeSearchTab->getCurrentLabel()->setText(tr("Results") + QString::fromUtf8(" <i>(") + QString::number(nb_search_results) + QString::fromUtf8(")</i>:")); activeSearchTab->getCurrentLabel()->setText(tr("Results") + QString::fromUtf8(" <i>(") + QString::number(nb_search_results) + QString::fromUtf8(")</i>:"));
} }
@ -443,6 +445,11 @@ void SearchEngine::searchFinished(int exitcode, QProcess::ExitStatus)
bool useNotificationBalloons = Preferences::instance()->useProgramNotification(); bool useNotificationBalloons = Preferences::instance()->useProgramNotification();
if (useNotificationBalloons && mp_mainWindow->getCurrentTabWidget() != this) if (useNotificationBalloons && mp_mainWindow->getCurrentTabWidget() != this)
mp_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Search has finished")); mp_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Search has finished"));
if (activeSearchTab.isNull())
// The active tab was closed
return;
if (exitcode) { if (exitcode) {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
activeSearchTab->status = tr("Search aborted"); activeSearchTab->status = tr("Search aborted");
@ -461,13 +468,9 @@ void SearchEngine::searchFinished(int exitcode, QProcess::ExitStatus)
activeSearchTab->status = tr("Search has finished"); activeSearchTab->status = tr("Search has finished");
} }
} }
search_status->setText(currentSearchTab->status);
if (activeSearchTab)
if (currentSearchTab == activeSearchTab) search_status->setText(activeSearchTab->status);
activeSearchTab->getCurrentLabel()->setText(tr("Results", "i.e: Search results") + QString::fromUtf8(" <i>(") + QString::number(nb_search_results) + QString::fromUtf8(")</i>:"));
activeSearchTab->isActive = false; activeSearchTab->isActive = false;
activeSearchTab = 0; activeSearchTab = 0;
search_button->setText(tr("Search")); search_button->setText(tr("Search"));
} }
@ -476,9 +479,11 @@ void SearchEngine::searchFinished(int exitcode, QProcess::ExitStatus)
// file url | file name | file size | nb seeds | nb leechers | Search engine url // file url | file name | file size | nb seeds | nb leechers | Search engine url
void SearchEngine::appendSearchResult(const QString &line) void SearchEngine::appendSearchResult(const QString &line)
{ {
if (!activeSearchTab) { if (activeSearchTab.isNull()) {
if (searchProcess->state() != QProcess::NotRunning) if (searchProcess->state() != QProcess::NotRunning) {
searchProcess->terminate(); searchProcess->terminate();
searchProcess->waitForFinished(1000);
}
if (searchTimeout->isActive()) if (searchTimeout->isActive())
searchTimeout->stop(); searchTimeout->stop();
search_stopped = true; search_stopped = true;
@ -524,20 +529,22 @@ void SearchEngine::appendSearchResult(const QString &line)
void SearchEngine::closeTab(int index) void SearchEngine::closeTab(int index)
{ {
// Search is run for active tab so if user decided to close it, then stop search // Search is run for active tab so if user decided to close it, then stop search
if (activeSearchTab && index == tabWidget->indexOf(activeSearchTab)) { if (!activeSearchTab.isNull() && index == tabWidget->indexOf(activeSearchTab)) {
qDebug("Closed active search Tab"); qDebug("Closed active search Tab");
if (searchProcess->state() != QProcess::NotRunning) if (searchProcess->state() != QProcess::NotRunning)
searchProcess->terminate(); searchProcess->terminate();
searchProcess->waitForFinished(1000);
}
if (searchTimeout->isActive()) if (searchTimeout->isActive())
searchTimeout->stop(); searchTimeout->stop();
search_stopped = true; search_stopped = true;
if (currentSearchTab == activeSearchTab) currentSearchTab = 0;
activeSearchTab = 0; activeSearchTab = 0;
} }
delete all_tab.takeAt(index); delete all_tab.takeAt(index);
if (!all_tab.size()) { if (!all_tab.size()) {
download_button->setEnabled(false); download_button->setEnabled(false);
goToDescBtn->setEnabled(false); goToDescBtn->setEnabled(false);
search_status->setText(tr("Stopped"));
} }
} }

Loading…
Cancel
Save