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.

Title: Reference count bug fix
Type: Stage:
Components: Tkinter Versions:
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: loewis Nosy List: loewis, mdehoon
Priority: normal Keywords: patch

Created on 2005-02-12 06:02 by mdehoon, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
Tkinter.diff mdehoon, 2005-02-12 06:02 Patch for
Messages (5)
msg47768 - (view) Author: Michiel de Hoon (mdehoon) * Date: 2005-02-12 06:02
In the _loadtk method of the class Tk, there are two
calls, one for 'tkerror' and one
for 'exit'. In the createcommand function in
_tkinter.c, there is a Py_XINCREF for the Tk widget and
one for the Python function (_tkerror or _exit). The
corresponding Py_XDECREFs are called (via
Tkapp_DeleteCommand) in PythonCmdDelete. The
Tkapp_DeleteCommand is called by the deletecommand in
the destroy method of class Misc in
However, the deletecommand is called only for commands
listed in self._tclCommands. Now, if a command is
created via the _register method in class Misc, the
command is appended to self._tclCommands. However, the
'tkerror' and 'exit' commands, created in the _loadtk
method of the class Tk, are not appended to
self._tclCommands. Hence, the '_tkerror' and '_exit'
functions as well as the Tk widget itself have an
incorrect reference count. The Tkapp_Dealloc routine in
_tkinter.c is not called and the Tk widget is not
The attached patch simply adds a
self._tclCommands.append for 'tkerror' and 'exit'. With
this patch, a Tk widget is destroyed and Tkapp_Dealloc
is called correctly.
msg47769 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2005-02-27 19:27
Logged In: YES 

Wouldn't it be better to use _register for _tkerror and
_exit as well? This patch overwrites self._tclCommands, but
the object may already have a _tclCommands attribute.
msg47770 - (view) Author: Michiel de Hoon (mdehoon) * Date: 2005-02-28 03:19
Logged In: YES 

If we use _register without modification, we'll end up with
a different command name:
>>> from Tkinter import Label
>>> l = Label(text="some text")
+ Tkinter created command -1214096332_tkerror
+ Tkinter created command -1208510068_exit
+ Tkinter created command -1208511348destroy
whereas the command names should be "_tkerror", "_exit".
We can modify the _register functon so that it takes an
optional "name" keyword argument. However, it will mean that
we create the command through the CallWrapper instead of the
command directly. For "_exit", if I understand the code
correctly, it would mean that the raise in _exit is obscured
by the raise in CallWrapper, which doesn't seem the right thing.

So I would suggest to add a "if self._tclCommands==None:" to
the patch instead of going through _register. I'll update
the patch if you agree.
msg47771 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2005-03-01 08:12
Logged In: YES 

Thanks for the patch, applied as 1.182 and
NEWS 1.1248 and 1.1193.2.23

I added the test for _tclCommands is None, please verify the

BTW, how would I make the exit command work? It seems that
it does not
properly cause a Python exit, but instead causes a TclError.
msg47772 - (view) Author: Michiel de Hoon (mdehoon) * Date: 2005-03-03 05:29
Logged In: YES 

I tried the CVS version on a few platforms (Cygwin, Mac OS
X, Linux) and I found no problems with it. Also the Python
test suite didn't reveal any problems. Thanks for looking at
this patch. I really appreciate your 5-to-1 patch review offer.
To answer your question about the exit command, it may be an
attempt to replace Tcl's exit command, so that the Tcl
interpreter doesn't exit. But I don't know, really, I don't
use Tkinter much. My real interest is the PyOS_InputHook
event hook function, which I need to work correctly for a
scientific plotting package for Python. While working on a
separate patch involving PyOS_InputHook, I noticed that
Tkinter doesn't call the DisableEventHook problem. This led
me to the reference count problem.
Date User Action Args
2022-04-11 14:56:09adminsetgithub: 41568
2005-02-12 06:02:58mdehooncreate