This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: CThunkObject_dealloc should call PyObject_GC_UnTrack?
Type: behavior Stage: resolved
Components: ctypes Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, belopolsky, meador.inge, python-dev, rfk
Priority: normal Keywords: patch

Created on 2011-07-04 00:56 by rfk, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
ctypes_gcuntrack.patch rfk, 2011-07-04 00:56 review
Messages (6)
msg139724 - (view) Author: Ryan Kelly (rfk) Date: 2011-07-04 00:56
According to the docs here:

  http://docs.python.org/c-api/gcsupport.html

Any object that uses PyObject_GC_Track in its constructor must call PyObject_GC_UnTrack in its deallocator.  The CThunkObject in _ctypes does the former but not the later.  Attached patch adds a call to PyObject_GC_UnTrack.

This doesn't fix any particular bug I was seeing, I just happened to be going through the _ctypes sources (you know, for fun...) and noticed this discrepancy.
msg143852 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2011-09-11 03:08
This patch seems reasonable, is consistent with the GC docs, and passed all regression tests.  Can someone apply?
msg143916 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-09-12 16:07
I was going to say that the patch has no visible effect, since PyObject_GC_Del() calls something which has the same effect as PyObject_GC_Untrack...
But the following code crashes the interpreter! And of course the patch fixes it...


import ctypes, gc
class Nasty:
    def __del__(self):
        gc.collect()

ctypes.CFUNCTYPE(None)(lambda x=Nasty(): None)
print("OK")
msg143929 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-09-12 19:20
New changeset eb9f566fd8db by Amaury Forgeot d'Arc in branch '2.7':
Issue #12483: ctypes: Fix a crash when the destruction of a callback
http://hg.python.org/cpython/rev/eb9f566fd8db

New changeset eae8e4ab0455 by Amaury Forgeot d'Arc in branch '3.2':
Issue #12483: ctypes: Fix a crash when the destruction of a callback
http://hg.python.org/cpython/rev/eae8e4ab0455

New changeset fe125a3fda54 by Amaury Forgeot d'Arc in branch 'default':
Merge 3.2: Issue #12483: ctypes: Fix a crash when the destruction of a callback
http://hg.python.org/cpython/rev/fe125a3fda54
msg143931 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2011-09-12 19:36
Heh, I was just about to upload another patch with your test case.  Thanks for committing this Amaury.
msg143932 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-09-12 19:40
Thanks for your help! I fear they are many other places like this one in CPython code.
History
Date User Action Args
2022-04-11 14:57:19adminsetgithub: 56692
2011-09-12 19:40:11amaury.forgeotdarcsetmessages: + msg143932
2011-09-12 19:36:27meador.ingesetstatus: open -> closed
resolution: fixed
messages: + msg143931

stage: patch review -> resolved
2011-09-12 19:20:20python-devsetnosy: + python-dev
messages: + msg143929
2011-09-12 16:07:59amaury.forgeotdarcsetmessages: + msg143916
2011-09-11 03:08:12meador.ingesetnosy: + amaury.forgeotdarc, meador.inge, belopolsky
messages: + msg143852

type: behavior
stage: patch review
2011-07-04 00:56:40rfkcreate