Skip to content

Commit 0fdfe40

Browse files
committed
Fix memory deallocation for native UTF-8 strings
Replaced incorrect use of Py_DecRef with malloc.free for freeing native UTF-8 strings allocated with toNativeUtf8. This ensures proper memory management and prevents potential memory leaks.
1 parent 5763e5b commit 0fdfe40

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/serious_python_android/lib/src/cpython.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ String getPythonError(CPython cpython) {
166166
final tracebackModuleNamePtr = "traceback".toNativeUtf8();
167167
var tracebackModulePtr =
168168
cpython.PyImport_ImportModule(tracebackModuleNamePtr.cast<Char>());
169-
cpython.Py_DecRef(tracebackModuleNamePtr.cast());
169+
malloc.free(tracebackModuleNamePtr);
170170

171171
if (tracebackModulePtr != nullptr) {
172172
//_debug("Traceback module loaded");
173173

174174
final formatFuncName = "format_exception".toNativeUtf8();
175175
final pFormatFunc = cpython.PyObject_GetAttrString(
176176
tracebackModulePtr, formatFuncName.cast());
177-
cpython.Py_DecRef(tracebackModuleNamePtr.cast());
177+
malloc.free(formatFuncName);
178178

179179
if (pFormatFunc != nullptr && cpython.PyCallable_Check(pFormatFunc) != 0) {
180180
// call `traceback.format_exception()` method

0 commit comments

Comments
 (0)