Skip to content

Commit

Permalink
fix: memory leak from string cstring disposal (#40)
Browse files Browse the repository at this point in the history
* fix: memory leak from string cstring disposal

* undo test case
  • Loading branch information
bgiori committed Oct 20, 2023
1 parent af747dc commit 93c647f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/amplitude_experiment/local/evaluation/evaluation.py
@@ -1,4 +1,5 @@
from .libevaluation_interop import libevaluation_interop_symbols
from ctypes import cast, c_char_p

def evaluate(rules: str, user: str) -> str:
"""
Expand All @@ -11,4 +12,6 @@ def evaluate(rules: str, user: str) -> str:
Evaluation results with variants in JSON
"""
result = libevaluation_interop_symbols().contents.kotlin.root.evaluate(rules, user)
return str(result, 'utf-8')
py_result = cast(result, c_char_p).value
libevaluation_interop_symbols().contents.DisposeString(result)
return str(py_result, 'utf-8')
Expand Up @@ -1086,7 +1086,11 @@ class struct_anon_14(Structure):
'evaluate',
]
struct_anon_14._fields_ = [
('evaluate', CFUNCTYPE(UNCHECKED(c_char_p), String, String)),
# NOTE(bgiori): Changed this line from `UNCHECKED(c_char_p)` to `UNCHECKED(c_void_p)`
# to help fix a memory leak. Strings returned from kotlin/native must
# be freed using the DisposeString function, but c_char_p converts the
# c value making it incompatible with the dispose function.
('evaluate', CFUNCTYPE(UNCHECKED(c_void_p), String, String)),
]

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

0 comments on commit 93c647f

Please sign in to comment.