Tk Source Code

Artifact [e1821fb9]
Login

Artifact e1821fb9c5fc4cae9e0aaae1c1052070e07588fb:

Attachment "winTkFinalizeBug-t.c" to ticket [1189899f] added by jfg118 2005-04-26 06:59:25.
/* 

 cl /Zi /I<...>/include/tcl8.4 winTkFinalizeBug-t.c /link /LIBPATH:<...>/lib tcl84tg.lib tk84tg.lib user32.lib

 This example causes a crash during the TkFinalize routine becase the main 
 thread is exiting the application, and the Tk exit handler "assumes" that
 the exit handlers are valid for all threads.  The DeleteWindowsExitProc proc
 sends the thread specific data pointer as ClientData [tkWindow.c:363].
 Inside of the DeleteWindowsExitProc function a call to Tcl_DestroyWindow
 [tkWindow.c:2729,2740] and TkCloseDisplay [tkWindow.c:1601] which calls 
 TkpCloseDisplay [tkWindow.c:281] all LOOKUP the thread specific data instead
 using the supplied ClientData in the exit handler. Since a different thread 
 is calling "TkFinalize" the lists become corrupted.

*/

#define TCL_THREADS

#include <tcl.h>
#include <windows.h>

static Tcl_ThreadCreateType ThreadMain(ClientData clientData)
{    
  Tcl_Interp *interp = 0;

  interp = Tcl_CreateInterp();

  if (Tcl_Init(interp) != TCL_OK)
    MessageBox(NULL, "Failed to initialize interp", "Error", MB_ICONERROR);

  if (Tcl_Eval(interp, "package require Tk") != TCL_OK)
    MessageBox(NULL, "Failed to package require Tk", "Error", MB_ICONERROR);
  
  TCL_THREAD_CREATE_RETURN;
}

int APIENTRY WinMain(HINSTANCE hInstance,
		     HINSTANCE hPrevInstance,
		     LPSTR lpszCmdLine,
		     int nCmdShow)
{
  Tcl_ThreadId tid;
  char argv0[MAX_PATH];

  GetModuleFileName(NULL, argv0, sizeof(argv0));

  Tcl_FindExecutable(argv0);
  Tcl_CreateInterp();

  Tcl_CreateThread(&tid, ThreadMain, (ClientData)0, 
                   TCL_THREAD_STACK_DEFAULT, TCL_THREAD_NOFLAGS);

  MessageBox(NULL, "Press Ok to Tcl_Finalize", "Debug", MB_ICONINFORMATION);

  Tcl_Finalize();

  return 0;
}