An attempt to fix Bug #3020550, limited to [grid]-related code (other geometry managers are probably affected, and fixable using the same approach).
The patch introduces a new internal Tk function, TkGetWindowSpecificData (added into tkInt stub table). It maintains a linked list of data blocks associated with each TkWindow, where each module (e.g. grid geometry manager) can store its own data related to a window; all data blocks in the chain are freed (Tcl_EventuallyFree'd with a specified freeProc) at the end of Tk_DestroyWindow, when _really_ no one should reference TkWindow any more.
Tk/generic/tkGrid.c is modified so it doesn't use hash table for tkwin-to-Gridder* mapping, but gets a Gridder with TkGetWindowSpecificData. The most visible part of #3020550, that is, recreating Gridders for tkwins that are then invalidated, goes away with this change automatically.
As of handling [grid] operations for dead windows, e.g. called by <Destroy> bindings, this patch doesn't generate errors on them, even on configuring/adding dead windows (or _into_ dead masters). It tries to behave as if destroyed windows were alive and about to die: e.g. if any window is gridded into a dead master, it is detached from its previous master and ends up unmapped. As Gridder* structure now remains valid when <Destroy> bindings are called, and freed only at the very end of Tk_DestroyWindow, I needed only a few `ifs' added to avoid breaking things with dead windows: we just have to prevent relinking slaves and masters if either of them is dead, and avoid using null tkwin from a dead Gridder in Tk window operations.
I chose this approach instead of complaining about dead windows because there is probably a lot of code expecting this behavior (_surely_ a lot of code expect [grid forget] to succeed). Without this patch, such code got an obscure reusing-dead-Gridders bug along with `succeeding' grid operations, but I decided it not to be a reason to _start_ generating errors in these cases.
I'm asking anyone interested to test this patch and review the code. If this patch is accepted, I'm going to modify tkPack.c and tkPlace.c so they use TkGetWindowSpecificData instead of tkwin-keyed hash table too.