Skip to content

Commit b5cddae

Browse files
author
cgzhang6
committed
fix memory leak
1 parent c999a0e commit b5cddae

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

Makefile

+1-1
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

+2-2
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

+22-10
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ py::bytes DataListNode::get_data() {
9595
}
9696

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

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

@@ -299,7 +299,7 @@ int PyWrapper::wrapperOnceExec(const char *usrTag, std::map <std::string, std::s
299299
return ret;
300300
}
301301
ptr = PyBytes_AsString(itemData.data.ptr());
302-
Py_ssize_t size = PyBytes_GET_SIZE(itemData.data.ptr());
302+
// Py_ssize_t size = PyBytes_GET_SIZE(itemData.data.ptr());
303303
// printf("GetSIze, %d", size);
304304
// printf("item data len: %d", itemData.len);
305305
memcpy(pr, ptr, itemData.len);
@@ -551,7 +551,7 @@ int PyWrapper::wrapperLoadRes(pDataList p, std::string patch_id) {
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 :{},patch_id:{}", p->type, patch_id);
@@ -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 len" << d.len << std::endl;
599599
// 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) {
615615
}
616616

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

@@ -626,8 +626,8 @@ int callBack(Response *resp, char *usrTag) {
626626
printf("null cb....\n");
627627
return -1;
628628
}
629-
pDataList headPtr;
630-
pDataList curPtr;
629+
pDataList headPtr = nullptr;
630+
pDataList curPtr = nullptr;
631631
int ret ;
632632
// 先判断python有没有抛出错误. response中的 errorCode
633633
if (resp->errCode != 0) {
@@ -679,7 +679,19 @@ int callBack(Response *resp, char *usrTag) {
679679
}
680680

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

685697
}

pyWrapper.h

+1-1
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

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ void initlog(std::string logDir, std::string logpath) {
6363

6464
// Creating a directory
6565
if (mkdir(logDir.c_str(), 0777) == -1) {
66-
printf("log目录创建失败或已存在 %s\n", logpath);
66+
printf("log目录创建失败或已存在 %s\n", logpath.c_str());
6767
} else {
68-
printf("log目录已创建, %s \n", logpath);
68+
printf("log目录已创建, %s \n", logpath.c_str());
6969
}
7070
auto file_logger = spdlog::rotating_logger_mt("mspper", logpath.c_str(), 1048576 * 10, 50);
7171
// Console logger
@@ -116,7 +116,7 @@ int WrapperAPI wrapperInit(pConfig cfg) {
116116
pyWrapper = new PyWrapper();
117117

118118
setLog(loglvl);
119-
printf("WrapperInit: 当前线程ID: %d \n", gettid());
119+
printf("WrapperInit: 当前线程ID: %ld \n", gettid());
120120
if (global_metric_cb != NULL) {
121121
printf("Metric Custom func set! \n");
122122
pyWrapper->wrapperSetMetricFunc(CTMeterCustom, global_metric_cb);
@@ -131,7 +131,7 @@ int WrapperAPI wrapperInit(pConfig cfg) {
131131
}
132132

133133
int WrapperAPI wrapperFini() {
134-
printf("WrapperFini: 当前线程ID: %d \n", gettid());
134+
printf("WrapperFini: 当前线程ID: %ld \n", gettid());
135135
pyWrapper->wrapperFini();
136136
return 0;
137137
}
@@ -272,7 +272,7 @@ int WrapperAPI wrapperRead(const void *handle, pDataList *respData) {
272272
py::gil_scoped_acquire acquire;
273273
int ret = 0;
274274
// 构造响应数据
275-
printf("start read...sid %s\n", handle);
275+
printf("start read...sid %p\n", handle);
276276
ret = pyWrapper->wrapperRead((char *) handle, respData);
277277
if (ret != 0) {
278278
spdlog::get("stderr_console")->error("wrapper read error!");

0 commit comments

Comments
 (0)