From 0ec347f3909a2087c9f54397dca57c21d9f0ab6c Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sat, 11 Jul 2020 14:59:54 +0300
Subject: [PATCH] Improve feed cleanup

---
 applications/gui2/src/main.cpp                | 34 ++++++++++---------
 components/common/cpp/src/configuration.cpp   | 10 ++++--
 .../rgbd-sources/include/ftl/rgbd/source.hpp  |  3 +-
 .../streams/include/ftl/streams/feed.hpp      |  1 +
 components/streams/src/feed.cpp               | 13 +++++--
 5 files changed, 39 insertions(+), 22 deletions(-)

diff --git a/applications/gui2/src/main.cpp b/applications/gui2/src/main.cpp
index 500baaca3..95cc56159 100644
--- a/applications/gui2/src/main.cpp
+++ b/applications/gui2/src/main.cpp
@@ -147,24 +147,26 @@ int main(int argc, char **argv) {
 
 	nanogui::init();
 
-	FTLGui gui(argc, argv);
+	{
+		FTLGui gui(argc, argv);
 
-	try {
-		gui.mainloop();
-	}
-	catch (const ftl::exception &e) {
+		try {
+			gui.mainloop();
+		}
+		catch (const ftl::exception &e) {
 
-		LOG(ERROR) << "Fatal error: " << e.what();
-		LOG(ERROR) << e.trace();
-	}
-	catch (const std::runtime_error &e) {
-		std::string error_msg = std::string("Caught a fatal error: ") + std::string(e.what());
-		#ifdef _WIN32
-			MessageBoxA(nullptr, error_msg.c_str(), NULL, MB_ICONERROR | MB_OK);
-		#else
-			LOG(ERROR) << error_msg;
-		#endif
-		return -1;
+			LOG(ERROR) << "Fatal error: " << e.what();
+			LOG(ERROR) << e.trace();
+		}
+		catch (const std::runtime_error &e) {
+			std::string error_msg = std::string("Caught a fatal error: ") + std::string(e.what());
+			#ifdef _WIN32
+				MessageBoxA(nullptr, error_msg.c_str(), NULL, MB_ICONERROR | MB_OK);
+			#else
+				LOG(ERROR) << error_msg;
+			#endif
+			return -1;
+		}
 	}
 
 	nanogui::shutdown();
diff --git a/components/common/cpp/src/configuration.cpp b/components/common/cpp/src/configuration.cpp
index cdd483ec0..db446490b 100644
--- a/components/common/cpp/src/configuration.cpp
+++ b/components/common/cpp/src/configuration.cpp
@@ -662,9 +662,13 @@ void ftl::config::cleanup() {
 
 	//UNIQUE_LOCK(mutex, lk);
 
-	for (auto f : config_instance) {
+	for (auto &f : config_instance) {
+		LOG(WARNING) << "Not deleted properly: " << f.second->getID();
 		//delete f.second;
-		f.second->save();
+	//	f.second->save();
+	}
+	while (config_instance.begin() != config_instance.end()) {
+		delete config_instance.begin()->second;
 	}
 	config_instance.clear();
 
@@ -674,7 +678,7 @@ void ftl::config::cleanup() {
 }
 
 void ftl::config::removeConfigurable(Configurable *cfg) {
-	if (doing_cleanup) return;
+	//if (doing_cleanup) return;
 	UNIQUE_LOCK(mutex, lk);
 
 	auto i = config_instance.find(cfg->getID());
diff --git a/components/rgbd-sources/include/ftl/rgbd/source.hpp b/components/rgbd-sources/include/ftl/rgbd/source.hpp
index 044978fdc..323a687c2 100644
--- a/components/rgbd-sources/include/ftl/rgbd/source.hpp
+++ b/components/rgbd-sources/include/ftl/rgbd/source.hpp
@@ -54,9 +54,10 @@ class Source : public ftl::Configurable, public ftl::data::DiscreteSource {
 	protected:
 	explicit Source(ftl::config::json_t &cfg);
 	Source(ftl::config::json_t &cfg, ftl::net::Universe *net);
-	virtual ~Source();
 
 	public:
+	virtual ~Source();
+	
 	/**
 	 * Is this source valid and ready to grab?.
 	 */
diff --git a/components/streams/include/ftl/streams/feed.hpp b/components/streams/include/ftl/streams/feed.hpp
index 2f6d12e03..c4f8efda5 100644
--- a/components/streams/include/ftl/streams/feed.hpp
+++ b/components/streams/include/ftl/streams/feed.hpp
@@ -86,6 +86,7 @@ private:
 	std::map<uint32_t, ftl::data::FrameSetPtr> latest_;
 
 	std::unordered_map<uint32_t, ftl::stream::Stream*> streams_;
+	std::unordered_map<uint32_t, ftl::rgbd::Source*> devices_;
 	std::unordered_map<uint32_t, ftl::operators::Graph*> pre_pipelines_;
 
 	std::vector<std::string> netcams_;
diff --git a/components/streams/src/feed.cpp b/components/streams/src/feed.cpp
index 5be03ebfe..7c770caa9 100644
--- a/components/streams/src/feed.cpp
+++ b/components/streams/src/feed.cpp
@@ -194,9 +194,16 @@ Feed::~Feed() {
 		delete filter;
 	}
 	filters_.clear();
+
 	// TODO stop everything and clean up
-	// delete pre_pipelines_
 	// delete
+
+	for (auto &p : pre_pipelines_) {
+		delete p.second;
+	}
+	for (auto &d : devices_) {
+		delete d.second;
+	}	
 }
 
 
@@ -509,8 +516,10 @@ uint32_t Feed::add(const std::string &path) {
 			// to the texture objects for use by Camera.
 			source = ftl::create<ftl::render::Source>(this, srcname, this);
 		} else {
-			source = ftl::create<ftl::rgbd::Source>(this, srcname, net_);
+			auto *dsource = ftl::create<ftl::rgbd::Source>(this, srcname, net_);
 			_createPipeline(fsid);
+			devices_[fsid] = dsource;
+			source = dsource;
 		}
 
 		// Create local builder instance
-- 
GitLab