From 9db90216d11fd313bd991feafaca69177d41535a Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 10 Feb 2025 14:05:39 +0800 Subject: [PATCH 1/2] fix a memory leak --- src/public/modules.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/public/modules.c b/src/public/modules.c index 06bac088..dbb730fa 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -156,7 +156,10 @@ __SUCCESS: c11_string__delete(filename); c11_string__delete(slashed_path); - if(need_free) PK_FREE((void*)data); + if(need_free){ + // data is from `callbacks.importfile` we should use `free()` + free((void*)data); + } return ok ? 1 : -1; } @@ -165,7 +168,7 @@ bool py_importlib_reload(py_GlobalRef module) { c11_sv path = py_tosv(py_getdict(module, __path__)); c11_string* slashed_path = c11_sv__replace(path, '.', PK_PLATFORM_SEP); c11_string* filename = c11_string__new3("%s.py", slashed_path->data); - const char* data = vm->callbacks.importfile(filename->data); + char* data = vm->callbacks.importfile(filename->data); if(data == NULL) { c11_string__delete(filename); filename = c11_string__new3("%s%c__init__.py", slashed_path->data, PK_PLATFORM_SEP); @@ -175,6 +178,7 @@ bool py_importlib_reload(py_GlobalRef module) { if(data == NULL) return ImportError("module '%v' not found", path); bool ok = py_exec(data, filename->data, EXEC_MODE, module); c11_string__delete(filename); + free(data); py_assign(py_retval(), module); return ok; } From 4d5e6f26d0e591ef94a171495e501976c156e2e7 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 11 Feb 2025 11:47:31 +0800 Subject: [PATCH 2/2] revert `free()` --- src/public/modules.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/public/modules.c b/src/public/modules.c index dbb730fa..39e14ff3 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -156,10 +156,7 @@ __SUCCESS: c11_string__delete(filename); c11_string__delete(slashed_path); - if(need_free){ - // data is from `callbacks.importfile` we should use `free()` - free((void*)data); - } + if(need_free) PK_FREE((void*)data); return ok ? 1 : -1; } @@ -178,7 +175,7 @@ bool py_importlib_reload(py_GlobalRef module) { if(data == NULL) return ImportError("module '%v' not found", path); bool ok = py_exec(data, filename->data, EXEC_MODE, module); c11_string__delete(filename); - free(data); + PK_FREE(data); py_assign(py_retval(), module); return ok; }