From eff340deaf3ac422b130347e2806131317992785 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Mon, 29 Jun 2020 22:12:40 +0400
Subject: [PATCH] Fix touchbar destruction with Window.

---
 .../SourceFiles/platform/mac/mac_touchbar.mm    | 13 +++++++++++++
 .../SourceFiles/platform/mac/main_window_mac.h  |  1 +
 .../SourceFiles/platform/mac/main_window_mac.mm | 17 +++++++++--------
 3 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/Telegram/SourceFiles/platform/mac/mac_touchbar.mm b/Telegram/SourceFiles/platform/mac/mac_touchbar.mm
index 35fb4971ca..7ffdc970e3 100644
--- a/Telegram/SourceFiles/platform/mac/mac_touchbar.mm
+++ b/Telegram/SourceFiles/platform/mac/mac_touchbar.mm
@@ -454,6 +454,7 @@ void AppendEmojiPacks(
 
 @interface PinnedDialogsPanel : NSImageView
 - (id)init:(not_null<Main::Session*>)session;
+- (void)dealloc;
 @end // @interface PinnedDialogsPanel
 
 @implementation PinnedDialogsPanel {
@@ -770,6 +771,9 @@ void AppendEmojiPacks(
 
 	const auto updatePanelSize = [=] {
 		const auto size = lastDialogsCount->current();
+		if (self.image) {
+			[self.image release];
+		}
 		self.image = [[NSImage alloc] initWithSize:NSMakeSize(
 			size * (kCircleDiameter + kPinnedButtonsSpace)
 				+ kPinnedButtonsLeftSkip
@@ -990,6 +994,13 @@ void AppendEmojiPacks(
 	return self;
 }
 
+- (void)dealloc {
+	if (self.image) {
+		[self.image release];
+	}
+	[super dealloc];
+}
+
 - (int)shift {
 	return (_hasArchive ? 1 : 0) + (_selfUnpinned ? 1 : 0);
 }
@@ -1890,6 +1901,8 @@ void AppendEmojiPacks(
 }
 
 -(void)dealloc {
+	[_touchBarMain release];
+	[_touchBarAudioPlayer release];
 	[super dealloc];
 }
 
diff --git a/Telegram/SourceFiles/platform/mac/main_window_mac.h b/Telegram/SourceFiles/platform/mac/main_window_mac.h
index f8a90c9d73..431cafe13a 100644
--- a/Telegram/SourceFiles/platform/mac/main_window_mac.h
+++ b/Telegram/SourceFiles/platform/mac/main_window_mac.h
@@ -96,6 +96,7 @@ private:
 	void hideAndDeactivate();
 	void updateTitleCounter();
 	void updateIconCounters();
+	void destroyCurrentTouchBar();
 
 	std::unique_ptr<Private> _private;
 
diff --git a/Telegram/SourceFiles/platform/mac/main_window_mac.mm b/Telegram/SourceFiles/platform/mac/main_window_mac.mm
index 4fd80c6289..449fb0fbc5 100644
--- a/Telegram/SourceFiles/platform/mac/main_window_mac.mm
+++ b/Telegram/SourceFiles/platform/mac/main_window_mac.mm
@@ -491,14 +491,6 @@ void MainWindow::initTouchBar() {
 		}
 	};
 
-	const auto destroyCurrentTouchBar = [=] {
-		if (_private->_touchBar) {
-			[_private->_touchBar setTouchBar:Platform::TouchBarType::None];
-			[_private->_touchBar release];
-		}
-		_private->_touchBar = nil;
-	};
-
 	Core::App().domain().activeSessionChanges(
 	) | rpl::start_with_next([=](Main::Session *session) {
 		if (session) {
@@ -512,6 +504,14 @@ void MainWindow::initTouchBar() {
 	}, lifetime());
 }
 
+void MainWindow::destroyCurrentTouchBar() {
+	if (_private->_touchBar) {
+		[_private->_touchBar setTouchBar:Platform::TouchBarType::None];
+		[_private->_touchBar release];
+	}
+	_private->_touchBar = nil;
+}
+
 void MainWindow::closeWithoutDestroy() {
 	NSWindow *nsWindow = [reinterpret_cast<NSView*>(winId()) window];
 
@@ -875,6 +875,7 @@ bool MainWindow::eventFilter(QObject *obj, QEvent *evt) {
 }
 
 MainWindow::~MainWindow() {
+	destroyCurrentTouchBar();
 }
 
 } // namespace