From b5cddaed10c26517a8e3128f7f2a6da4600953a8 Mon Sep 17 00:00:00 2001 From: cgzhang6 Date: Tue, 2 Apr 2024 19:31:17 +0800 Subject: [PATCH] fix memory leak --- Makefile | 2 +- fswatch.cpp | 4 ++-- pyWrapper.cpp | 32 ++++++++++++++++++++++---------- pyWrapper.h | 2 +- wrapper.cpp | 10 +++++----- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 923bfa56..0eb84472 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ endif wrapper: echo $(PYLIB) - ${CC} -Wall -pedantic -Wextra -fPIC -shared -std=c++1y -fvisibility=default -Wno-attributes $(PYINC) -g -O0 -I. -I./include/spdlog/include/ -I./include/ -o libwrapper.so pyWrapper.cpp fswatch.cpp wrapper.cpp -L. -L /opt/conda/envs/loader/lib $(PYLIB) -rdynamic + ${CC} -Wall -pedantic -Wextra -fPIC -shared -std=c++1y -fvisibility=default -Wno-unused-variable -Wno-deprecated-declarations -Wno-unused-parameter -Wno-attributes $(PYINC) -g -O0 -I. -I./include/spdlog/include/ -I./include/ -o libwrapper.so pyWrapper.cpp fswatch.cpp wrapper.cpp -L. -L /opt/conda/envs/loader/lib $(PYLIB) -rdynamic mkdir -p wrapper_lib cp libwrapper.so ./wrapper_lib diff --git a/fswatch.cpp b/fswatch.cpp index ce0a209e..9e845717 100644 --- a/fswatch.cpp +++ b/fswatch.cpp @@ -208,9 +208,9 @@ int main() { EventHandle e = test; funs.insert({"IN_MOVE_SELF", test}); ino.InitWatchFile(s, NULL); - printf("pid,%d\n", _pid); + printf("pid,%lu\n", _pid); int ret = ino.StartWatchThread(funs, _pid); - printf("aftpid,%d\n", _pid); + printf("aftpid,%lu\n", _pid); ret = pthread_join(_pid, NULL); printf("%d\n", ret); return 0; diff --git a/pyWrapper.cpp b/pyWrapper.cpp index 1b133306..c2dca3eb 100644 --- a/pyWrapper.cpp +++ b/pyWrapper.cpp @@ -95,7 +95,7 @@ py::bytes DataListNode::get_data() { } DataListNode *DataListCls::get(std::string key) { - for (int idx = 0; idx < list.size(); idx++) { + for (size_t idx = 0; idx < list.size(); idx++) { DataListNode *node = &list[idx]; if (strcmp(node->key.c_str(), key.c_str()) == 0) { return node; @@ -203,7 +203,7 @@ void PyWrapper::StartMonitorWrapperClass(std::string wrapperFileAbs) { FSInotify *ino = new FSInotify(); pthread_t _pid; std::vector s; - printf("starting monitoring %s, pid is: %d\n", wrapperFileAbs.c_str(), _pid); + printf("starting monitoring %s, pid is: %lu\n", wrapperFileAbs.c_str(), _pid); s.push_back(wrapperFileAbs); std::map funs; @@ -212,7 +212,7 @@ void PyWrapper::StartMonitorWrapperClass(std::string wrapperFileAbs) { ino->InitWatchFile(s, this); int ret = ino->StartWatchThread(funs, _pid); if (ret != 0) { - printf("Error starting monitoring %s, pid is: %d\n", wrapperFileAbs.c_str(), _pid); + printf("Error starting monitoring %s, pid is: %lu\n", wrapperFileAbs.c_str(), _pid); } } @@ -299,7 +299,7 @@ int PyWrapper::wrapperOnceExec(const char *usrTag, std::map data), len); item.len = p->len; - char t = static_cast(p->type); + // char t = static_cast(p->type); item.type = p->type; item.status = p->status; spdlog::debug("reqDatatype :{},patch_id:{}", p->type, patch_id); @@ -593,7 +593,7 @@ int PyWrapper::wrapperTest() { std::cout << e.what() << std::endl; return -1; } - for (int i = 0; i < l->list.size(); ++i) { + for (size_t i = 0; i < l->list.size(); ++i) { ResponseData d = l->list[i]; // std::cout << "Response len" << d.len << std::endl; // std::cout << "response actual data Size " << d.data.length() << std::endl; @@ -615,7 +615,7 @@ int callbackMetric(const char *usrTag, const char *meterKey, int count) { } int callbackTrace(const char *usrTag, const char *key, const char *value) { - printf("callback Trace: %s, %s, %d\n", usrTag, key, value); + printf("callback Trace: %s, %s, %s\n", usrTag, key, value); return g_trace_cb(usrTag, key, value); } @@ -626,8 +626,8 @@ int callBack(Response *resp, char *usrTag) { printf("null cb....\n"); return -1; } - pDataList headPtr; - pDataList curPtr; + pDataList headPtr = nullptr; + pDataList curPtr = nullptr; int ret ; // 先判断python有没有抛出错误. response中的 errorCode if (resp->errCode != 0) { @@ -679,7 +679,19 @@ int callBack(Response *resp, char *usrTag) { } int cb_ret = cb_(usrTag, headPtr, 0); - spdlog::debug("call c's callback,usrTag:{} ret {}",usrTag, cb_ret); + while (headPtr != nullptr) { + pDataList ptr = headPtr; + headPtr = headPtr->next; + if (ptr->key) { + free(ptr->key); + } + if (ptr->data) { + free(ptr->data); + } + delete ptr; + } + + spdlog::debug("call c's callback, usrTag:{} ret {}",usrTag, cb_ret); return 0; } diff --git a/pyWrapper.h b/pyWrapper.h index 94ad1672..972bfeeb 100644 --- a/pyWrapper.h +++ b/pyWrapper.h @@ -66,8 +66,8 @@ class ResponseData { std::string key; py::bytes data; unsigned int len; - int status; int type; + int status; }; class Response { diff --git a/wrapper.cpp b/wrapper.cpp index 9edc0361..6864c66f 100644 --- a/wrapper.cpp +++ b/wrapper.cpp @@ -63,9 +63,9 @@ void initlog(std::string logDir, std::string logpath) { // Creating a directory if (mkdir(logDir.c_str(), 0777) == -1) { - printf("log目录创建失败或已存在 %s\n", logpath); + printf("log目录创建失败或已存在 %s\n", logpath.c_str()); } else { - printf("log目录已创建, %s \n", logpath); + printf("log目录已创建, %s \n", logpath.c_str()); } auto file_logger = spdlog::rotating_logger_mt("mspper", logpath.c_str(), 1048576 * 10, 50); // Console logger @@ -116,7 +116,7 @@ int WrapperAPI wrapperInit(pConfig cfg) { pyWrapper = new PyWrapper(); setLog(loglvl); - printf("WrapperInit: 当前线程ID: %d \n", gettid()); + printf("WrapperInit: 当前线程ID: %ld \n", gettid()); if (global_metric_cb != NULL) { printf("Metric Custom func set! \n"); pyWrapper->wrapperSetMetricFunc(CTMeterCustom, global_metric_cb); @@ -131,7 +131,7 @@ int WrapperAPI wrapperInit(pConfig cfg) { } int WrapperAPI wrapperFini() { - printf("WrapperFini: 当前线程ID: %d \n", gettid()); + printf("WrapperFini: 当前线程ID: %ld \n", gettid()); pyWrapper->wrapperFini(); return 0; } @@ -272,7 +272,7 @@ int WrapperAPI wrapperRead(const void *handle, pDataList *respData) { py::gil_scoped_acquire acquire; int ret = 0; // 构造响应数据 - printf("start read...sid %s\n", handle); + printf("start read...sid %p\n", handle); ret = pyWrapper->wrapperRead((char *) handle, respData); if (ret != 0) { spdlog::get("stderr_console")->error("wrapper read error!");