From c15019dee6110c84cd1e182dd617f6d7deb2e78d Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 30 Jun 2020 18:26:44 +0400 Subject: [PATCH] Fix crashes in MainWidget and ApiWrap. --- Telegram/SourceFiles/apiwrap.h | 1 - Telegram/SourceFiles/boxes/background_preview_box.cpp | 4 ++-- Telegram/SourceFiles/core/local_url_handlers.cpp | 10 +++++----- Telegram/SourceFiles/mainwidget.cpp | 6 +++--- Telegram/SourceFiles/mainwidget.h | 2 ++ Telegram/SourceFiles/window/window_peer_menu.cpp | 4 ++-- .../SourceFiles/window/window_session_controller.h | 7 ++----- 7 files changed, 16 insertions(+), 18 deletions(-) diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index f88e2ac3ba..b3dd94624f 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -368,7 +368,6 @@ public: not_null peer, const std::vector> &users); - rpl::producer sendActions() const { return _sendActions.events(); } diff --git a/Telegram/SourceFiles/boxes/background_preview_box.cpp b/Telegram/SourceFiles/boxes/background_preview_box.cpp index 8de1d447a0..e3f45eae5c 100644 --- a/Telegram/SourceFiles/boxes/background_preview_box.cpp +++ b/Telegram/SourceFiles/boxes/background_preview_box.cpp @@ -781,12 +781,12 @@ bool BackgroundPreviewBox::Start( Ui::show(Box(tr::lng_background_bad_link(tr::now))); return false; } - controller->session().api().requestWallPaper(slug, [=]( + controller->session().api().requestWallPaper(slug, crl::guard(controller, [=]( const Data::WallPaper &result) { Ui::show(Box( controller, result.withUrlParams(params))); - }, [](const RPCError &error) { + }), [](const RPCError &error) { Ui::show(Box(tr::lng_background_bad_link(tr::now))); }); return true; diff --git a/Telegram/SourceFiles/core/local_url_handlers.cpp b/Telegram/SourceFiles/core/local_url_handlers.cpp index 0958c49950..d70f75e4a5 100644 --- a/Telegram/SourceFiles/core/local_url_handlers.cpp +++ b/Telegram/SourceFiles/core/local_url_handlers.cpp @@ -321,12 +321,12 @@ bool ResolvePrivatePost( if (!channelId || !IsServerMsgId(msgId)) { return false; } - const auto done = [=](not_null peer) { - App::wnd()->sessionController()->showPeerHistory( + const auto done = crl::guard(controller, [=](not_null peer) { + controller->showPeerHistory( peer->id, Window::SectionShow::Way::Forward, msgId); - }; + }); const auto fail = [=] { Ui::show(Box(tr::lng_error_post_link_invalid(tr::now))); }; @@ -388,7 +388,7 @@ bool HandleUnknown( return false; } const auto request = match->captured(1); - const auto callback = [=](const MTPDhelp_deepLinkInfo &result) { + const auto callback = crl::guard(controller, [=](const MTPDhelp_deepLinkInfo &result) { const auto text = TextWithEntities{ qs(result.vmessage()), Api::EntitiesFromMTP( @@ -408,7 +408,7 @@ bool HandleUnknown( } else { Ui::show(Box(text)); } - }; + }); controller->session().api().requestDeepLinkInfo(request, callback); return true; } diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 6f27ebba4c..2d3214403e 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -230,6 +230,7 @@ MainWidget::MainWidget( not_null controller) : RpWidget(parent) , _controller(controller) +, _api(&controller->session().mtp()) , _dialogsWidth(st::columnMinimalWidthLeft) , _thirdColumnWidth(st::columnMinimalWidthThird) , _sideShadow(this) @@ -1264,7 +1265,6 @@ void MainWidget::scheduleViewIncrement(HistoryItem *item) { } void MainWidget::viewsIncrement() { - const auto api = &session().api(); for (auto i = _viewsToIncrement.begin(); i != _viewsToIncrement.cend();) { if (_viewsIncrementRequests.contains(i->first)) { ++i; @@ -1276,7 +1276,7 @@ void MainWidget::viewsIncrement() { for (const auto msgId : i->second) { ids.push_back(MTP_int(msgId)); } - const auto requestId = api->request(MTPmessages_GetMessagesViews( + const auto requestId = _api.request(MTPmessages_GetMessagesViews( i->first->input, MTP_vector(ids), MTP_bool(true) @@ -2624,7 +2624,7 @@ void MainWidget::openPeerByName( }); } } else { - session().api().request(MTPcontacts_ResolveUsername( + _api.request(MTPcontacts_ResolveUsername( MTP_string(username) )).done([=](const MTPcontacts_ResolvedPeer &result) { usernameResolveDone(result, msgId, startToken); diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h index ccf8b650e0..97a6116cb9 100644 --- a/Telegram/SourceFiles/mainwidget.h +++ b/Telegram/SourceFiles/mainwidget.h @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/rp_widget.h" #include "ui/effects/animations.h" #include "media/player/media_player_float.h" +#include "mtproto/sender.h" #include "data/data_pts_waiter.h" class RPCError; @@ -350,6 +351,7 @@ private: void handleHistoryBack(); const not_null _controller; + MTP::Sender _api; Ui::Animations::Simple _a_show; bool _showBack = false; diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp index fbbacdd83f..cc736bc86d 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.cpp +++ b/Telegram/SourceFiles/window/window_peer_menu.cpp @@ -1023,7 +1023,7 @@ void PeerMenuAddChannelMembers( return; } const auto api = &channel->session().api(); - api->requestChannelMembersForAdd(channel, [=]( + api->requestChannelMembersForAdd(channel, crl::guard(navigation, [=]( const MTPchannels_ChannelParticipants &result) { api->parseChannelParticipants(channel, result, [&]( int availableCount, @@ -1045,7 +1045,7 @@ void PeerMenuAddChannelMembers( channel, { already.begin(), already.end() }); }); - }); + })); } void PeerMenuAddMuteAction( diff --git a/Telegram/SourceFiles/window/window_session_controller.h b/Telegram/SourceFiles/window/window_session_controller.h index 6078f8f098..e0520ebee5 100644 --- a/Telegram/SourceFiles/window/window_session_controller.h +++ b/Telegram/SourceFiles/window/window_session_controller.h @@ -116,7 +116,7 @@ struct SectionShow { class SessionController; -class SessionNavigation { +class SessionNavigation : public base::has_weak_ptr { public: explicit SessionNavigation(not_null session); @@ -156,10 +156,7 @@ private: }; -class SessionController - : public SessionNavigation - , public base::has_weak_ptr - , private base::Subscriber { +class SessionController : public SessionNavigation, private base::Subscriber { public: SessionController( not_null session,