Sfoglia il codice sorgente

UI: Fix potential memory leak in properties

When hitting the Cancel button, cleanup code needs to go through the
reject() callback as well.
kc5nra 10 anni fa
parent
commit
dac3fd88e0
2 ha cambiato i file con 32 aggiunte e 12 eliminazioni
  1. 30 12
      obs/window-basic-properties.cpp
  2. 2 0
      obs/window-basic-properties.hpp

+ 30 - 12
obs/window-basic-properties.cpp

@@ -55,6 +55,9 @@ OBSBasicProperties::OBSBasicProperties(QWidget *parent, OBSSource source_)
 	if (cx > 400 && cy > 400)
 		resize(cx, cy);
 
+	/* The OBSData constructor increments the reference once */
+	obs_data_release(oldSettings);
+
 	OBSData settings = obs_source_get_settings(source);
 	obs_data_apply(oldSettings, settings);
 	obs_data_release(settings);
@@ -193,6 +196,32 @@ void OBSBasicProperties::timerEvent(QTimerEvent *event)
 	}
 }
 
+void OBSBasicProperties::Cleanup()
+{
+	// remove draw callback and release display in case our drawable
+	// surfaces go away before the destructor gets called
+	obs_display_remove_draw_callback(display,
+			OBSBasicProperties::DrawPreview, this);
+	display = nullptr;
+
+	config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cx",
+			width());
+	config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cy",
+			height());
+}
+
+void OBSBasicProperties::reject()
+{
+	if (!acceptClicked && (CheckSettings() != 0)) {
+		if (!ConfirmQuit()) {
+			return;
+		}
+	}
+
+	Cleanup();
+	done(0);
+}
+
 void OBSBasicProperties::closeEvent(QCloseEvent *event)
 {
 	if (!acceptClicked && (CheckSettings() != 0)) {
@@ -206,18 +235,7 @@ void OBSBasicProperties::closeEvent(QCloseEvent *event)
 	if (!event->isAccepted())
 		return;
 
-	obs_data_release(oldSettings);
-
-	// remove draw callback and release display in case our drawable
-	// surfaces go away before the destructor gets called
-	obs_display_remove_draw_callback(display,
-			OBSBasicProperties::DrawPreview, this);
-	display = nullptr;
-
-	config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cx",
-			width());
-	config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cy",
-			height());
+	Cleanup();
 }
 
 void OBSBasicProperties::Init()

+ 2 - 0
obs/window-basic-properties.hpp

@@ -50,6 +50,7 @@ private:
 	static void DrawPreview(void *data, uint32_t cx, uint32_t cy);
 	bool ConfirmQuit();
 	int  CheckSettings();
+	void Cleanup();
 
 private slots:
 	void OnPropertiesResized();
@@ -65,4 +66,5 @@ protected:
 	virtual void resizeEvent(QResizeEvent *event) override;
 	virtual void timerEvent(QTimerEvent *event) override;
 	virtual void closeEvent(QCloseEvent *event) override;
+	virtual void reject() override;
 };