Fix possible crash in album cover fetcher

This commit is contained in:
Jonas Kvinge 2020-05-10 16:54:14 +02:00
parent 139e148912
commit d7661f0964
4 changed files with 25 additions and 4 deletions

View File

@ -40,8 +40,20 @@ AlbumCoverFetcher::AlbumCoverFetcher(CoverProviders *cover_providers, QObject *p
network_(network ? network : new NetworkAccessManager(this)), network_(network ? network : new NetworkAccessManager(this)),
next_id_(0), next_id_(0),
request_starter_(new QTimer(this)) { request_starter_(new QTimer(this)) {
request_starter_->setInterval(1000); request_starter_->setInterval(1000);
connect(request_starter_, SIGNAL(timeout()), SLOT(StartRequests())); connect(request_starter_, SIGNAL(timeout()), SLOT(StartRequests()));
}
AlbumCoverFetcher::~AlbumCoverFetcher() {
for (AlbumCoverFetcherSearch *search : active_requests_.values()) {
search->disconnect();
search->deleteLater();
}
active_requests_.clear();
} }
quint64 AlbumCoverFetcher::FetchAlbumCover(const QString &artist, const QString &album, const QString &title, bool fetchall) { quint64 AlbumCoverFetcher::FetchAlbumCover(const QString &artist, const QString &album, const QString &title, bool fetchall) {
@ -125,8 +137,8 @@ void AlbumCoverFetcher::StartRequests() {
void AlbumCoverFetcher::SingleSearchFinished(const quint64 request_id, const CoverSearchResults results) { void AlbumCoverFetcher::SingleSearchFinished(const quint64 request_id, const CoverSearchResults results) {
if (!active_requests_.contains(request_id)) return;
AlbumCoverFetcherSearch *search = active_requests_.take(request_id); AlbumCoverFetcherSearch *search = active_requests_.take(request_id);
if (!search) return;
search->deleteLater(); search->deleteLater();
emit SearchFinished(request_id, results, search->statistics()); emit SearchFinished(request_id, results, search->statistics());
@ -135,8 +147,8 @@ void AlbumCoverFetcher::SingleSearchFinished(const quint64 request_id, const Cov
void AlbumCoverFetcher::SingleCoverFetched(const quint64 request_id, const QUrl &cover_url, const QImage &image) { void AlbumCoverFetcher::SingleCoverFetched(const quint64 request_id, const QUrl &cover_url, const QImage &image) {
if (!active_requests_.contains(request_id)) return;
AlbumCoverFetcherSearch *search = active_requests_.take(request_id); AlbumCoverFetcherSearch *search = active_requests_.take(request_id);
if (!search) return;
search->deleteLater(); search->deleteLater();
emit AlbumCoverFetched(request_id, cover_url, image, search->statistics()); emit AlbumCoverFetched(request_id, cover_url, image, search->statistics());

View File

@ -89,7 +89,7 @@ class AlbumCoverFetcher : public QObject {
public: public:
explicit AlbumCoverFetcher(CoverProviders *cover_providers, QObject *parent = nullptr, QNetworkAccessManager *network = 0); explicit AlbumCoverFetcher(CoverProviders *cover_providers, QObject *parent = nullptr, QNetworkAccessManager *network = 0);
virtual ~AlbumCoverFetcher() {} ~AlbumCoverFetcher();
static const int kMaxConcurrentRequests; static const int kMaxConcurrentRequests;

View File

@ -61,6 +61,11 @@ AlbumCoverFetcherSearch::AlbumCoverFetcherSearch(
} }
AlbumCoverFetcherSearch::~AlbumCoverFetcherSearch() {
pending_requests_.clear();
Cancel();
}
void AlbumCoverFetcherSearch::TerminateSearch() { void AlbumCoverFetcherSearch::TerminateSearch() {
for (quint64 id : pending_requests_.keys()) { for (quint64 id : pending_requests_.keys()) {
@ -205,6 +210,7 @@ void AlbumCoverFetcherSearch::FetchMoreImages() {
void AlbumCoverFetcherSearch::ProviderCoverFetchFinished(QNetworkReply *reply) { void AlbumCoverFetcherSearch::ProviderCoverFetchFinished(QNetworkReply *reply) {
disconnect(reply, &QNetworkReply::finished, this, nullptr);
reply->deleteLater(); reply->deleteLater();
if (!pending_image_loads_.contains(reply)) return; if (!pending_image_loads_.contains(reply)) return;
@ -280,7 +286,7 @@ void AlbumCoverFetcherSearch::SendBestImage() {
cover_url = best_image.first.image_url; cover_url = best_image.first.image_url;
image = best_image.second; image = best_image.second;
qLog(Info) << "Using " << best_image.first.image_url << "from" << best_image.first.provider << "with score" << best_image.first.score; qLog(Info) << "Using" << best_image.first.image_url << "from" << best_image.first.provider << "with score" << best_image.first.score;
statistics_.chosen_images_by_provider_[best_image.first.provider]++; statistics_.chosen_images_by_provider_[best_image.first.provider]++;
statistics_.chosen_images_++; statistics_.chosen_images_++;
@ -304,7 +310,9 @@ void AlbumCoverFetcherSearch::Cancel() {
} }
else if (!pending_image_loads_.isEmpty()) { else if (!pending_image_loads_.isEmpty()) {
for (QNetworkReply *reply : pending_image_loads_.keys()) { for (QNetworkReply *reply : pending_image_loads_.keys()) {
disconnect(reply, &QNetworkReply::finished, this, nullptr);
reply->abort(); reply->abort();
reply->deleteLater();
} }
pending_image_loads_.clear(); pending_image_loads_.clear();
} }

View File

@ -49,6 +49,7 @@ class AlbumCoverFetcherSearch : public QObject {
public: public:
explicit AlbumCoverFetcherSearch(const CoverSearchRequest &request, QNetworkAccessManager *network, QObject *parent); explicit AlbumCoverFetcherSearch(const CoverSearchRequest &request, QNetworkAccessManager *network, QObject *parent);
~AlbumCoverFetcherSearch();
void Start(CoverProviders *cover_providers); void Start(CoverProviders *cover_providers);