From b4f173cdb3cc8c84db66bdf02bf926a4fbfdbe3a Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 29 Nov 2024 10:25:21 +0400 Subject: [PATCH] Fix possible crash in ads preloading. --- .../SourceFiles/history/history_widget.cpp | 23 ++++++++----------- Telegram/SourceFiles/history/history_widget.h | 2 ++ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 49fea20a1..b8f8fb0fa 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -321,30 +321,26 @@ HistoryWidget::HistoryWidget( _list->onParentGeometryChanged(); }), lifetime()); - const auto weak = Ui::MakeWeak(this); _scroll->addContentRequests( ) | rpl::start_with_next([=] { if (_history && _history->loadedAtBottom()) { using Result = Data::SponsoredMessages::AppendResult; const auto tryToAppend = [=] { - const auto r = session().sponsoredMessages().append(_history); - if (r == Result::Appended) { + const auto sponsored = &session().sponsoredMessages(); + const auto result = sponsored->append(_history); + if (result == Result::Appended) { _scroll->contentAdded(); } - return r; + return result; }; - if (tryToAppend() == Result::MediaLoading) { - const auto sharedLifetime = std::make_shared(); + if (tryToAppend() == Result::MediaLoading + && !_historySponsoredPreloading) { session().downloaderTaskFinished( ) | rpl::start_with_next([=] { - if (const auto strong = weak.data()) { - if (tryToAppend() != Result::MediaLoading) { - sharedLifetime->destroy(); - } - } else { - sharedLifetime->destroy(); + if (tryToAppend() != Result::MediaLoading) { + _historySponsoredPreloading.destroy(); } - }, *sharedLifetime); + }, _historySponsoredPreloading); } } }, lifetime()); @@ -2625,6 +2621,7 @@ void HistoryWidget::setHistory(History *history) { unregisterDraftSources(); clearAllLoadRequests(); clearSupportPreloadRequest(); + _historySponsoredPreloading.destroy(); const auto wasHistory = base::take(_history); const auto wasMigrated = base::take(_migrated); unloadHeavyViewParts(wasHistory); diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index 73a8ff1c8..6c4c9cdbb 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -744,6 +744,8 @@ private: QPointer _list; History *_migrated = nullptr; History *_history = nullptr; + rpl::lifetime _historySponsoredPreloading; + // Initial updateHistoryGeometry() was called. bool _historyInited = false; // If updateListSize() was called without updateHistoryGeometry().