Fix possible crash in ads preloading.

This commit is contained in:
John Preston 2024-11-29 10:25:21 +04:00
parent 03e4592082
commit b4f173cdb3
2 changed files with 12 additions and 13 deletions

View file

@ -321,30 +321,26 @@ HistoryWidget::HistoryWidget(
_list->onParentGeometryChanged(); _list->onParentGeometryChanged();
}), lifetime()); }), lifetime());
const auto weak = Ui::MakeWeak(this);
_scroll->addContentRequests( _scroll->addContentRequests(
) | rpl::start_with_next([=] { ) | rpl::start_with_next([=] {
if (_history && _history->loadedAtBottom()) { if (_history && _history->loadedAtBottom()) {
using Result = Data::SponsoredMessages::AppendResult; using Result = Data::SponsoredMessages::AppendResult;
const auto tryToAppend = [=] { const auto tryToAppend = [=] {
const auto r = session().sponsoredMessages().append(_history); const auto sponsored = &session().sponsoredMessages();
if (r == Result::Appended) { const auto result = sponsored->append(_history);
if (result == Result::Appended) {
_scroll->contentAdded(); _scroll->contentAdded();
} }
return r; return result;
}; };
if (tryToAppend() == Result::MediaLoading) { if (tryToAppend() == Result::MediaLoading
const auto sharedLifetime = std::make_shared<rpl::lifetime>(); && !_historySponsoredPreloading) {
session().downloaderTaskFinished( session().downloaderTaskFinished(
) | rpl::start_with_next([=] { ) | rpl::start_with_next([=] {
if (const auto strong = weak.data()) { if (tryToAppend() != Result::MediaLoading) {
if (tryToAppend() != Result::MediaLoading) { _historySponsoredPreloading.destroy();
sharedLifetime->destroy();
}
} else {
sharedLifetime->destroy();
} }
}, *sharedLifetime); }, _historySponsoredPreloading);
} }
} }
}, lifetime()); }, lifetime());
@ -2625,6 +2621,7 @@ void HistoryWidget::setHistory(History *history) {
unregisterDraftSources(); unregisterDraftSources();
clearAllLoadRequests(); clearAllLoadRequests();
clearSupportPreloadRequest(); clearSupportPreloadRequest();
_historySponsoredPreloading.destroy();
const auto wasHistory = base::take(_history); const auto wasHistory = base::take(_history);
const auto wasMigrated = base::take(_migrated); const auto wasMigrated = base::take(_migrated);
unloadHeavyViewParts(wasHistory); unloadHeavyViewParts(wasHistory);

View file

@ -744,6 +744,8 @@ private:
QPointer<HistoryInner> _list; QPointer<HistoryInner> _list;
History *_migrated = nullptr; History *_migrated = nullptr;
History *_history = nullptr; History *_history = nullptr;
rpl::lifetime _historySponsoredPreloading;
// Initial updateHistoryGeometry() was called. // Initial updateHistoryGeometry() was called.
bool _historyInited = false; bool _historyInited = false;
// If updateListSize() was called without updateHistoryGeometry(). // If updateListSize() was called without updateHistoryGeometry().