Skip to content

Commit 977b87e

Browse files
author
cgzhang6
committed
fix memory leak
1 parent 665bd4d commit 977b87e

5 files changed

Lines changed: 31 additions & 19 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ endif
1818

1919
wrapper:
2020
echo $(PYLIB)
21-
${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
21+
${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
2222
mkdir -p wrapper_lib
2323
cp libwrapper.so ./wrapper_lib
2424

fswatch.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ int main() {
208208
EventHandle e = test;
209209
funs.insert({"IN_MOVE_SELF", test});
210210
ino.InitWatchFile(s, NULL);
211-
printf("pid,%d\n", _pid);
211+
printf("pid,%lu\n", _pid);
212212
int ret = ino.StartWatchThread(funs, _pid);
213-
printf("aftpid,%d\n", _pid);
213+
printf("aftpid,%lu\n", _pid);
214214
ret = pthread_join(_pid, NULL);
215215
printf("%d\n", ret);
216216
return 0;

pyWrapper.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ py::bytes DataListNode::get_data() {
9494
}
9595

9696
DataListNode *DataListCls::get(std::string key) {
97-
for (int idx = 0; idx < list.size(); idx++) {
97+
for (size_t idx = 0; idx < list.size(); idx++) {
9898
DataListNode *node = &list[idx];
9999
if (strcmp(node->key.c_str(), key.c_str()) == 0) {
100100
return node;
@@ -201,7 +201,7 @@ void PyWrapper::StartMonitorWrapperClass(std::string wrapperFileAbs) {
201201
FSInotify *ino = new FSInotify();
202202
pthread_t _pid;
203203
std::vector <std::string> s;
204-
printf("starting monitoring %s, pid is: %d\n", wrapperFileAbs.c_str(), _pid);
204+
printf("starting monitoring %s, pid is: %lu\n", wrapperFileAbs.c_str(), _pid);
205205
s.push_back(wrapperFileAbs);
206206
std::map <std::string, EventHandle> funs;
207207

@@ -210,7 +210,7 @@ void PyWrapper::StartMonitorWrapperClass(std::string wrapperFileAbs) {
210210
ino->InitWatchFile(s, this);
211211
int ret = ino->StartWatchThread(funs, _pid);
212212
if (ret != 0) {
213-
printf("Error starting monitoring %s, pid is: %d\n", wrapperFileAbs.c_str(), _pid);
213+
printf("Error starting monitoring %s, pid is: %lu\n", wrapperFileAbs.c_str(), _pid);
214214
}
215215
}
216216

@@ -297,7 +297,7 @@ int PyWrapper::wrapperOnceExec(const char *usrTag, std::map <std::string, std::s
297297
return ret;
298298
}
299299
ptr = PyBytes_AsString(itemData.data.ptr());
300-
Py_ssize_t size = PyBytes_GET_SIZE(itemData.data.ptr());
300+
// Py_ssize_t size = PyBytes_GET_SIZE(itemData.data.ptr());
301301
// printf("GetSIze, %d", size);
302302
// printf("item data len: %d", itemData.len);
303303
memcpy(pr, ptr, itemData.len);
@@ -551,7 +551,7 @@ int PyWrapper::wrapperLoadRes(pDataList p, unsigned int resId) {
551551
item.data = py::bytes((char *) (p->data), len);
552552

553553
item.len = p->len;
554-
char t = static_cast<int>(p->type);
554+
// char t = static_cast<int>(p->type);
555555
item.type = p->type;
556556
item.status = p->status;
557557
spdlog::debug("reqDatatype :{},resID:{}", p->type, resId);
@@ -593,7 +593,7 @@ int PyWrapper::wrapperTest() {
593593
std::cout << e.what() << std::endl;
594594
return -1;
595595
}
596-
for (int i = 0; i < l->list.size(); ++i) {
596+
for (size_t i = 0; i < l->list.size(); ++i) {
597597
ResponseData d = l->list[i];
598598
// std::cout << "Response key: " << d.key << std::endl;
599599
// std::cout << "Response len" << d.len << std::endl;
@@ -616,7 +616,7 @@ int callbackMetric(const char *usrTag, const char *meterKey, int count) {
616616
}
617617

618618
int callbackTrace(const char *usrTag, const char *key, const char *value) {
619-
printf("callback Trace: %s, %s, %d\n", usrTag, key, value);
619+
printf("callback Trace: %s, %s, %s\n", usrTag, key, value);
620620
return g_trace_cb(usrTag, key, value);
621621
}
622622

@@ -629,8 +629,8 @@ int callBack(Response *resp, std::string sid) {
629629
}
630630
const char *usrTag = GetSidUsrTag(sid);
631631

632-
pDataList headPtr;
633-
pDataList curPtr;
632+
pDataList headPtr = nullptr;
633+
pDataList curPtr = nullptr;
634634
// 先判断python有没有抛出错误. response中的 errorCode
635635
if (resp->errCode != 0) {
636636
spdlog::get("stderr_console")->error("find error from python: {}", resp->errCode);
@@ -679,8 +679,20 @@ int callBack(Response *resp, std::string sid) {
679679
tmpData->status, sid);
680680
}
681681

682-
cb_(usrTag, headPtr, 0);
683-
printf("ok\n:");
682+
int cb_ret = cb_(usrTag, headPtr, 0);
683+
while (headPtr != nullptr) {
684+
pDataList ptr = headPtr;
685+
headPtr = headPtr->next;
686+
if (ptr->key) {
687+
free(ptr->key);
688+
}
689+
if (ptr->data) {
690+
free(ptr->data);
691+
}
692+
delete ptr;
693+
}
694+
695+
spdlog::debug("call c's callback, usrTag:{} ret {}",usrTag, cb_ret);
684696
return 0;
685697

686698
}

pyWrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ class ResponseData {
6666
std::string key;
6767
py::bytes data;
6868
unsigned int len;
69-
int status;
7069
int type;
70+
int status;
7171
};
7272

7373
class Response {

wrapper.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ void initlog(std::string logDir, std::string logpath) {
5454

5555
// Creating a directory
5656
if (mkdir(logDir.c_str(), 0777) == -1) {
57-
printf("log目录创建失败或已存在 %s\n", logpath);
57+
printf("log目录创建失败或已存在 %s\n", logpath.c_str());
5858
} else {
59-
printf("log目录已创建, %s \n", logpath);
59+
printf("log目录已创建, %s \n", logpath.c_str());
6060
}
6161
auto file_logger = spdlog::rotating_logger_mt("mspper", logpath.c_str(), 1048576 * 10, 50);
6262
// Console logger
@@ -107,7 +107,7 @@ int WrapperAPI wrapperInit(pConfig cfg) {
107107
pyWrapper = new PyWrapper();
108108

109109
setLog(loglvl);
110-
printf("WrapperInit: 当前线程ID: %d \n", gettid());
110+
printf("WrapperInit: 当前线程ID: %ld \n", gettid());
111111
if (global_metric_cb != NULL) {
112112
printf("Metric Custom func set! \n");
113113
pyWrapper->wrapperSetMetricFunc(CTMeterCustom, global_metric_cb);
@@ -122,7 +122,7 @@ int WrapperAPI wrapperInit(pConfig cfg) {
122122
}
123123

124124
int WrapperAPI wrapperFini() {
125-
printf("WrapperFini: 当前线程ID: %d \n", gettid());
125+
printf("WrapperFini: 当前线程ID: %ld \n", gettid());
126126
pyWrapper->wrapperFini();
127127
return 0;
128128
}

0 commit comments

Comments
 (0)