Browse Source

UI: Initialize YoutubeChatDock chat input members in constructor

Currently, the chat input elements (lineEdit, sendButton, and
chatLayout) are initialized when the QCefWidget gets set. This is
problematic behavior that only happened to work because we're a bit
lucky: The chat is only enabled after a widget is set, and it's only set
once. Without those conditions, the chat dock would crash when enabling
the chat before a widget is set, and the elements would get recreated if
the widget is set a second time, resulting in the original elements not
getting freed and leaking.
Moving the element creation to the constructor fixes both of these
problems, as now they're created immediately and only once.

Detected by PVS-Studio.
gxalpha 1 year ago
parent
commit
7cd5ede1e0
2 changed files with 10 additions and 7 deletions
  1. 9 6
      UI/auth-youtube.cpp
  2. 1 1
      UI/auth-youtube.hpp

+ 9 - 6
UI/auth-youtube.cpp

@@ -350,7 +350,7 @@ std::shared_ptr<Auth> YoutubeAuth::Login(QWidget *owner,
 }
 
 #ifdef BROWSER_AVAILABLE
-void YoutubeChatDock::SetWidget(QCefWidget *widget_)
+YoutubeChatDock::YoutubeChatDock(const QString &title) : BrowserDock(title)
 {
 	lineEdit = new LineEditAutoResize();
 	lineEdit->setVisible(false);
@@ -364,6 +364,14 @@ void YoutubeChatDock::SetWidget(QCefWidget *widget_)
 	chatLayout->addWidget(lineEdit, 1);
 	chatLayout->addWidget(sendButton);
 
+	QWidget::connect(lineEdit, &LineEditAutoResize::returnPressed, this,
+			 &YoutubeChatDock::SendChatMessage);
+	QWidget::connect(sendButton, &QPushButton::pressed, this,
+			 &YoutubeChatDock::SendChatMessage);
+}
+
+void YoutubeChatDock::SetWidget(QCefWidget *widget_)
+{
 	QVBoxLayout *layout = new QVBoxLayout();
 	layout->setContentsMargins(0, 0, 0, 0);
 	layout->addWidget(widget_, 1);
@@ -373,11 +381,6 @@ void YoutubeChatDock::SetWidget(QCefWidget *widget_)
 	widget->setLayout(layout);
 	setWidget(widget);
 
-	QWidget::connect(lineEdit, &LineEditAutoResize::returnPressed, this,
-			 &YoutubeChatDock::SendChatMessage);
-	QWidget::connect(sendButton, &QPushButton::pressed, this,
-			 &YoutubeChatDock::SendChatMessage);
-
 	cefWidget.reset(widget_);
 }
 

+ 1 - 1
UI/auth-youtube.hpp

@@ -21,7 +21,7 @@ private:
 	QHBoxLayout *chatLayout;
 
 public:
-	inline YoutubeChatDock(const QString &title) : BrowserDock(title) {}
+	YoutubeChatDock(const QString &title);
 	void SetWidget(QCefWidget *widget_);
 	void SetApiChatId(const std::string &id);