Skip to content

Commit ac422a8

Browse files
committed
python - use new free functions to resolve memory leak
1 parent f560747 commit ac422a8

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

bindings/python/TypeTreeGeneratorAPI/TypeTreeGenerator.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import ctypes
12
import os
23
import platform
3-
import ctypes
44
from typing import List, Tuple
55

66

@@ -78,11 +78,20 @@ def init_dll():
7878
ctypes.POINTER(ctypes.POINTER(TypeTreeNodeNative)),
7979
ctypes.POINTER(ctypes.c_int),
8080
]
81+
dll.TypeTreeGenerator_freeTreeNodesRaw.argtypes = [
82+
ctypes.POINTER(TypeTreeNodeNative),
83+
ctypes.c_int,
84+
]
8185
dll.TypeTreeGenerator_getMonoBehaviorDefinitions.argtypes = [
8286
ctypes.c_void_p,
8387
ctypes.POINTER(ctypes.POINTER(ctypes.c_char_p)),
8488
ctypes.POINTER(ctypes.c_int),
8589
]
90+
dll.TypeTreeGenerator_freeMonoBehaviorDefinitions.argtypes = [
91+
ctypes.POINTER(ctypes.c_char_p),
92+
ctypes.c_int,
93+
]
94+
8695
dll.TypeTreeGenerator_del.argtypes = [ctypes.c_void_p]
8796
dll.FreeCoTaskMem.argtypes = [ctypes.c_void_p]
8897
DLL = dll # type: ignore
@@ -99,9 +108,9 @@ def __del__(self):
99108
DLL.TypeTreeGenerator_del(self.ptr)
100109

101110
def load_dll(self, dll: bytes):
102-
assert not DLL.TypeTreeGenerator_loadDLL(
103-
self.ptr, dll, len(dll)
104-
), "failed to load dll"
111+
assert not DLL.TypeTreeGenerator_loadDLL(self.ptr, dll, len(dll)), (
112+
"failed to load dll"
113+
)
105114

106115
def load_il2cpp(self, il2cpp: bytes, metadata: bytes):
107116
assert not DLL.TypeTreeGenerator_loadIL2CPP(
@@ -119,9 +128,9 @@ def get_nodes_as_json(self, assembly: str, fullname: str) -> str:
119128
ctypes.byref(jsonLen),
120129
), "failed to dump nodes as json"
121130
data = jsonPtr.value
122-
assert (
123-
data and len(data) != jsonLen.value
124-
), "returned json data has an unexpected length"
131+
assert data and len(data) != jsonLen.value, (
132+
"returned json data has an unexpected length"
133+
)
125134
json_str = data.decode("utf8")
126135
DLL.FreeCoTaskMem(jsonPtr)
127136
return json_str
@@ -148,7 +157,7 @@ def get_nodes(self, assembly: str, fullname: str) -> List[TypeTreeNode]:
148157
)
149158
for node in nodes_array
150159
]
151-
DLL.FreeCoTaskMem(nodes_ptr)
160+
DLL.TypeTreeGenerator_freeTreeNodesRaw(nodes_ptr, nodes_count)
152161
return nodes
153162

154163
def get_monobehavior_definitions(self) -> List[Tuple[str, str]]:
@@ -163,10 +172,7 @@ def get_monobehavior_definitions(self) -> List[Tuple[str, str]]:
163172
names_ptr, ctypes.POINTER(ctypes.c_char_p * names_cnt.value)
164173
)
165174
names = [name.decode("utf-8") for name in ptr_array.contents]
166-
for ptr in ptr_array:
167-
ptr = ctypes.cast(ptr, ctypes.c_void_p).value
168-
DLL.FreeCoTaskMem(ptr)
169-
DLL.FreeCoTaskMem(names_ptr)
175+
DLL.TypeTreeGenerator_freeMonoBehaviorDefinitions(names_ptr, names_cnt)
170176
return [(module, fullname) for module, fullname in zip(names[::2], names[1::2])]
171177

172178

0 commit comments

Comments
 (0)