Skip to content

ctypes infinite pointer cache  #100926

Closed
Closed
@earonesty

Description

@earonesty

Bug(ish?) report

The following function has a cache. If you are using a factory to call ctypes.POINTER in a loop, the memory usage is unbounded and unable to be reclaimed.

https://docs.python.org/3/library/ctypes.html#ctypes.POINTER

The documentation should mention that this is unbounded and should not be called in a loop, or the cache should be changed to a configurable, bounded LRU cache.

Example of a variable length type factory used by windows, and a bad func that cannot be called in a loop:

def shitemid_factory(size: int) -> Type[ctypes.Structure]:
    class SHITEMID_Var(ctypes.Structure):
        _fields_ = (
            ("cb", USHORT),
            ("abID", BYTE * size),
        )

    return SHITEMID_Var

def bad_func():
    SHITEMID_Var = shitemid_factory(sz - ctypes.sizeof(USHORT))
    item_var = ctypes.cast(item_ptr, ctypes.POINTER(SHITEMID_Var))

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions