From 232227035c8d6a31eaaf193863cbadda949c08fd Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期二, 20 七月 2021 20:19:26 +0800
Subject: [PATCH] fix memory leak

---
 box/center_main.cc |   49 +++++++++++++++++++++++++++++++------------------
 1 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/box/center_main.cc b/box/center_main.cc
index 5baa409..8c840dc 100644
--- a/box/center_main.cc
+++ b/box/center_main.cc
@@ -19,36 +19,49 @@
 #include "box.h"
 #include "center.h"
 #include "defs.h"
+#include "log.h"
 #include "signalhandle.h"
+#include <boost/interprocess/sync/named_mutex.hpp>
 #include <chrono>
 #include <thread>
 using namespace std::chrono_literals;
+using namespace bhome_shm;
 
 int center_main(int argc, const char *argv[])
 {
 	AppArg args(argc, argv);
-	if (args.Has("remove")) {
-		BHomeShm().Remove();
-		return 0;
+
+	ns_log::AddLog(BHLogDir() + "bhshmq_center.log");
+	auto lvl = args.Get("log", "info");
+	if (strcasecmp(lvl.c_str(), "trace") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::trace); }
+	if (strcasecmp(lvl.c_str(), "debug") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::debug); }
+	if (strcasecmp(lvl.c_str(), "info") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::info); }
+	if (strcasecmp(lvl.c_str(), "warning") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::warning); }
+	if (strcasecmp(lvl.c_str(), "error") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::error); }
+	if (strcasecmp(lvl.c_str(), "fatal") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::fatal); }
+
+	if (!CenterInit()) {
+		auto msg = "init memory failed, or center is already running.";
+		LOG_FATAL() << msg;
+		printf("%s\n", msg);
+		exit(0);
+	}
+	auto &shm = BHomeShm();
+	GlobalInit(shm);
+
+	if (args.Has("daemon") || args.Has("d")) {
+		int r = daemon(0, 0); // maybe add center control msg to close itself.
 	}
 
-	bool run = true;
-	auto showStatus = [&]() {
-		auto init = BHomeShm().get_free_memory();
-		uint64_t idx = 0;
-		while (run) {
-			std::this_thread::sleep_for(1s);
-			printf("%8d shared memory: avail : %ld / %ld\n", ++idx, BHomeShm().get_free_memory(), init);
-		}
-	};
-	std::thread t(showStatus);
-
-	BHCenter center(BHomeShm());
+	BHCenter center(shm);
 	center.Start();
-	printf("center started ...\n");
+
+	auto msg = "center (" + shm.name() + ") started ...";
+	LOG_INFO() << msg;
+	printf("%s\n", msg.c_str());
 	WaitForSignals({SIGINT, SIGTERM});
-	run = false;
-	t.join();
+	center.Stop();
+	LOG_INFO() << "center stopped.";
 	return 0;
 }
 

--
Gitblit v1.8.0