Skip to content

Commit 93c647f

Browse files
authored
fix: memory leak from string cstring disposal (#40)
* fix: memory leak from string cstring disposal * undo test case
1 parent af747dc commit 93c647f

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/amplitude_experiment/local/evaluation/evaluation.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .libevaluation_interop import libevaluation_interop_symbols
2+
from ctypes import cast, c_char_p
23

34
def evaluate(rules: str, user: str) -> str:
45
"""
@@ -11,4 +12,6 @@ def evaluate(rules: str, user: str) -> str:
1112
Evaluation results with variants in JSON
1213
"""
1314
result = libevaluation_interop_symbols().contents.kotlin.root.evaluate(rules, user)
14-
return str(result, 'utf-8')
15+
py_result = cast(result, c_char_p).value
16+
libevaluation_interop_symbols().contents.DisposeString(result)
17+
return str(py_result, 'utf-8')

src/amplitude_experiment/local/evaluation/libevaluation_interop.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,11 @@ class struct_anon_14(Structure):
10861086
'evaluate',
10871087
]
10881088
struct_anon_14._fields_ = [
1089-
('evaluate', CFUNCTYPE(UNCHECKED(c_char_p), String, String)),
1089+
# NOTE(bgiori): Changed this line from `UNCHECKED(c_char_p)` to `UNCHECKED(c_void_p)`
1090+
# to help fix a memory leak. Strings returned from kotlin/native must
1091+
# be freed using the DisposeString function, but c_char_p converts the
1092+
# c value making it incompatible with the dispose function.
1093+
('evaluate', CFUNCTYPE(UNCHECKED(c_void_p), String, String)),
10901094
]
10911095

10921096
# src/amplitude_experiment/local/evaluation/lib/macosX64/libevaluation_interop_api.h: 100

0 commit comments

Comments
 (0)