From 2661fe5cd50e0615d442825204ad80c8d616508e Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Sun, 21 May 2017 12:25:54 +0300
Subject: [PATCH] Fix crash in clear history after report spam.

---
 Telegram/SourceFiles/historywidget.cpp | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/Telegram/SourceFiles/historywidget.cpp b/Telegram/SourceFiles/historywidget.cpp
index 7f0010c7b..93fbcd53b 100644
--- a/Telegram/SourceFiles/historywidget.cpp
+++ b/Telegram/SourceFiles/historywidget.cpp
@@ -4467,18 +4467,21 @@ void HistoryWidget::onReportSpamHide() {
 
 void HistoryWidget::onReportSpamClear() {
 	Expects(_peer != nullptr);
-	if (_peer->isUser()) {
-		App::main()->deleteConversation(_peer);
-	} else if (auto chat = _peer->asChat()) {
-		App::main()->showBackFromStack();
-		MTP::send(MTPmessages_DeleteChatUser(chat->inputChat, App::self()->inputUser), App::main()->rpcDone(&MainWidget::deleteHistoryAfterLeave, _peer), App::main()->rpcFail(&MainWidget::leaveChatFailed, _peer));
-	} else if (auto channel = _peer->asChannel()) {
-		App::main()->showBackFromStack();
-		if (channel->migrateFrom()) {
-			App::main()->deleteConversation(channel->migrateFrom());
+	InvokeQueued(App::main(), [peer = _peer] {
+		if (peer->isUser()) {
+			App::main()->deleteConversation(peer);
+		} else if (auto chat = peer->asChat()) {
+			MTP::send(MTPmessages_DeleteChatUser(chat->inputChat, App::self()->inputUser), App::main()->rpcDone(&MainWidget::deleteHistoryAfterLeave, peer), App::main()->rpcFail(&MainWidget::leaveChatFailed, peer));
+		} else if (auto channel = peer->asChannel()) {
+			if (channel->migrateFrom()) {
+				App::main()->deleteConversation(channel->migrateFrom());
+			}
+			MTP::send(MTPchannels_LeaveChannel(channel->inputChannel), App::main()->rpcDone(&MainWidget::sentUpdatesReceived));
 		}
-		MTP::send(MTPchannels_LeaveChannel(channel->inputChannel), App::main()->rpcDone(&MainWidget::sentUpdatesReceived));
-	}
+	});
+
+	// Invalidates _peer.
+	App::main()->showBackFromStack();
 }
 
 void HistoryWidget::peerMessagesUpdated(PeerId peer) {