Skip to content

Commit 93c019f

Browse files
mos9527K0lb3
authored andcommitted
Python: Attempt to free allocated string after use
NativeAPI: Use UTF8 for string encoding
1 parent a8f0377 commit 93c019f

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

TypeTreeGeneratorAPI/NativeAPI.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ unsafe public static int TypeTreeGenerator_getMonoBehaviorDefinitions(IntPtr typ
192192
{
193193
string module = typeNames[i].Module.Name;
194194
string fullName = typeNames[i].FullName;
195-
Marshal.WriteIntPtr(arrayPtr, (i * 2) * Marshal.SizeOf<IntPtr>(), Marshal.StringToCoTaskMemAnsi(module));
196-
Marshal.WriteIntPtr(arrayPtr, (i * 2 + 1) * Marshal.SizeOf<IntPtr>(), Marshal.StringToCoTaskMemAnsi(fullName));
195+
Marshal.WriteIntPtr(arrayPtr, (i * 2) * Marshal.SizeOf<IntPtr>(), Marshal.StringToCoTaskMemUTF8(module));
196+
Marshal.WriteIntPtr(arrayPtr, (i * 2 + 1) * Marshal.SizeOf<IntPtr>(), Marshal.StringToCoTaskMemUTF8(fullName));
197197
}
198198

199199
Marshal.WriteIntPtr(arrAddrPtr, arrayPtr);

bindings/python/TypeTreeGeneratorAPI/TypeTreeGenerator.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,13 @@ def get_monobehavior_definitions(self) -> List[Tuple[str, str]]:
159159
ctypes.byref(names_ptr),
160160
ctypes.byref(names_cnt),
161161
), "failed to get module exports"
162-
names_array = ctypes.cast(
162+
ptr_array = ctypes.cast(
163163
names_ptr, ctypes.POINTER(ctypes.c_char_p * names_cnt.value)
164-
).contents
165-
names = [name.decode("ascii") for name in names_array]
164+
)
165+
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)
166169
DLL.FreeCoTaskMem(names_ptr)
167170
return [(module, fullname) for module, fullname in zip(names[::2], names[1::2])]
168171

0 commit comments

Comments
 (0)