Tk Source Code

View Ticket
Login
Ticket UUID: 1248241
Title: DllMain calls TkFinalize (arbitrary side effects)
Type: Bug Version: obsolete: 8.4.11
Submitter: mistachkin Created on: 2005-07-31 03:55:04
Subsystem: 74. Application Embedding Assigned To: hobbs
Priority: 3 Low Severity:
Status: Open Last Modified: 2011-01-31 04:42:09
Resolution: None Closed By:
    Closed on:
Description:
DllMain cannot call most of the Win32 API, see:

http://msdn.microsoft.com/library/en-us/dllproc/base/dllmain.asp?frame=true

This means that calling exit handlers that potentially
have arbitrary side effects could result in a variety
of nasty surprises.
User Comments: mistachkin added on 2005-08-09 15:53:13:
Logged In: YES 
user_id=113501

The only "real" way to "fix" this is for there to be a
public Tk_Finalize API that can be called prior to actually
unloading the DLL.  Hence, it cannot be fixed without a TIP.

Since the current solution migitates a number of other
important issues, it should stay the way it is.  Lowering
priority to move it off the radar.

davygrvy added on 2005-08-02 09:18:11:
Logged In: YES 
user_id=7549

BTW, there are two types of exits (controlled and
uncontrolled), yet there is only one type of exit handler. 
Do we need two types?

davygrvy added on 2005-08-02 08:48:12:
Logged In: YES 
user_id=7549

Joe,  Stop for a second and look at it the way it was
before.  Tcl's DllMain calls Tcl_Finalize.  If this is in
the middle of a teardown caused by ExitProcess(), Tk's
DllMain was already called for DLL_PROCESS_DETACH (LIFO
order) and has already been unloaded and if there are
handlers for any Tk stuff left in the finalize linkedlist,
they jump into now undefined space.

Yes, a DLL_PROCESS_DETACH condition can call all the Win32
API (but no thread syncing/start/stops).  Just during load,
the limit for what others haven't been loaded yet which
appears to be the limit describe for kernel32.

Jeff, Kevin just fixed the pipe channel driver reloading
during finalize, so his fix might be related to your users
experience.

"At best, Tcl
should be left to handle the destruction (close) of any
extension libraries."

Yes, it should but ExitProcess() reverses it all, hence the
need I found for making that patch to Tk.

hobbs added on 2005-08-02 07:03:49:
Logged In: YES 
user_id=72656

This was added by davygrvy in 12-2003.  The ChangeLog is
misleading about when this actually went into 8.4 though. 
In any case, here is the commit log:

        * generic/tkEvent.c:  Added three new functions:
TkCreateExitHandler,
        * generic/tkInt.h:    TkDeleteExitHandler, and
TkFinalize.  This adds
        * generic/tkMenu.c:   an insertion point so Tk's
exit handlers can be
        * generic/tkWindow.c: called on their own from
tk85.dll's DllMain
        * mac/tkMacButton.c:  for DLL_PROCESS_DETACH.  These
are private to
        * unix/tkUnixEvent.c: the binary and not exported. 
It is possible
        * win/tkWin32Dll.c:   the Windows OS can unload Tk
_prior_ to Tcl
        * win/tkWinEmbed.c:   under some conditions such as
ExitProcess().
        * win/tkWinMenu.c:    This avoids a dangling pointer
problem when Tcl
        * win/tkWinX.c:       does Tcl_Finalize after Tk has
been unloaded.
        * win/winMain.c:      DllMain's DLL_PROCESS_DETACH
now protected with
                              SEH as DeleteWindowsExitProc
is causing an
                              exception of its own under
some teardown
                              conditions.  AT&T assembly
syntax has not been
                              added for MinGW yet.  [Tcl
Patch 858493]

However, I do agree that this seems highly unorthodox,
possibly dangerous, and maybe unnecessary.  Checking what
TkFinalize does, it handles the calling of
TkCreateExitHandler callbacks.

The problem appears to be rooted in a lack of Tk_Finalize. 
I believe Dave was trying to solve the problem of the Tk dll
being unloaded before Tcl_Finalize is called - a valid
possibility in some embedded situations.  The problem is
that TkFinalize may get called when completely unnecessary.
 I just received a user report today saying:

"When the application calls exit() – before initialisation
of TCL – it strikes a memory access problem, see below. The
application was previously built with TCL/TL version 8.0 for
Windows 2000 and did not encounter this problem."

Although the TkFinalize is wrapped in SEH (with lots of
messy ifdefs, I might add), perhaps something is not handled
properly, as nothing else is obviously indicated on such a
simple case.  TkFinalize tries to make Tcl calls without
even knowing if Tcl was ever initialized.

The solution isn't necessarily trivial, and may be best
imposed by saying that Tcl_Finalize must be called before
unloading Tcl *or* any extension libraries.  At best, Tcl
should be left to handle the destruction (close) of any
extension libraries.

dgp added on 2005-08-01 21:46:15:
Logged In: YES 
user_id=80530


assigning something Windows-specific
to me is a mistake.