Skip to content
Tcl Source Code Artifact [6a51d78932]
Login

Artifact [6a51d78932]

Artifact 6a51d7893242923730f27f52dc3b73522e51fce9:

Ticket change [6a51d78932] - Ticket [219355ffffffffff|219355ffff] <i>fix proposal for TR#5312 (part 2)</i> status still Open with 4 other changes by dgp on 2002-04-07 07:09:59.
D 2002-04-07T07:09:59
J icomment Logged\sIn:\sYES\s\nuser_id=80530\n\n>From\s"Egon\sPasztor"\s<[email protected]>\n\n\n\sHi,\sI'm\susing\sTcl\s8.3.1\son\sa\sWindows\s2000\sDual-Pentium\sIII\sPC;\n\s\s\s\s\sexploring\swith\sMicrosoft\sVisual\sC++\s6.0.\n\n\sThis\sis\sregarding\sbug\sTR#5312\sas\slisted\son\sthe\sscriptics\ntcl\sbug\s\ndatabase\n\s\s\s\s--\s"Application\sError"\swhen\sclosing\stclsh83.exe\n\s\s\s\s\s\s\s\s(tcl8.3.1\swith\sthreads)\sTcl\sCore"\n\n\s\s\s\s\s>>\sFrom\swww.scriptics.com,\sbug\sdatabase,\sTR#5312:\n\s\s\s\s\s>\n>\sObservedBehavior:\sTclsh83.exe\sthrows\san\s"Application\sError"\n\s\s\s\s\s>\swhen\sterminated\swith\s"Close"\sfrom\sthe\swindow's\smenu.\n\s\s\s\s\s>\sIt\sdoes\sthat\sONLY\swith\sactivated\sthread\ssupport.\n\n\n\sI\sran\sinto\s..\ssortof\sa\smore\ssevere\sversion\sof\sthis\sbug,\n\s\s\s\s\sand\sended\sup\stracking\sit\shere.\s\s\sI\snotice\sadditional\n\s\s\s\s\sinformation\shas\sbeen\sposted:\n\n\n\s\s\s\s\s>>\sFrom\swww.scriptics.com,\sbug\sdatabase,\sTR#5312:\n\s\s\s\s\s>\n>\sIt\sthrows\sthe\sexception\son\sline\s151\sof\stclWinNotify.c\sin\n\s\s\s\s\s>\sTcl_FinalizeNotifier().\s\sIt\slooks\slike\sthe\snotifier\n\s\s\s\s\s>\sdata\scoming\sin\sto\sthe\sfunction\sis\sinvalid.\n\n\n\sInvalid\sindeed\s--\son\smy\scomputer,\szero,\suninitialized:\n\sI\sthink\sthe\sproblem\sis\sthat\sTclFinalizeNotifier\s(tclNotify.c)\n\s\s\sis\sbeing\scalled\sfrom\sa\sthread\sin\swhich\sTclInitNotifier\nwas\snever\s\ncalled.\n\n\s\s\s\s\s>\sThis\sneeds\sa\smore\sthorough\sinvestigation.\n\n\s\s....\sread\son\s...\s\s:)\n\n\n\n\s\n===================================================================\n\s\s====================\s\s\s\s\s\s\s\s\s1\s\s\s\s\s\s\s\s\s\n===========================\n\s\n===================================================================\n\n\n\sI\ssee\sthat\sTcl_FinalizeNotifier\s(tclWinNotify.c)\n\s\s\s\sis\sactually\scalled\sby\sTclFinalizeNotifier\s(tclNotify.c),\n\s\s\s\swhich\scontains\sthe\slines\n\n\n\s\s\s\s\s>>\sThreadSpecificData\s*tsdPtr\s=\sTCL_TSD_INIT(&dataKey);\n\s\s\s\s\s>>\s....\n\s\s\s\s\s>>\sTcl_FinalizeNotifier(tsdPtr->clientData);\n\n\n\sThis\smacro,\s"TCL_TSD_INIT",\n\s\s\s\sgets\sa\spointer\sto\sthread-local-storage\sif\ssome\shas\sbeen\nallocated,\n\s\s\s\sor\sallocates\ssome\snew\sif\snone\shas.\n\n\sSo\s**\sif\sTclFinalizeNotifier\swere\scalled\n\s\s\s\s\s\s\sfrom\sa\sthread\sthat\shad\sno\sthread-local-storage\nconstructed\s**\n\n\s\s\sthen\sTCL_TSD_INIT\swould\sallocate\sfresh\sspace,\sfilled\swith\nzeros;\n\s\s\s\s\s\sand\spass\sit\sinto\sTcl_FinalizeNotifier,\scausing\sthe\scrash.\n\n\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s--\s--\s--\s--\s--\s--\s--\s--\n\n\sThis\sis\swhat\sI\sthink\sis\shappening\s--\n\sI\stried\sto\strace\sthe\santics\sof\sthe\svarious\sthreads\n\s\s\sstarted\sand\sstopped\swhile\sexecuting\sthe\ssteps\sneeded\n\s\s\sto\sreproduce\sthe\scrash\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s--\s(ie,\sstarting\stclsh83,\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sthen\sselecting\s"CLOSE"\sfrom\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sthe\sconsole's\spull-down\smenu)\s--\n\n\s\s\sthe\s"CLOSE"\ssignal\sseems\sto\scause\sTcl_Finalize\n(tclEvent.c)\sto\sbe\s\ncalled\n\s\s\s\s\s(from\sDllMain\s(tclWin32Dll.c))\sfrom\swithin\sa\snew,\nvirgin\sthread\s--\n\s\s\s\s\sone\swhich\shas\snever\syet\scalled\sany\sTcl\sfunction\sat\sall.\n\n\n\sTcl_Finalize\scalls\sTcl_FinalizeThread\s(tclEvent.c),\n\s\s\s\swhich\scalls\sTclFinalizeNotifier.\n\n\n\n\s\n===================================================================\n\s\s====================\s\s\s\s\s\s\s\s\s2\s\s\s\s\s\s\s\s\s\n===========================\n\s\n===================================================================\n\n\n\n\s\sThat\sis,\s*Suppose*\sthat\sTcl_Finalize\swere\scalled\son\ssuch\sa\nvirgin\s\nthread:\n\n\s\s\s\sA:\s\sI\ssee\sthat\sone\sof\sthe\sfirst\slines\sin\sTcl_Finalize\n(tclEvent.c)\s\nis\n\n\s\s\s\s\s\s\s\s\s\s>>\stsdPtr\s=\sTCL_TSD_INIT(&dataKey);\n\n\s\s\s\s\s\s\s\sif\sthis\swere\sa\s"virgin"\sthread,\sthis\sline\swould\s*create*\n\s\s\s\s\s\s\s\s\s\snew\sthread-local\sstorage,\sassociated\swith\sthe\n"dataKey"\n\s\s\s\s\s\s\s\s\s\sin\sthat\sfile\s(tclEvent.c).\n\n\n\n\s\s\s\sB:\s\sThe\snext\sblock\sof\scode\sis\sprotected\sby\san\s"if"\sstatement\n\n\s\s\s\s\s\s\s\s\s\s>>\sif\s(subsystemsInitialized\s!=\s0)\s{\n\n\s\s\s\s\s\s\s\spresumably\sto\skeep\sTcl_Finalize\sfrom\sdoing\sanything\nif\sno\sTcl\n\s\s\s\s\s\s\s\sfunctions\shave\sever\srun.\s\sBut\sI'm\ssupposing\sthat\swe\nare\sat\sthe\n\s\s\s\s\s\s\s\send\sof\sa\ssuccessful\sTcl\sprogram;\sTcl\sfunctions\n*have*\sbeen\s\ncalled,\n\s\s\s\s\s\s\s\sand\sall\ssubsystems\sinitialized;\sjust\snothing\sfrom\n*this*\sthread.\n\n\s\s\s\s\s\s\s\sSo\scontrol\sproceeds\sinto\sthe\smain\sbody\sof\nTcl_Finalize\s...\n\n\n\n\s\s\s\sC:\s\s...\sdown\sto\sthe\scall\sto\sTcl_FinalizeThread,\sabout\nhalfway\n\s\s\s\s\s\s\s\sthrough\sthe\sbody\sof\sTcl_Finalize.\n\n\s\s\s\s\s\s\s\sTcl_FinalizeThread\s(tclEvent.c)\sbegins\swith\n\n\s\s\s\s\s\s\s\s\s\s>>\sThreadSpecificData\s*tsdPtr\s=\n\s\s\s\s\s\s\s\s\s\s>>\s(ThreadSpecificData\n*)TclThreadDataKeyGet(&dataKey);\n\s\s\s\s\s\s\s\s\s\s>>\n>>\sif\s(tsdPtr\s!=\sNULL)\s{\s....\n\n\s\s\s\s\s\s\s\swhich\sgets\sa\spointer\sto\sthread-local-storage\sin\sa\nmanner\sthat\n\s\s\s\s\s\s\s\s\s\sdoesn't\screate\sthe\sstorage\sif\sit's\snot\salready\npresent\s--\n\s\s\s\s\s\s\s\s\s\sand\schecks\sit\sfor\sNULL.\n\n\s\s\s\s\s\s\s\sHowever,\sthread-local-storage\sfor\sthis\sthread\s*was*\ncreated\n\s\s\s\s\s\s\s\s\s\sin\sTcl_Finalize\s(see\spoint\sA,\sabove)\s--\sand\swith\nthe\ssame\n\s\s\s\s\s\s\s\s\s\sdataKey,\ssince\scontrol\sis\sstill\sin\s(tclEvent.c).\n\n\s\s\s\s\s\s\s\sSo\scontrol\sproceeds\spast\sthe\sif-statement's\sguard\ninto\sthe\n\s\s\s\s\s\s\s\s\s\smain\sbody\sof\sTcl_FinalizeThread\s...\n\n\n\s\s\s\sD:\s\sMost\sof\swhat\sfollows\sin\sTcl_FinalizeThread\sis\sin\sa\nloop\sthat\n\s\s\s\s\s\s\s\s\s\swill\sbe\sskipped\sif\s"tsdPtr->firstExitPtr"\sis\s0.\n\n\s\s\s\s\s\s\s\sSince\sthe\sthread-local-storage\swas\sjust\sallocated,\n\s\s\s\s\s\s\s\s\s\sit\sis\slikely\sto\sbe\s0,\n\s\s\s\s\s\s\s\s\s\sso\scontrol\swill\smake\sit\sdown\sto\n\s\s\s\s\s\s\s\s\s\sthe\scall\sto\s"TclFinalizeNotifier".\n\n\n\s\s\s\sE:\s\sWhich\sbrings\sus\sback\sto\sthe\sscenario\sposited\sabove\s--\n\s\s\s\s\s\s\s\sTclFinalizeNotifier\s(tclNotify.c)\n\s\s\s\s\s\s\s\sbegins\swith\n\n\s\s\s\s\s\s\s\s\s\s>>\sThreadSpecificData\s*tsdPtr\s=\nTCL_TSD_INIT(&dataKey);\n\n\s\s\s\s\s\s\s\swhich\sallocates\sanother\snew\sblock\sof\nthread-local-storage\n\s\s\s\s\s\s\s\s\s\s(since\sthis\sis\sa\sdifferent\s"dataKey",\sin\ntclNotify.c\snow)\n\s\s\s\s\s\s\s\sand\sproceeds\sto\scall\sTcl_FinalizeNotifier\n\s\s\s\s\s\s\s\swith\sits\suninitialized\scontents,\scausing\sthe\scrash.\n\n\s)\n\n\n\n\s\n===================================================================\n\s\s====================\s\s\s\s\s\s\s\s\s3\s\s\s\s\s\s\s\s\s\n===========================\n\s\n===================================================================\n\n\n\s\sSo\sit\slooks\slike\sTcl_Finalize\swould\scrash\sif\scalled\sfrom\sa\nvirgin\s\nthread.\n\s\sI\sthink\sthis\sis\swhat\sactually\shappens:\n\n\n\s\sAccording\sto\sthe\sMicrosoft\sdocumentation,\sa\sDLL's\sDllMain\nfunction\n\s\s\s\sis\scalled\soff\sand\son\sfor\sone\sof\sfour\sreasons,\n\s\s\s\s\s\s"DLL_PROCESS_ATTACH",\s\s\s"DLL_PROCESS_DETACH"\n\s\s\s\s\s\s"DLL_THREAD_ATTACH",\s&\s"DLL_THREAD_DETACH"\n\n\s\sSo\sI\sput\scalls\sto\smy\sown\s"printf-like"\sexternal\nmessage-logging\s\nfunction\n\s\s\s\sinside\s"DllMain"\s(tclWin32Dll.c)\sto\swatch\sthe\sthreads\nstartup\n\s\s\s\sand\sshutdown.\n\n\n\s\s\s\s\s\s\s\s\s\s>>\sFrom\swww.scriptics.com,\sbug\sdatabase,\sTR#5312:\n\s\s\s\s\s\s\s\s\s\s>\n>\sSo\sit\slooks\slike\sthe\sOS\smight\sbe\sterminating\n\s\s\s\s\s\s\s\s\s\s>\srunning\sthreads\sfor\sus\sin\sreverse\sorder.\n\n\n\s\sWell..\snot\s"reverse\sorder",\sexactly.\n\s\sIt\sseems\sthat\swhen\sthe\suser\sselects\s"CLOSE"\sfrom\sthe\nconsole's\smenu,\n\s\s\s\sWindows\sactually\sspawns\sa\s*brand\snew\sthread*\sfrom\swhich\nto\scall\n\s\s\s\s"ExitProcess"\s\s--\scalling\sDllMain\s(DLL_PROCESS_DETACH))\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s--\scalling\sTcl_Finalize\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s--\sall\sfrom\swithin\sthis\svirgin\sthread.\n\n\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s--\s--\s--\s--\s--\s--\s--\s--\n\n\s\sThat\sis,\sa\sdump\sof\sall\sthe\scalls\sto\sDllMain\sduring\sa\srun\nof\stclsh83\n\s\slooks\slike\sthis:\n\n\s\s\s<1>:\sDllMain\s(DLL_PROCESS_ATTACH)\sis\scalled\son\sthread\sA\n\s\s\s\s\s\s\s\s\s(The\smain\sthread\sstarting\sup;\s"main"\s(tclAppInit.c)\nis\scalled\s\nnext\n\s\s\s\s\s\s\s\s\s\s..\sthis\scalls\sTcl_Main,\swhich\sinits\slots\sof\sthings\n..\sand\s\nsoon:\n\n\s\s\s<2>:\sDllMain\s(DLL_THREAD_ATTACH)\sis\scalled\son\sthread\sB\n\s\s\s\s\s\s\s\s\s(Evidently\srunning\s"ConsoleReaderThread"\sin\ntclWinConsole.c;\n\s\s\s\s\s\s\s\s\s\sa\sthread\s"managing"\sSTDIN\sin\ssome\sway;\sspawned\sby\n\s\s\s\s\s\s\s\s\s\sCreateThread\scalled\sin\sTclWinOpenConsoleChannel\s\n(tclWinConsole.c))\n\n\s\s\s<3>:\sDllMain\s(DLL_THREAD_ATTACH)\sis\scalled\son\sthread\sC\n\s\s\s\s\s\s\s\s\s(Same\sthing,\sthis\stime\s"ConsoleWriterThread",\nmanaging\sSTDOUT)\n\s\s\s\s\s\s\s\s\s...\snow\sthe\sconsole\sappears\son\sscreen\sand\scursor's\nblinking\s...\n\s\s\s\s\s\s\s\s\s...\sand\safter\sthe\sfirst\scommand\sis\sinterpreted,\swe\nget\sone\s\nmore:\n\n\s\s\s<4>:\sDllMain\s(DLL_THREAD_ATTACH)\sis\scalled\son\sthread\sD\n\s\s\s\s\s\s\s\s\s...\sto\srepresent\sSTDERR.\n\n\s\s\s\s\s\s\s\s\s*Now*\s..\sTake\syour\smouse\sto\sthe\sconsole\smenu\sand\nselect\sCLOSE\s\n...\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sand\swe\sget\s...\n\n\s\s\s<5>:\sDllMain\s(DLL_THREAD_ATTACH)\sis\scalled\son\sthread\sE\n\s\s\s\s\s\s\s\s\s..\s\s??\s??\s??\sEvidently\sstarted\sby\sWindows\s??\s??\s??\s..\n\s\s\s\s\s\s\s\s\sfollowed\sby\n\n\s\s\s<6>:\sDllMain\s(DLL_PROCESS_DETACH)\sis\scalled\son\sthread\sE\n\s\s\s\s\s\s\s\s\s--\swhich\scalls\sTcl_Finalize\son\sthread\sE,\n\s\s\s\s\s\s\s\s\s\s\s\scausing\sthe\scrash\sas\straced\sabove,\ssince\sthread\sE\n\s\s\s\s\s\s\s\s\s\s\s\shas\sno\sTcl\sinitializations.\n\n\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s--\s--\s--\s--\s--\s--\s--\s--\n\n\s\sIf\sinstead,\safter\sEvent\s4,\sinstead\sof\sselecting\sCLOSE\syou\n\s\s\s\ssimply\stype\s"exit",\sand\sexecute\sit,\n\s\s\s\sit\sgets\sinterpreted\s(in\sthread\sA\sof\scourse)\n\s\s\s\scalling\sTcl_Exit,\n\s\s\s\s\s\s\scalling\sTcl_Finalize\s(in\sthread\sA,\sthe\sTcl\sthread)\n\s\s\s\s\s\s\sand\severything\sis\sfine.\n\n\s\s\s\sTcl_Exit\snext\scalls\sTclpExit,\swhich\sis\s#defined\sto\sthe\n\s\s\s\s\s\s\snative\sOS\s"exit"\s(tclWinPort.h)\s--\n\s\s\s\s\s\s\s\s\s(all\sthis\sstill\son\sthread\sA)\n\s\s\s\s\s\s\s--\swhich\sfinally\skills\sthe\sprocess.\n\n\s\sSo\sin\sthis\scase,\sinstead\sof\sEvents\s5\sand\s6,\n\s\s\s\sthere\sis\ssimply\n\n\s\s\s\s<5>:\sDllMain\s(DLL_PROCESS_DETACH)\sis\scalled\son\sthread\sA\n\n\s\s\s\s..\s(which,\sof\scourse,\scalls\sTcl_Finalize\sa\ssecond\stime\n\s\s\s\s\s\s\s\s(still\son\sthread\sA)\s..\sbut\sTcl_Finalize\schecks\sfor\nthis.)\n\n\n\s\n===================================================================\n\n\n\s\sWhen\sI\sfirst\ssaw\sthis,\sI\swas\sinitially\ssurprised\sthat\nthere\sweren't\n\s\s\s\sany\s"DLL_THREAD_DETACH"'es.\s\sOddly,\sI'd\sthought\sthat\nthey\swould\n\s\s\s\scome\sin\spredictable\spairs.\s\s\sNope.\n\n\s\sI\sfound\sconfirmation\sof\sthis\sin\sMicrosoft\sDocs\son\s"DllMain"\n\n\s\s\s>>\nhttp://msdn.microsoft.com/library/psdk/winbase/dll_8asu.htm\n\nwhich\scontains\sthe\stext:\n\n\n\s\s\s\s\s\s>\sWhen\sa\sDLL\sis\sunloaded\sfrom\sa\sprocess\sas\sa\sresult\sof\n\s\s\s\s\s\s>\s...\stermination\sof\sthe\sprocess\s...\n\s\s\s\s\s\s>\sthe\ssystem\sdoes\snot\scall\sthe\sDLL's\sentry-point\sfunction\n\s\s\s\s\s\s>\swith\sthe\sDLL_THREAD_DETACH\svalue\sfor\sthe\sindividual\nthreads\n\s\s\s\s\s\s>\sof\sthe\sprocess.\sThe\sDLL\sis\sonly\ssent\sa\nDLL_PROCESS_DETACH\n\s\s\s\s\s\s>\snotification.\n\n\n\s\n===================================================================\n\n\n\s\sI\swas\sless\ssuccessful\sin\sfinding\sofficial\sexplanatation\sof\nthis\n\s\s\s\srandom\sextra\s"thread\sE"\sstarted\swhen\sthe\suser\sselects\n"CLOSE"\n\s\s\s\sin\sthe\sconsole's\smenu.\n\n\s\sThe\sbest\sdocumentation\sI\scould\sfind\swas\snoting\sthat\sthe\nMicrosoft\n\s\s\s\sDocs\sfor\sWin32's\s"SetConsoleCtrlHandler"\s--\sthe\sAPI\sfor\nsetting\n\s\s\s\sa\sCLOSE\sor\sControl-C\shandler\s--\n\n\s\s\s\s>>\nhttp://msdn.microsoft.com/library/psdk/winbase/conchar_599u.htm\n\n--\scontinually\srefer\sto\sthese\sthings\s--\sControl-C,\nControl-BREAK,\n\s\s\s\s\sperhaps\sCLOSE\stoo\s--\sas\s"signals",\sreminiscent\sof\sUnix\nsignals,\n\s\s\s\s\sand\sthe\sMicrosoft\sDoc\sfor\s"signals"\s--\n\n\s\s\s\s>>\nhttp://msdn.microsoft.com/library/devprods/vs6/visualc/vccore/_crt_signa\nl.htm\n\n\s\s--\scontains\sthe\s..\summ..\sominous\swarning:\n\n\s\s\s\s\s\s>\sWhen\sa\sCTRL+C\sinterrupt\soccurs,\sWin32\soperating\ssystems\n\s\s\s\s\s\s>\sgenerate\sa\snew\sthread\sto\sspecifically\shandle\sthat\ninterrupt.\n\s\s\s\s\s\s>\sThis\scan\scause\sa\ssingle-thread\sapplication\ssuch\sas\sUNIX,\n\s\s\s\s\s\s>\sto\sbecome\smultithreaded,\sresulting\sin\sunexpected\nbehavior.\n\n\n\s\s(This\smakes\sme\swonder\sabout\sbug\sTR#4132\sas\swell\s--\n("Control-BREAK"\n\s\s\scausing\sa\shang\sin\stclsh)\s--\sdeadlock\sfrom\sunexpected\nmultithreading?)\n\n\n\s\sBut\sthe\sreality\sof\sthis\s"thread\sE"\screated\sjust\sto\shandle\nthe\sCLOSE\n\s\sis\sconfirmed:\s\sIf\syou\sactually\sregister\sa\s"CLOSE\shandler"\nfunction,\n\s\susing\s"SetConsoleCtrlHandler"\s--\sit\sis\scalled\son\s"thread\sE".\n\n\n\n\n\s\n===================================================================\n\s\s====================\s\s\s\s\s\s\s\s\s4\s\s\s\s\s\s\s\s\s\n===========================\n\s\n===================================================================\n\n\n\s\sOn\sthe\sscriptics\spage,\sit\sis\ssuggested:\n\n\n\s\s\s\s\s\s\s\s\s\s>>\sFrom\swww.scriptics.com,\sbug\sdatabase,\sTR#5312:\n\s\s\s\s\s\s\s\s\s\s>\n>\sI'll\sassume\sright\snow\sthat\stclAppInit.c\s(the\sapp,\n\s\s\s\s\s\s\s\s\s\s>\snot\sthe\sdll)\smight\sneed\sa\scontrol-c\shandler\nprovided\sby\n\s\s\s\s\s\s\s\s\s\s>\sWin32's\sSetConsoleCtrlHandler()\sfor\sa\scontrolled\nshut-down\n\n\s\sand\sof\scourse,\sthe\sproblem\s*can*\sbe\sovercome\sthis\sway.\n\n\s\sThe\ssimplest\sthing\sI\scan\sthink\sof\sis\n\s\s\s\s..\s"well,\sif\sTcl_Finalize\sis\sgoing\sto\sget\scalled\son\nthread\sE,\n\s\s\s\s\s\s\s\slet's\smake\ssure\sTcl\sis\sinitialized\son\sit."\n\n\s\sBy\sadding\sthe\sfunction:\n\n\n\s\s\s\s>>\sBOOL\sWINAPI\sHandlerRoutine\s(DWORD\sdwCtrlType)\n\s\s\s\s>>\s{\n\s\s\s\s>>\sTcl_DeleteInterp(Tcl_CreateInterp());\n\s\s\s\s>>\sreturn\sFALSE;\n\s\s\s\s>>\s}\n\n\s\sto\s(tclAppInit.c),\salong\swith\sa\scall\sto\n\n\s\s\s\s>>\sSetConsoleCtrlHandler(HandlerRoutine,\sTRUE);\n\n\s\sinside\s"main",\sthe\sproblem\sgoes\saway,\n\s\sand\syou\scan\sCLOSE\stclsh83\swith\sease.\n\n\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sBUT\n\n\n\s\s(and\sthis\sbrings\sus\sto\sthe\spoint\sof\smy\slong\semail)\n\n\s\sI'd\slike\sto\ssuggest\sa\sdeeper\ssolution\s--\n\s\sone\sthat\swould\salso\sfix\s*\smy\s*\sproblem.\n\n\n\s\n===================================================================\n\s\n===================================================================\n\n\n\s\sBecause\sthe\s"fundamental"\sproblem\sseems\sto\sbe\sthat\n\s\s\s\sit\slooks\slike\sTcl_Finalize\swasn't\sdesigned\sto\sbe\scalled\n\s\s\s\sfrom\sa\snon-tcl\sthread;\n\s\s\s\s\s\sAnd\sI\sran\sinto\sthis\sbug\sfrom\sa\scompletely\sdifferent\sangle.\n\n\n\s\s*\sMe\s*,\sI'm\swriting\sa\sC++\sprogram,\n\s\s\s\s\s\swith\sa\sTcl/Tk\sinterpreter\sembedded\sin\sit.\n\n\n\s\sMine\sis\sa\smultithreaded\sWin32\sapplication,\snot\sconsole\sat\sall,\n\s\s\s\s\sbut\smost\sof\sthe\stime\sit\sworks\sinvisibly,\s(no\swindows\sat\nall)\n\s\s\s\s\sand\sonly\swith\snative\sWin32\scalls.\n\n\s\s\s\s\sOnly\ssome\sof\sthe\stime\s(ie,\snot\severy\stime\sthe\sprogram\sruns)\n\s\s\s\s\s\s\sdo\sI\sneed\sto\spop\sup\sa\sGUI\s---\sfor\swhich\sI\sinit\sand\nuse\sTk.\n\n\s\sSo\sbasically:\n\n\s\s\s\sMost\sof\sthe\stime:\n\n\s\s\s\s\s\s\s\sMy\sprogram\sstarts\s(Thread\sA)\n\s\s\s\s\s\s\s\s\s..\sruns\sfor\sa\swhile\s..\n\s\s\s\s\s\s\s\s\s\s\s\sand\sthen\sexits\sand\s(Thread\sA)\sdies.\n\n\s\s\s\sHowever,\ssometimes:\n\n\s\s\s\s\s\s\s\sMy\sprogram\sstarts\s(Thread\sA)\n\s\s\s\s\s\s\s\s\s..\sruns\sfor\sa\swhile\s..\n\s\s\s\s\s\s\s\s\s..\ssomething\shappens\srequiring\sa\sGUI\n\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s..\sso\sI\sspawn\s(Thread\sB),\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\swhich\sruns\scode\scut-and-pasted\sfrom\s(tkMain.c)\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sto\sstart\sa\stk-based\sGUI\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s..\swhich\sruns\sfor\sa\swhile\s..\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s..\sand\sthen\sis\sstopped,\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sinterpreter\sdeleted,\sthread\sfinalized,\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s(Thread\sB)\sdies.\n\n\s\s\s\s\s\s\s\s\s..\sand\sthen\ssome\stime\slater,\n\s\s\s\s\s\s\s\s\s\s\s\smy\sprogram\sexits\sand\s(Thread\sA)\sdies.\n\n\n\s\sBut\severy\stime\smy\sprogram\suses\sTk,\swhen\smy\sprogram\nsubsequently\n\s\s\s\s\sexits,\sI\sget\sthe\ssame\sTcl_FinalizeNotifier\scrash\s--\n\s\s\s\s\sthe\scause\sbeing\sthe\ssame\sthing:\n\n\s\s\s\s\s\s\s\sWhen\sthread\sA\sexits,\n\s\s\s\s\s\s\s\s\s\s\sTcl/Tk\sDllMain's\sare\scalled\swith\sDLL_PROCESS_DETACH\n\s\s\s\s\s\s\s\s\s\s\s\s\swhich\scalls\sTcl_Finalize\s----\sfrom\sthread\sA\n\n\s\s\s\s\s\s\s\s\sBut\smy\smain\sthread\s--\sthread\sA\s--\sis\snot\sa\stcl\sthread,\n\s\s\s\s\s\s\s\s\s\s\s\s\sand\sso\snone\sof\sits\s"Notifiers"\shave\sbeen\ninitialized.\n\n\n\n\n\s\s\s\s\s\s\s\s###############################################\n\s\s\s\s\s\s\s\s##\s\sHey,\shas\sthis\sbug\sbeen\sreported\sbefore?\s\s##\n\s\s\s\s\s\s\s\s###############################################\n\n\s\sI've\sseen\soodles\sof\sexamples\sof\sembedding\sTk\n\s\s\s\s\s\s\s\s\sinto\sa\ssingle-threaded\sC++\sapplication,\n\s\s\s\s\s\sand\smany\sexamples\sof\sembedding\sTk\sinto\smany\sthreads\n\s\s\s\s\s\sof\sa\smulti-threaded\sC++\sapplication,\salways\sinvariably\n\s\s\s\s\s\sincluding\sthe\smain\sthread.\n\n\s\sBut\shad\sanyone\stried\sto\sembed\sTk\sinto\s*only\ssubordinate\nthreads*\n\s\s\s\s\s\sof\sa\sC++\sapplication,\sand\sseen\sthis\sproblem?\n\n\s\s\s\s\s\s\s\s###############################################\n\s\s\s\s\s\s\s\s###############################################\n\n\n\n\s\sOf\scourse,\sthere\sare\stons\sof\sworkarounds\s--\n\s\s\s\s\s\sit's\seasy\sto\sinitialize\sTcl\son\sthe\smain\sthread;\n\s\s\s\s\s\sa\ssimple\sTcl_DeleteInterp(Tcl_CreateInterp())\sdoes\sit,\n\s\s\s\s\s\sand\sthen\sthe\simplicit\sTcl_Finalize\scaused\sby\sDLL\sunloading\n\s\s\s\s\s\swould\swork.\s\s\sBut\sthis..\sfeels\shacky\s--\shaving\sto\ninitialize\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sstuff\sjust\sso\sthe\sfinalizer\sdoesn't\scrash.\n\n\s\sOr\sI\scould\sabandon\sautomatic\sDLL\sloading\n\s\s\s\s\s\sand\sswitch\sto\sexplicity\sloading\sand\sunloading\sthe\sTcl\n\s\s\s\s\s\slibrary\sin\sthread\sB\s--\sthis\swould\sprevent\sTcl_Finalize\n\s\s\s\s\s\sfrom\sbeing\scalled\son\sthread\sA\sat\sall\s--\sbut\sthis\sis\sa\npain.\n\n\s\sInstead,\smy\sproblem\scould\sbe\sfixed\scleanly\s*along*\swith\nbug\s5312\n\s\s\s\sby\schanging\sTcl_Finalize\sto\scorrectly\sfinalize\sTcl\seven\n\s\s\s\sif\scalled\son\sa\svirgin,\snever-called-Tcl-before\sthread.\n\n\n\n\n\s\n===================================================================\n\s\s====================\s\s\s\s\s\s\s\s\s5\s\s\s\s\s\s\s\s\s\n===========================\n\s\n===================================================================\n\n\n\s\sNaively,\sthis\sdoesn't\sseem\shard\s--\n\n\s\s\s\s\s1:\s\sTcl_Finalize\s(tclEvent.c)\n\s\s\s\s\s\s\s\s\sstarts\swith\sthe\sline\n\n\s\s\s\s\s\s\s\s\s\s\s>>\stsdPtr\s=\sTCL_TSD_INIT(&dataKey);\n\n\s\s\s\s\s\s\s\s\swhich\sruns\sthe\srisk\sof\sinadvertently\s*creating*\n\s\s\s\s\s\s\s\s\sthread-local-storage\sif\snone\sexists.\s\sReplacing\sthis\n\s\s\s\s\s\s\s\s\swith\n\n\s\s\s\s\s\s\s\s\s\s>>\stsdPtr\s=\s(ThreadSpecificData\s\n*)TclThreadDataKeyGet(&dataKey);\n\n\s\s\s\s\s\s\s\s\sdoesn't,\sand\sprevents\sTcl_FinalizeThread\s(called\slower\n\s\s\s\s\s\s\s\s\sin\sTcl_Finalize)\sfrom\sdoing\sanything,\ssince\nTcl_FinalizeThread\n\s\s\s\s\s\s\s\s\suses\sidentical\scode\sto\s*check*\sif\nthread-local-storage\sexists,\n\s\s\s\s\s\s\s\s\sand\sdoes\snothing\sif\sit\shasn't.\n\n\n\s\s\s\s2:\s\s\sWhen\sTcl_Finalize\sis\scalled\son\sa\svirgin\sthread,\n\s\s\s\s\s\s\s\s\s\s\s"tsdPtr"\swill\snow\sbe\szero,\swhich\swasn't\spossible\nbefore.\n\n\s\s\s\s\s\s\s\s\sThis\srequires\sone\smore\schange\s--\slower\sin\sTcl_Finalize\n\s\s\s\s\s\s\s\s\s\s\sis\sthe\scode\n\n\s\s\s\s\s\s\s\s\s\s>>\sif\s(tsdPtr->tclLibraryPath\s!=\sNULL)\s{\n\s\s\s\s\s\s\s\s\s\s>>\sTcl_DecrRefCount(tsdPtr->tclLibraryPath);\n\s\s\s\s\s\s\s\s\s\s>>\stsdPtr->tclLibraryPath\s=\sNULL;\n\s\s\s\s\s\s\s\s\s\s>>\s}\n\n\s\s\s\s\s\s\s\s\swhich\sdereferences\s"tsdPtr"\swithout\schecking\sit.\n\s\s\s\s\s\s\s\s\sAdding\san\sadditional\n\n\s\s\s\s\s\s\s\s\s\s>>\sif\s(tsdPtr)\n\n\s\s\s\s\s\s\s\s\sin\sfront\sof\sthis,\sprotects\sagainst\sthe\s"tsdPtr==0"\ncase.\n\n\n\s\sAs\sfar\sas\sI\scan\stell,\sthat's\senough.\s\s\s??\n\s\s..\sand\smy\sC++\sprogram\n\s\s\s\s\s(linked\swith\sthe\sTcl/Tk\sDlls)\sno\slonger\scrashes\son\sexit;\n\n\s\s..\sand\stclsh83,\s(also\slinked\swith\sthis\sDll)\n\s\s\s\s\scan\sbe\sCLOSE'd\sfine.\n\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s--\s--\s--\s--\s--\s--\s--\s--\n\n\s\sI\shesitate\sto\sgo\sfurther\s..\sbecause\sI\smust\sadmit\sI\sdon't\nunderstand\n\s\s\s\sall\sthe\sstuff\sin\sTcl_Finalize\s..\n\n\s\s\s\s\sthose\scalls\sto\sTclFinalizeCompExecEnv()\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sand\sTclFinalizeSynchronization(),\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sjust\sto\sname\stwo\s..\sare\sa\smystery\sto\sme\n\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sand\sI've\sgrown\stoo\stired\sto\strack\sthem,\n\n\s\s\s\s\sso\sI\scan't\stell\sif\sthere's\sany\sother\sreason\sthat\sI'm\snot\n\s\s\s\s\scurrently\sunderstanding\s--\swhy\sTcl_Finalize\sshould\sneed\n\s\s\s\s\sto\sbe\scalled\son\sa\stcl\sthread.\n\n\s\sBut\sI've\smade\sthe\sabove\schanges\sin\smy\sown\scopy,\sand\sit\nseems\sbetter...\n\n\n\s\n===================================================================\n\s\n===================================================================\n\n\n\n\n
J login dgp
J mimetype text/plain
J username dgp
K 219355ffffffffffffffffffffffffffffffffff
U dgp
Z 827005f60e887468ef665b0d5e1a6a9b