diff --git a/Misc/NEWS.d/next/Library/2024-08-30-09-01-35.gh-issue-123504.lJ9_BB.rst b/Misc/NEWS.d/next/Library/2024-08-30-09-01-35.gh-issue-123504.lJ9_BB.rst new file mode 100644 index 00000000000..ea504d3532d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-08-30-09-01-35.gh-issue-123504.lJ9_BB.rst @@ -0,0 +1 @@ +Fixed reference leak in the finalization of :mod:`tkinter`. diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 1542d3af42f..e1e81082d9e 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -3389,17 +3389,28 @@ DisableEventHook(void) #endif } +static int +module_clear(PyObject *mod) +{ + Py_CLEAR(Tkinter_TclError); + Py_CLEAR(Tkapp_Type); + Py_CLEAR(Tktt_Type); + Py_CLEAR(PyTclObject_Type); + return 0; +} + +static void +module_free(void *mod) +{ + module_clear((PyObject *)mod); +} static struct PyModuleDef _tkintermodule = { PyModuleDef_HEAD_INIT, - "_tkinter", - NULL, - -1, - moduleMethods, - NULL, - NULL, - NULL, - NULL + .m_name = "_tkinter", + .m_methods = moduleMethods, + .m_clear = module_clear, + .m_free = module_free }; PyMODINIT_FUNC