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

Loading…
Cancel
Save