From 1440866d6f2a8f60ad4521931bce996ee31cbb13 Mon Sep 17 00:00:00 2001 From: fvogel Date: Sat, 15 Apr 2023 12:11:00 +0000 Subject: Fix [310c74ecf4]: ThemeChangedProc() can be called after Tk destroyed. Patch from CHristopher Chavez. --- generic/ttk/ttkTheme.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/generic/ttk/ttkTheme.c b/generic/ttk/ttkTheme.c index 711c410..e87e579 100644 --- a/generic/ttk/ttkTheme.c +++ b/generic/ttk/ttkTheme.c @@ -405,6 +405,29 @@ typedef struct static void ThemeChangedProc(void *); /* Forward */ +/* Ttk_TkDestroyedHandler -- + * See bug [310c74ecf440]: idle calls to ThemeChangedProc() + * need to be canceled when Tk is destroyed, since the interp + * may still be active afterward; canceling them from + * Ttk_StylePkgFree() would be too late. + */ +static void Ttk_TkDestroyedHandler( + ClientData clientData, + TCL_UNUSED(Tcl_Interp*), + TCL_UNUSED(const char*), + TCL_UNUSED(const char*), + TCL_UNUSED(int)) +{ + StylePackageData* pkgPtr = (StylePackageData*)clientData; + + /* + * Cancel any pending ThemeChanged calls: + */ + if (pkgPtr->themeChangePending) { + Tcl_CancelIdleCall(ThemeChangedProc, pkgPtr); + } +} + /* Ttk_StylePkgFree -- * Cleanup procedure for StylePackageData. */ @@ -418,13 +441,6 @@ static void Ttk_StylePkgFree( Cleanup *cleanup; /* - * Cancel any pending ThemeChanged calls: - */ - if (pkgPtr->themeChangePending) { - Tcl_CancelIdleCall(ThemeChangedProc, pkgPtr); - } - - /* * Free themes. */ entryPtr = Tcl_FirstHashEntry(&pkgPtr->themeTable, &search); @@ -1728,6 +1744,8 @@ void Ttk_StylePkgInit(Tcl_Interp *interp) Tcl_SetAssocData(interp, PKG_ASSOC_KEY, Ttk_StylePkgFree, pkgPtr); + Tcl_TraceCommand(interp, ".", TCL_TRACE_DELETE, Ttk_TkDestroyedHandler, pkgPtr); + /* * Create the default system theme: * -- cgit v0.12 From e83171231902bf78c262a583a6e7a90ff3d18a8b Mon Sep 17 00:00:00 2001 From: fvogel Date: Mon, 1 May 2023 14:22:06 +0000 Subject: Alternate fix for [310c74ecf4], not making use of a delete trace. --- generic/tkInt.h | 3 ++- generic/tkWindow.c | 1 + generic/ttk/ttkTheme.c | 46 +++++++++++++++++++--------------------------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/generic/tkInt.h b/generic/tkInt.h index 35b7e67..7568585 100644 --- a/generic/tkInt.h +++ b/generic/tkInt.h @@ -1092,10 +1092,11 @@ extern "C" { #endif /* - * Themed widget set init function: + * Themed widget set init function, and handler called when Tk is destroyed. */ MODULE_SCOPE int Ttk_Init(Tcl_Interp *interp); +MODULE_SCOPE void Ttk_TkDestroyedHandler(Tcl_Interp *interp); /* * Internal functions shared among Tk modules but not exported to the outside diff --git a/generic/tkWindow.c b/generic/tkWindow.c index fa25553..59c1243 100644 --- a/generic/tkWindow.c +++ b/generic/tkWindow.c @@ -1621,6 +1621,7 @@ Tk_DestroyWindow( TkFontPkgFree(winPtr->mainPtr); TkFocusFree(winPtr->mainPtr); TkStylePkgFree(winPtr->mainPtr); + Ttk_TkDestroyedHandler(winPtr->mainPtr->interp); /* * When embedding Tk into other applications, make sure that all diff --git a/generic/ttk/ttkTheme.c b/generic/ttk/ttkTheme.c index e87e579..983678a 100644 --- a/generic/ttk/ttkTheme.c +++ b/generic/ttk/ttkTheme.c @@ -403,31 +403,6 @@ typedef struct int themeChangePending; /* scheduled ThemeChangedProc call? */ } StylePackageData; -static void ThemeChangedProc(void *); /* Forward */ - -/* Ttk_TkDestroyedHandler -- - * See bug [310c74ecf440]: idle calls to ThemeChangedProc() - * need to be canceled when Tk is destroyed, since the interp - * may still be active afterward; canceling them from - * Ttk_StylePkgFree() would be too late. - */ -static void Ttk_TkDestroyedHandler( - ClientData clientData, - TCL_UNUSED(Tcl_Interp*), - TCL_UNUSED(const char*), - TCL_UNUSED(const char*), - TCL_UNUSED(int)) -{ - StylePackageData* pkgPtr = (StylePackageData*)clientData; - - /* - * Cancel any pending ThemeChanged calls: - */ - if (pkgPtr->themeChangePending) { - Tcl_CancelIdleCall(ThemeChangedProc, pkgPtr); - } -} - /* Ttk_StylePkgFree -- * Cleanup procedure for StylePackageData. */ @@ -547,6 +522,25 @@ static void ThemeChanged(StylePackageData *pkgPtr) } } +/* Ttk_TkDestroyedHandler -- + * See bug [310c74ecf440]: idle calls to ThemeChangedProc() + * need to be canceled when Tk is destroyed, since the interp + * may still be active afterward; canceling them from + * Ttk_StylePkgFree() would be too late. + */ +void Ttk_TkDestroyedHandler( + Tcl_Interp* interp) +{ + StylePackageData* pkgPtr = (StylePackageData*)GetStylePackageData(interp); + + /* + * Cancel any pending ThemeChanged calls: + */ + if (pkgPtr->themeChangePending) { + Tcl_CancelIdleCall(ThemeChangedProc, pkgPtr); + } +} + /* * Ttk_CreateTheme -- * Create a new theme and register it in the global theme table. @@ -1744,8 +1738,6 @@ void Ttk_StylePkgInit(Tcl_Interp *interp) Tcl_SetAssocData(interp, PKG_ASSOC_KEY, Ttk_StylePkgFree, pkgPtr); - Tcl_TraceCommand(interp, ".", TCL_TRACE_DELETE, Ttk_TkDestroyedHandler, pkgPtr); - /* * Create the default system theme: * -- cgit v0.12 From 39531da4ec5e9fabc1cace60c152b9737cb2e2f7 Mon Sep 17 00:00:00 2001 From: fvogel Date: Tue, 2 May 2023 20:21:41 +0000 Subject: Remove superfluous casts. --- generic/ttk/ttkTheme.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/generic/ttk/ttkTheme.c b/generic/ttk/ttkTheme.c index 983678a..3cea404 100644 --- a/generic/ttk/ttkTheme.c +++ b/generic/ttk/ttkTheme.c @@ -477,7 +477,7 @@ static StylePackageData *GetStylePackageData(Tcl_Interp *interp) void Ttk_RegisterCleanup( Tcl_Interp *interp, ClientData clientData, Ttk_CleanupProc *cleanupProc) { - StylePackageData *pkgPtr = (StylePackageData *)GetStylePackageData(interp); + StylePackageData *pkgPtr = GetStylePackageData(interp); Cleanup *cleanup = (Cleanup *)ckalloc(sizeof(*cleanup)); cleanup->clientData = clientData; @@ -531,7 +531,7 @@ static void ThemeChanged(StylePackageData *pkgPtr) void Ttk_TkDestroyedHandler( Tcl_Interp* interp) { - StylePackageData* pkgPtr = (StylePackageData*)GetStylePackageData(interp); + StylePackageData* pkgPtr = GetStylePackageData(interp); /* * Cancel any pending ThemeChanged calls: -- cgit v0.12