Browse Source

UI: Fix poor handling of system tray pointers

The original pull request that added the system tray feature was riddled
with bad pointer usage.  Bare pointers that are never properly freed,
never contained within a QPointer or unique_ptr.

These have now been changed to use QPointer variables, and to always
check when the variables are valid before using.  The order of
destruction for QMenu that uses the actions has now been prioritized
first before the actions.
jp9000 9 years ago
parent
commit
9081df9708
2 changed files with 15 additions and 11 deletions
  1. 8 4
      UI/window-basic-main.cpp
  2. 7 7
      UI/window-basic-main.hpp

+ 8 - 4
UI/window-basic-main.cpp

@@ -3652,7 +3652,8 @@ inline void OBSBasic::OnActivate()
 		App()->IncrementSleepInhibition();
 		UpdateProcessPriority();
 
-		trayIcon->setIcon(QIcon(":/res/images/tray_active.png"));
+		if (trayIcon)
+			trayIcon->setIcon(QIcon(":/res/images/tray_active.png"));
 	}
 }
 
@@ -3663,7 +3664,8 @@ inline void OBSBasic::OnDeactivate()
 		App()->DecrementSleepInhibition();
 		ClearProcessPriority();
 
-		trayIcon->setIcon(QIcon(":/res/images/obs.png"));
+		if (trayIcon)
+			trayIcon->setIcon(QIcon(":/res/images/obs.png"));
 	}
 }
 
@@ -4557,7 +4559,8 @@ void OBSBasic::SetShowing(bool showing)
 			"BasicWindow", "geometry",
 			saveGeometry().toBase64().constData());
 
-		showHide->setText(QTStr("Basic.SystemTray.Show"));
+		if (showHide)
+			showHide->setText(QTStr("Basic.SystemTray.Show"));
 		QTimer::singleShot(250, this, SLOT(hide()));
 
 		if (previewEnabled)
@@ -4566,7 +4569,8 @@ void OBSBasic::SetShowing(bool showing)
 		setVisible(false);
 
 	} else if (showing && !isVisible()) {
-		showHide->setText(QTStr("Basic.SystemTray.Hide"));
+		if (showHide)
+			showHide->setText(QTStr("Basic.SystemTray.Hide"));
 		QTimer::singleShot(250, this, SLOT(show()));
 
 		if (previewEnabled)

+ 7 - 7
UI/window-basic-main.hpp

@@ -151,13 +151,13 @@ private:
 
 	QPointer<QMenu> startStreamMenu;
 
-	QSystemTrayIcon *trayIcon;
-	QMenu         *trayMenu;
-	QAction       *sysTrayStream;
-	QAction       *sysTrayRecord;
-	QAction       *showHide;
-	QAction       *showPreview;
-	QAction       *exit;
+	QPointer<QSystemTrayIcon> trayIcon;
+	QPointer<QAction>         sysTrayStream;
+	QPointer<QAction>         sysTrayRecord;
+	QPointer<QAction>         showHide;
+	QPointer<QAction>         showPreview;
+	QPointer<QAction>         exit;
+	QPointer<QMenu>           trayMenu;
 	bool          disableHiding = false;
 
 	void          DrawBackdrop(float cx, float cy);