[New-bugs-announce] [issue12998] Memory leak with CTypes Structure

M report at bugs.python.org
Sat Sep 17 08:29:26 CEST 2011


New submission from M <abcdefg.abcdefghijklmnopqrstuv at gmail.com>:

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.

See: http://stackoverflow.com/users/553702/user553702

----------
components: ctypes
messages: 144170
nosy: a01
priority: normal
severity: normal
status: open
title: Memory leak with CTypes Structure
type: resource usage

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12998>
_______________________________________


More information about the New-bugs-announce mailing list