Browse Source

UI: Use QScopedPointer (not QPointer) where applicable

Contrary to what the name would have you believe, QPointer<> is not used
to delete a pointer when it leaves its specific scope.  Instead, it's
used to check to see if the pointer is still valid.  For most
QWidget-based objects, this is actually fine because QWidgets that are
assigned to layouts or other widgets will automatically be destroyed --
however, for non-widget objects, this can cause a memory leak.

This patch replaces QPointer with QScopedPointer where applicable to
prevent memory leaks.

Closes obsproject/obs-studio#1367
jp9000 7 years ago
parent
commit
b6665f9cc0

+ 2 - 2
UI/window-basic-filters.cpp

@@ -569,7 +569,7 @@ static bool QueryRemove(QWidget *parent, obs_source_t *source)
 
 void OBSBasicFilters::on_addAsyncFilter_clicked()
 {
-	QPointer<QMenu> popup = CreateAddFilterPopupMenu(true);
+	QScopedPointer<QMenu> popup(CreateAddFilterPopupMenu(true));
 	if (popup)
 		popup->exec(QCursor::pos());
 }
@@ -611,7 +611,7 @@ void OBSBasicFilters::on_asyncFilters_currentRowChanged(int row)
 
 void OBSBasicFilters::on_addEffectFilter_clicked()
 {
-	QPointer<QMenu> popup = CreateAddFilterPopupMenu(false);
+	QScopedPointer<QMenu> popup(CreateAddFilterPopupMenu(false));
 	if (popup)
 		popup->exec(QCursor::pos());
 }

+ 1 - 1
UI/window-basic-main-transitions.cpp

@@ -750,7 +750,7 @@ void OBSBasic::CreateProgramOptions()
 	programOptions->setLayout(layout);
 
 	auto onAdd = [this] () {
-		QPointer<QMenu> menu = CreateTransitionMenu(this, nullptr);
+		QScopedPointer<QMenu> menu(CreateTransitionMenu(this, nullptr));
 		menu->exec(QCursor::pos());
 	};
 

+ 14 - 14
UI/window-basic-main.cpp

@@ -233,7 +233,7 @@ OBSBasic::OBSBasic(QWidget *parent)
 
 	cpuUsageInfo = os_cpu_usage_info_start();
 	cpuUsageTimer = new QTimer(this);
-	connect(cpuUsageTimer, SIGNAL(timeout()),
+	connect(cpuUsageTimer.data(), SIGNAL(timeout()),
 			ui->statusbar, SLOT(UpdateCPUUsage()));
 	cpuUsageTimer->start(3000);
 
@@ -1694,7 +1694,7 @@ void OBSBasic::OnFirstLoad()
 					this, &OBSBasic::ReceivedIntroJson);
 		}
 		if (wnit) {
-			introCheckThread = wnit;
+			introCheckThread.reset(wnit);
 			introCheckThread->start();
 		}
 	}
@@ -2852,7 +2852,7 @@ void OBSBasic::CheckForUpdates(bool manualUpdate)
 	if (updateCheckThread && updateCheckThread->isRunning())
 		return;
 
-	updateCheckThread = new AutoUpdateThread(manualUpdate);
+	updateCheckThread.reset(new AutoUpdateThread(manualUpdate));
 	updateCheckThread->start();
 #endif
 
@@ -4227,7 +4227,7 @@ void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
 		return;
 	}
 
-	QPointer<QMenu> popup = CreateAddSourcePopupMenu();
+	QScopedPointer<QMenu> popup(CreateAddSourcePopupMenu());
 	if (popup)
 		popup->exec(pos);
 }
@@ -4381,14 +4381,13 @@ void OBSBasic::UploadLog(const char *subdir, const char *file)
 
 	if (logUploadThread) {
 		logUploadThread->wait();
-		delete logUploadThread;
 	}
 
 	RemoteTextThread *thread = new RemoteTextThread(
 			"https://obsproject.com/logs/upload",
 			"text/plain", ss.str().c_str());
 
-	logUploadThread = thread;
+	logUploadThread.reset(thread);
 	connect(thread, &RemoteTextThread::Result,
 			this, &OBSBasic::logUploadFinished);
 	logUploadThread->start();
@@ -6143,25 +6142,26 @@ void OBSBasic::ToggleShowHide()
 
 void OBSBasic::SystemTrayInit()
 {
-	trayIcon = new QSystemTrayIcon(QIcon(":/res/images/obs.png"),
-			this);
+	trayIcon.reset(new QSystemTrayIcon(QIcon(":/res/images/obs.png"),
+			this));
 	trayIcon->setToolTip("OBS Studio");
 
 	showHide = new QAction(QTStr("Basic.SystemTray.Show"),
-			trayIcon);
+			trayIcon.data());
 	sysTrayStream = new QAction(QTStr("Basic.Main.StartStreaming"),
-			trayIcon);
+			trayIcon.data());
 	sysTrayRecord = new QAction(QTStr("Basic.Main.StartRecording"),
-			trayIcon);
+			trayIcon.data());
 	sysTrayReplayBuffer = new QAction(QTStr("Basic.Main.StartReplayBuffer"),
-			trayIcon);
+			trayIcon.data());
 	exit = new QAction(QTStr("Exit"),
-			trayIcon);
+			trayIcon.data());
 
 	if (outputHandler && !outputHandler->replayBuffer)
 		sysTrayReplayBuffer->setEnabled(false);
 
-	connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
+	connect(trayIcon.data(),
+			SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
 			this,
 			SLOT(IconActivated(QSystemTrayIcon::ActivationReason)));
 	connect(showHide, SIGNAL(triggered()),

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

@@ -138,9 +138,9 @@ private:
 	const char *copyFiltersString;
 	bool copyVisible = true;
 
-	QPointer<QThread> updateCheckThread;
-	QPointer<QThread> introCheckThread;
-	QPointer<QThread> logUploadThread;
+	QScopedPointer<QThread> updateCheckThread;
+	QScopedPointer<QThread> introCheckThread;
+	QScopedPointer<QThread> logUploadThread;
 
 	QPointer<OBSBasicInteraction> interaction;
 	QPointer<OBSBasicProperties> properties;
@@ -184,7 +184,7 @@ private:
 
 	QPointer<QPushButton> replayBufferButton;
 
-	QPointer<QSystemTrayIcon> trayIcon;
+	QScopedPointer<QSystemTrayIcon> trayIcon;
 	QPointer<QAction>         sysTrayStream;
 	QPointer<QAction>         sysTrayRecord;
 	QPointer<QAction>         sysTrayReplayBuffer;