[New-bugs-announce] [issue12836] cast() creates circular reference in original object

Benjamin Gilbert report at bugs.python.org
Wed Aug 24 23:53:55 CEST 2011


New submission from Benjamin Gilbert <bgilbert at backtick.net>:

Section 15.17.1.15 of the ctypes documentation illustrates the use of cast() thusly:

>>> a = (c_byte * 4)()
>>> cast(a, POINTER(c_int))
<ctypes.LP_c_long object at ...>
>>>

Executing the cast() causes a._objects to gain a reference back to a:

>>> a = (c_byte * 4)()
>>> a._objects
>>> cast(a, POINTER(c_int))
<__main__.LP_c_int object at 0x7fb879065b90>
>>> a._objects
{140430281170384: <__main__.c_byte_Array_4 object at 0x7fb879065dd0>}
>>> a
<__main__.c_byte_Array_4 object at 0x7fb879065dd0>
>>> 

If large temporary arrays are allocated, cast, used, and discarded in a tight inner loop, a lot of memory can thus be consumed by self-referencing objects before the garbage collector has a chance to run.  Even if this behavior is correct from the perspective of ctypes, it is surprising.

----------
components: ctypes
messages: 142933
nosy: bgilbert
priority: normal
severity: normal
status: open
title: cast() creates circular reference in original object
type: resource usage
versions: Python 2.7

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


More information about the New-bugs-announce mailing list