-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
ctypes infinite pointer cache #100926
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
Comments
We've run into a similar issue with pypdfium2: pypdfium2-team/pypdfium2#346 |
cc @encukou |
I'm aware of the issue, can bump it up my list of priorities. Still not sure I'll be able to get a fix in 3.14. IMO, the POINTER should be cached in stginfo, rather than a global cache, so when a type goes away so does the pointer to it (there'd probably be a ref cycle for the GC to resolve, but that's better than an unbounded cache). |
Thanks. |
I would like to try, if you don't mind. |
@encukou I have made a PR, please take a look. |
…cukou/cpython into encukou/pythongh-100926-ctypes-pointers-cache
…gInfo (GH-131282) Deprecate _pointer_type_cache and calling POINTER on a string. Co-authored-by: neonene <[email protected]> Co-authored-by: Jun Komoda <[email protected]> Co-authored-by: Petr Viktorin <[email protected]>
Thanks @earonesty for the report, @sergey-miryanov for the PR, @neonene for input & reviews, @mara004 and @picnixz for pings! @junkmd, you'll want something like this to avoid deprecation warnings: - from ctypes import _pointer_type_cache # type: ignore
- _pointer_type_cache[self] = p
+ if sys.version_info >= (3, 14):
+ self.__pointer_type__ = p
+ else:
+ from ctypes import _pointer_type_cache # type: ignore
+ _pointer_type_cache[self] = p |
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:
Linked PRs
The text was updated successfully, but these errors were encountered: