| Ticket UUID: | 0008d6a6b33e03ec9cb1259ad565f328015a9de7 | |||
| Title: | Mouse pointer position reported in non-existing window | |||
| Type: | Bug | Version: | ||
| Submitter: | erikleunissen | Created on: | 2020-09-23 21:21:16 | |
| Subsystem: | 65. Generic Window Operations | Assigned To: | fvogel | |
| Priority: | 5 Medium | Severity: | Minor | |
| Status: | Closed | Last Modified: | 2020-10-07 06:17:14 | |
| Resolution: | Fixed | Closed By: | fvogel | |
| Closed on: | 2020-10-07 06:17:14 | |||
| Description: |
The attached script shows how [winfo containing ...] reports the mouse pointer position in a non-existing window. Observed under Linux (not checked with other platforms). | |||
| User Comments: |
fvogel added on 2020-10-07 06:17:14:
So be it. bll added on 2020-10-06 20:15:51: We can go with (2). fvogel added on 2020-10-06 19:57:29: So which exactly would be your choice? (1) or (2)? bll added on 2020-10-05 13:12:59: 1) ... still exists, but most operations on it will behave in an undefined manner, and/or return arbitrary results. 2) ... still exists, but operations that involve it may return invalid results, or return an error. 3) ... still exists, but operations involving it may return invalid results, an error, or fail to be executed altogether." I rather like (1), as "undefined manner" covers all sorts of issues. (2) is fine. The issue I have with (3) is, as this statement gets more and more specific, then the list of failure types becomes longer and longer. Also, "failing to be executed" generally results in an error, so the statement becomes redundant. erikleunissen added on 2020-10-05 07:28:39: Still better: Widget commands do not return an error, they just cannot be executed/evaluated because the command doesn't exist anymore, yielding: invalid command name "$w". New proposal: "When the Destroy event is delivered to a widget, it is in a “half-dead” state: the widget still exists, but operations involving it may return invalid results, an error, or fail to be executed altogether." erikleunissen added on 2020-10-04 23:02:35: Uhmm, I meant: (2) "invalid results" describes what [winfo containing $w] returns, or maybe even [winfo exists] depending on your point of view. erikleunissen added on 2020-10-04 23:00:25: After more thinking I believe that the text that I proposed isn't appropriate after all. "Undefined behaviour" is too wild: it includes anything in principle, even memory corruption and segfaults, which is not applicable here. The term is often used for languages compiled to machine code, but that's not relevant here. Therefore, instead of: "When the Destroy event is delivered to a widget, it is in a “half-dead” state: the widget still exists, but most operations on it will fail." I propose: "When the Destroy event is delivered to a widget, it is in a “half-dead” state: the widget still exists, but operations that involve it may return invalid results, or return an error." Please note that: (1) the words "that involve" have been chosen instead of "on", because [winfo containing] doesn't operate on a widget, but does involve it. (2) "invalid results" describes what [winfo geometry $w] returns or maybe even [winfo exists] depending on your point of view. (3) "return an eror" applies to widgets command because those commands don't exist anymore. If it's not too late, may I request to reopen this issue and consider this new proposal? Maybe Brad is willing to review my text once more. fvogel added on 2020-10-04 16:11:34: Merged in core-8-6-branch and trunk. fvogel added on 2020-10-02 19:52:50: Committed: [2a2ff924]. bll added on 2020-10-02 04:43:37: Erik's replacement text is more precise. Behaving in an undefined manner also includes failures. fvogel added on 2020-10-01 21:19:25: Perhaps. I'd like to get opinion/confirmation from native english speakers here. erikleunissen added on 2020-10-01 20:48:36: But then I believe that the behaviour isn't so well documented. I'd say the operations in this ticket didn't fail: they all returned a value. But the results can't be relied upon, even of [winfo exists $w]. So the proper text should be something like that. "but most operations on it will return arbitrary/unreliable results". If other cases really exhibit failures (e.g. errors, or values that aren't even on the theoretical scale, ...), then an error return would be appropriate, or the text in the docs could be: "but most operations on it will behave in an undefined manner, and/or return arbitrary results". fvogel added on 2020-10-01 19:52:02: I understand the issue to be the inconsistency between what [winfo exists] returns and [winfo containing] (or other commands) return. Therefore another way to fix this would be to flip the result of [winfo exists] fro true to false. That could be [59cc3bb937]. But reading the manual (for the bind command), it says:
Destroy
A Destroy event is delivered to a window when it is destroyed.
When the Destroy event is delivered to a widget, it is in a “half-dead” state: the widget still exists, but most operations on it will fail.
Therefore I finally think that no fix at all is needed: the behavior is already documented properly. erikleunissen added on 2020-09-30 21:13:06: I think that the problem is to determine the pointer window for the case that (winPtr->flags & TK_ALREADY_DEAD) is true erikleunissen added on 2020-09-30 21:00:45: I'm afraid that the fix proposal in [0277bf7cd5] is incorrect. It makes [winfo containing] return "" even if the mouse pointer is over another Tk widget after window destruction. I added another script (exercise-2.tcl) to show that. It binds on a second window above the toplevel .a, and I added a leave execution trace on [destroy] to show what [winfo containing] ought return. When I execute the script with [0277bf7cd5], the output is: *** destroy event handler Window .a.f exists: 0 Window containing mouse pointer: "" *** trace handler on destroy .a.f Window .a.f exists: 0 Window containing mouse pointer: ".a" bll added on 2020-09-30 00:09:36: I would think it would be better to redesign the <destroy> handler so that it queues the bind script event (after 0 <bind-script>), finishes the destroy, then let Tk pick up the event and run the bind script. Of course, this same issue will exist in every event handler. fvogel added on 2020-09-29 21:21:12: The annoying thing is that while my fix solves the reported case, we have the same issue with, e.g. [winfo ismapped], and probably all other [winfo] commands. fvogel added on 2020-09-29 21:17:22: Fix proposal, see [0277bf7cd5]. bll added on 2020-09-27 23:16:59: I believe that this is a design flaw within Tk. As such, it's rather too difficult to clean up easily. The <destroy> event processing should be detached from the destroy processing by Tk, not by the user. erikleunissen added on 2020-09-27 13:45:56: Hi Brad, Your work-around is almost identical to the work-around that I already had in place before filing this bug report. Finding a work-around had already been solved, and I wasn't expecting that to be or become the purpose of a bug report. As it appears now, you have been spending your time on something that wasn't a problem, and therefore maybe a waste. I've got the impression that you may have misinterpreted the bug report. I've been re-reading the text of what I've written myself, but cannot find how that could have been the cause/seed that lead you on the side-track. I appreciate your inclination to help out. That said, it also distracts from the process of resolving the bug proper (if it is a bug), which still stands as far as I'm concerned. Erik. bll added on 2020-09-26 16:52:52: It also disconnects the call from the "bind <destroy>", so any destroy operations can be finished. It may be quite possible to use an "after 10", and I believe that works. bll added on 2020-09-26 16:22:25: What I suspect is happening is that from the X11 point of view, the window
hasn't been completely destroyed yet. And a lot of Tk's routines are thin
wrappers over the system calls, and Tk is just returning what X11 tells it.
Try:
proc handleWindowDestruction {W} {
puts "Window $W exists: [winfo exists $W]"
after 50 handleWindowDestructionPart2
}
proc handleWindowDestructionPart2 { } {
puts "Window containing mouse pointer: \"[getPointerWindow]\""
}
This allows the Tk event loop to be re-entered, so any other
outstanding Tk events will be processed, and gives a small amount
of time for the window to go away.
I just ran a test, and that appears to work, assuming I'm running the
test correctly.
erikleunissen added on 2020-09-25 07:28:43: Thanks for your work-around suggestion. (B.t.w. it doesn't work for my purpose, because it doesn't provide the new pointer window in case [winfo exists $W] returns false). As for my use case: I'm sorry, after all I'm not prepared to discuss that now. I respectfully let you decide about whether to pursue this issue. I primarily wanted to report something that seems wrong in itself, that's it. bll added on 2020-09-24 20:57:56: Your use case gives me information on whether this is a bug that should be
pursued or not. Putting resources into pursuing obscure bugs that are hardly
ever exercised is not worthwhile. Seeing your use case will tell me if this
is a worthwhile cause or not.
The <destroy> handler could just be written as:
if { [winfo exists $W] } {
set pw [getPointerWindow]
}
to work around the problem.
erikleunissen added on 2020-09-24 19:44:38: Unclear, OK. So what's the direction to clarity? You seem to be presenting the beginning of an explanation/justification for the result of [winfo containing]. But there are two commands in immediate succession. Can you reconcile the results of both [winfo exist] and [winfo containing], called in this order? Regarding my use case: I can imagine that code binding to <Destroy> is not mainstream. But whether I have a use case, seems another story to me. No big secret, but I just don't particularly care for this to go off topic ... bll added on 2020-09-24 16:06:17: Whether this is a bug or not remains unclear. Is the <destroy> handler called after or at the end of the destroy process. The window could have been still there at that time, I have no way to tell. Do you have a use case where this is a problem? erikleunissen added on 2020-09-24 07:35:02: I noticed that it makes a difference how you launch this script under Linux. If you "load" it from inside tkcon, the mouse pointer doesn't move over the yellow toplevel, which is essential. (Up to now I launched it from inside a terminal). I made another adjustment so that the reported output also shows when "loaded" from inside tkcon. The script exercise.tcl has once more been refreshed. erikleunissen added on 2020-09-24 07:13:01: Just checked with various Linux window managers and Windows 7: Linux with KDE/Plasma, IceWM, TWM and Windows 7 all show the reported output. Though: - for TWM I had to manually position the toplevels on the screen, and the mouse pointer over the yellow toplevel before window destruction. - for Windows I had to ensure that toplevel .a is topmost. To that end I added a [wm withdraw .] to the script exercise.tcl. I attached the revised script and removed the previous one. bll added on 2020-09-23 21:55:05: I cannot re-create. There may be some sort of timing related problem. erikleunissen added on 2020-09-23 21:34:04: The output that I'm getting is: Window .a exists: 0 Window containing mouse pointer: ".a" bll added on 2020-09-23 21:30:44: Can you explain exactly what you are seeing that seems wrong? What output are you getting? | |||
Attachments:
- exercise-2.tcl [download] added by erikleunissen on 2020-09-30 21:01:18. [details]
- exercise2.tcl [download] added by bll on 2020-09-26 16:53:10. [details]
- exercise.tcl [download] added by erikleunissen on 2020-09-24 07:35:12. [details]
