Tk Source Code

View Ticket
Login
Ticket UUID: 671330
Title: segfault when e.g. deiconifying destroyed window
Type: Bug Version: obsolete: 8.4.1
Submitter: hinteger Created on: 2003-01-20 19:59:11
Subsystem: 67. Unix Window Operations Assigned To: jenglish
Priority: 9 Immediate Severity:
Status: Closed Last Modified: 2003-01-29 03:44:15
Resolution: Fixed Closed By: jenglish
    Closed on: 2003-01-28 20:44:15
Description:
(at least that's what it looks to me)
I have a (quite long) script that does on toplevel on
the local screen and on on a remote screen. If there
are more top-levels on the remote screen (sometimes
requires user interaction) I do raise & deiconify the
toplevel on <Visibility> and <Unmap> event (just in
case the operator tries to close the window). In case
more toplevels are fighting for the topmost postion, I
do the raising with a (random) delay of ~2000 ms.
Sometimes I do get segfaults, when, so far I have
debugged the thing, I do deiconify the window. I thing
(but I'm not sure) this is when I binded an empty
script to the two events, destroyed the toplevel and
the random delay is still running and fireing the
deiconify/raise later. (Well, that might not be a good
way to code, but that's another story). It could as
well be, that X on on of the 2 screens terminates, I
just don't know.

You can check the thread "suggestion to track down
segfaults"
at Google
http://groups.google.com/groups?dq=&hl=en&lr=&ie=UTF-8&safe=off&threadm=3E2814AC.9070107%40inode.at&prev=/groups%3Fdq%3D%26num%3D30%26hl%3Den%26lr%3D%26ie%3DUTF-8%26group%3Dcomp.lang.tcl%26safe%3Doff%26start%3D30

Everything (code exaples and stack traces are listed there)

I've spent quit a lot hours trying to set up a x-liner
to reproduce the error, but I was'nt successful. Sorry.
If you want me to do some more debugging, give me a
note (and a list of gdb commands ;-) )

Gerhard
User Comments: jenglish added on 2003-01-29 03:44:15:
Logged In: YES 
user_id=68433

Added test case; committing patch.

jenglish added on 2003-01-25 06:02:22:

File Added - 40591: tk-deletion-epoch.patch

Logged In: YES 
user_id=68433

New patch uploaded.  Same as before, and in addition:
+ Removed now-unnecessary checks for otherValuePtr == NULL
in {Free|Dup}WindowInternalRep()
+ Free{Window|MM|Pixel}InternalRep () set objPtr->typePtr
to NULL.

Still need to add a test case.

hinteger added on 2003-01-23 15:52:03:
Logged In: YES 
user_id=124720

Ok, patch did survive in rough nature. No more segfaults.
Thanks again. I'm glad that this was fixed so quick.

hinteger added on 2003-01-22 21:44:31:
Logged In: YES 
user_id=124720

Wait, only tkWindow.c has been changed since release of
8.4.1 (V1.55), and this change fixes bug #630533, so I'd
better use the current CVS HEAD as basis, so it's ok to use
your patch. Will try that immediately.

hinteger added on 2003-01-22 19:53:16:
Logged In: YES 
user_id=124720

Great you found the problem, and also a script to reproduce it.
A patch against 8.4.1 would indeed be helpful. If you'd like
to clean up the code before, no problem, I have a workaround
for the bug (do a [winfo exists] check before), so I'm not
in a hurry.

Thanks

jenglish added on 2003-01-22 09:35:04:

File Added - 40299: tk-deletion-epoch.patch

Logged In: YES 
user_id=68433

Attached patch (to current CVS HEAD) should fix the problem.

Gerhard: could you test this?  (If you need a patch against
8.4.1 instead let me know)
Donal: could you review this?

Summary of fix: 
* Moved deletionEpoch from the  TkDisplay structure to the
TkMainInfo structure. 
* Reworked SetWindowFromAny and TkGetWindowFromObj so that
the otherValuePtr of a windowObj always points to a
WindowRep structure.
 * WindowRep now stores a pointer to the TkMainInfo
structure instead of a Tk_Window.

This causes one new regression -- test image-1.10 (related
to Bug #220819 "image command segfault (overwrite .)") has
been upgraded from an inscrutable error message to a
segfault.

I need to add a test case, and there's some now-dead code
that can be removed;  will do that tomorrow.

jenglish added on 2003-01-22 06:04:24:
Logged In: YES 
user_id=68433

WOO-HOO!  Figured it out!

The following script reliably reproduces the crash.  You'll
need two X servers.

set w [toplevel .t -screen $OTHERDISPLAY]
wm deiconify $w;# this caches the WindowRep
destroy .t
wm deiconify $w;# crash!

The problem is that the epoch guard is checking the
deletionEpoch of the main window's display, but destroying
.t only affects the deletionEpoch of $OTHERDISPLAY.

Proposed fix: move deletionEpoch from TkDisplay to
TkMainInfo.  (It makes more sense to put it in TkMainInfo
anyway, since that's where the nameTable lives).

jenglish added on 2003-01-22 03:27:58:
Logged In: YES 
user_id=68433

The uninitialized WindowRep should be fixed before the 8.4.2
release, but I don't think that's what's causing this bug.

From the stack trace in:
http://groups.google.com/groups?selm=bd4db85f.0301200822.6792dc5d%40posting.google.com

>#3  0x080668a7 in Tk_WmObjCmd (clientData=0x8230bf0,
interp=0x82053f0, objc=3,
>    objv=0x8205f3c) at
/usr/src/tk8.4.1/unix/tkUnixWm.c:1036
>
>1036            Tcl_AppendResult(interp, "window \"",
winPtr->pathName,
>(gdb) print winPtr->pathName
>$3 = 0x61616161 <Address 0x61616161 out of bounds>

So 'winPtr' _points to_ deleted storage (0x6161... is the
magic stomp value from tclCkalloc.c).  If  it had come from
an unititialized WindowRep I would expect winPtr _itself_ to
be 0x61616161.

Near as I can tell, either a cached WindowRep of a deleted
window is passing the epoch check in TkGetWindowFromObj, or
Tk_NameToWindow is returning a deleted window.  Can't figure
out how either of these could happen.

dkf added on 2003-01-21 20:12:08:
Logged In: YES 
user_id=79902

Ooer.  That's wrong.  Or at least the code posted by jenglish 
below is wrong.  It is a principle of Tcl_Objs that the type of 
an object is only modified by successful type-conversion 
operations; failures should not change the type.  (Though the 
higher-level ops that the type conversions are used in might 
fail for other reasons.)  The internal rep should always be 
matched with the typePtr field properly...

Probably easiest to duplicate the T_NTW and test, rewrite 
the failure path, and move (and rename) the jump target to 
refer to the place where the structure is populated.

Let me know if you want me to develop a patch along those 
lines...

jenglish added on 2003-01-21 05:35:37:
Logged In: YES 
user_id=68433

Something else to look at:  in generic/tkObj.c, r1.7,
(TkGetWindowFromObj), consider the case where objPtr has no
internal rep, and the string rep is not a valid window name.
 This will end up setting objPtr's internal rep to an
*uninitialized* WindowRep:

|    winPtr = (WindowRep *) objPtr->internalRep.otherValuePtr;
|    if (winPtr == NULL) {
|        winPtr = (WindowRep *) ckalloc(sizeof(WindowRep));
|        objPtr->internalRep.otherValuePtr = (VOID *) winPtr;
|        goto parseWindowString;
|    } else if (tkwin != winPtr->mainwin ||
|               dispPtr->deletionEpoch != winPtr->epoch) {
|    parseWindowString:
|        foundWindow = Tk_NameToWindow(interp,
|                Tcl_GetStringFromObj(objPtr, NULL), tkwin);
|        if (foundWindow == NULL) {
|            return TCL_ERROR;
|        }
|        winPtr->tkwin = foundWindow;
|        winPtr->mainwin = tkwin;
|        winPtr->epoch = dispPtr->deletionEpoch;
|    }

This is likely part of the problem, although I can't figure
out how this (by itself) would lead to the observed crash
and there's probably something else going on.

hinteger added on 2003-01-21 05:29:30:
Logged In: YES 
user_id=124720

1) I did have the segfaults also berfore I compiled with
TCL_MEM_DEBUG, cannot say if more often or not. By inserting
"puts" I found out, that the error was when I entered the
event loop (via a after/vwait). Then I started using a
TCL_MEM_DEBUG enabled wish.

2) don't want to remove the catch, because real-life
application. Sometimes people log (terminate X), so not
catching this might be a bad thing.

3) did that already,but forgot to post : error is when
deiconifying.

jenglish added on 2003-01-21 04:54:16:
Logged In: YES 
user_id=68433

Some more things to try (and thanks for your patience):
Does this still happen (or happen less frequently) when
running *without* TCL_MEM_DEBUG?

With TCL_MEM_DEBUG enabled, does this still happen if you
remove the 'catch' statements from the 'after' script in
proc raise_win:

>         catch {wm deiconify $w}
>         catch {raise $w}

Lastly, if you're willing to fight with the debugger again
:-), can you tell if the crash is happening in the call to
[wm deiconify] or the call to [wm raise]?  (printing
objv[1]->bytes in the Tk_WmObjCmd stack frame should help
you tell.)

An essential clue: from the stack backtrace, at
unix/tkUnixWm.c line 1036, 'winPtr' points into
already-freed storage.  The value of winPtr comes from a
previous call to TkGetWindowFromObj.

I am not sure _how_ this is happening, but it looks like
TkGetWindowFromObj() is to blame.

Attachments: