Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8263981

Browse files
Issue #25582: Fixed 100 MB memory leak in test_ctypes.
2 parents 850be0f + b63902a commit 8263981

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

Lib/ctypes/test/test_pointers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,19 @@ def test_pointer_type_name(self):
195195
LargeNamedType = type('T' * 2 ** 25, (Structure,), {})
196196
self.assertTrue(POINTER(LargeNamedType))
197197

198+
# to not leak references, we must clean _pointer_type_cache
199+
from ctypes import _pointer_type_cache
200+
del _pointer_type_cache[LargeNamedType]
201+
198202
def test_pointer_type_str_name(self):
199203
large_string = 'T' * 2 ** 25
200-
self.assertTrue(POINTER(large_string))
204+
P = POINTER(large_string)
205+
self.assertTrue(P)
206+
207+
# to not leak references, we must clean _pointer_type_cache
208+
from ctypes import _pointer_type_cache
209+
del _pointer_type_cache[id(P)]
210+
201211

202212
if __name__ == '__main__':
203213
unittest.main()

Lib/ctypes/test/test_win32.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,9 @@ class RECT(Structure):
135135
self.assertEqual(ret.top, top.value)
136136
self.assertEqual(ret.bottom, bottom.value)
137137

138+
# to not leak references, we must clean _pointer_type_cache
139+
from ctypes import _pointer_type_cache
140+
del _pointer_type_cache[RECT]
141+
138142
if __name__ == '__main__':
139143
unittest.main()

0 commit comments

Comments
 (0)