-
-
Notifications
You must be signed in to change notification settings - Fork 30k
-
-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak with CTypes Structure #57207
Comments
When using Python 2.5.4 on Windows (and potentially other versions and platforms), usage of CTypes like the following cause a memory leak. The memory leak can be verified with ProcessExplorer, as the memory Python takes up keeps increasing until eventually Python will get a MemoryError. import ctypes
def hi():
class c1(ctypes.Structure):
_fields_=[('f1',ctypes.c_uint8)]
class c2(ctypes.Structure):
_fields_=[('g1',c1*2)]
while True:
test=hi() This is true even if the garbage collector is called explicitly: import gc
import ctypes
def hi():
class c1(ctypes.Structure):
_fields_=[('f1',ctypes.c_uint8)]
class c2(ctypes.Structure):
_fields_=[('g1',c1*2)]
while True:
test=hi()
test2=gc.collect() It is also true if "del" is called on the return value of the function. In order to get the memory leak, it appears to be necessary to have two Structure classes, with one of them containing a "multiple" of the other one as a field. In a code project where functions returning Structure classes similarly to this example are being used, MemoryError has been encountered. |
Correction to the above link: See: http://stackoverflow.com/questions/7452625/python-ctypes-memory-leak-with-structure |
This is caused by a cache which is kept of array's for different (Structure, length) pairs. |
If it is a cache, shouldn't it be garbage-collected or limited in size? Why does it grow without bounds? |
Why should it? After all, you're sort of abusing ctypes by repeatedly creating Struture types over and over again. C structures that you might want to wrap with these types are fixed and known at the time of programming, so there is never a need to create the same Structure type twice. Thus the set of Structure subclasses created during the live-time of a program is both, fixed and small, so there is no need to limit the cache size. |
I can reproduce the leak with Python 2.5.4, but not with Python 2.6.5 Python 2.5.4 is an ancient version. Please upgrade to Python 2.7 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: