Index: ChangeLog
===================================================================
RCS file: /cvsroot/tcl/tcl/ChangeLog,v
retrieving revision 1.1453.2.294
diff -u -u -r1.1453.2.294 ChangeLog
--- ChangeLog	20 Jul 2004 11:13:07 -0000	1.1453.2.294
+++ ChangeLog	21 Jul 2004 01:30:38 -0000
@@ -1,3 +1,11 @@
+2004-07-20  Jeff Hobbs  <jeffh@ActiveState.com>
+
+	* generic/tclEvent.c:       Correct threaded obj allocator to
+	* generic/tclInt.h:         fully cleanup on exit and allow for
+	* generic/tclThreadAlloc.c: reinitialization. [Bug #736426]
+	* unix/tclUnixThrd.c:       (mistachkin, kenny)
+	* win/tclWinThrd.c:
+
 2004-07-20  Daniel Steffen  <das@users.sourceforge.net>
 
 	* unix/tcl.m4: fixed Darwin autoconf breakage caused by
Index: generic/tclEvent.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclEvent.c,v
retrieving revision 1.28.2.7
diff -u -u -r1.28.2.7 tclEvent.c
--- generic/tclEvent.c	15 Jul 2004 09:57:10 -0000	1.28.2.7
+++ generic/tclEvent.c	21 Jul 2004 01:30:38 -0000
@@ -897,7 +897,9 @@
 	/*
 	 * There shouldn't be any malloc'ed memory after this.
 	 */
-
+#if defined(TCL_THREADS) && defined(USE_THREAD_ALLOC) && !defined(TCL_MEM_DEBUG) && !defined(PURIFY)
+	TclFinalizeThreadAlloc();
+#endif
 	TclFinalizeMemorySubsystem();
 	inFinalize = 0;
     }
Index: generic/tclInt.h
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclInt.h,v
retrieving revision 1.118.2.6
diff -u -u -r1.118.2.6 tclInt.h
--- generic/tclInt.h	22 Jun 2004 11:55:35 -0000	1.118.2.6
+++ generic/tclInt.h	21 Jul 2004 01:30:39 -0000
@@ -2138,6 +2138,9 @@
 
 EXTERN Tcl_Obj *TclThreadAllocObj _ANSI_ARGS_((void));
 EXTERN void TclThreadFreeObj _ANSI_ARGS_((Tcl_Obj *));
+EXTERN void TclFinalizeThreadAlloc _ANSI_ARGS_((void));
+EXTERN void TclpFreeAllocMutex _ANSI_ARGS_((Tcl_Mutex* mutex));
+
 
 #  define TclAllocObjStorage(objPtr) \
        (objPtr) = TclThreadAllocObj()
Index: generic/tclThreadAlloc.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclThreadAlloc.c,v
retrieving revision 1.4.2.2
diff -u -u -r1.4.2.2 tclThreadAlloc.c
--- generic/tclThreadAlloc.c	10 May 2003 04:57:40 -0000	1.4.2.2
+++ generic/tclThreadAlloc.c	21 Jul 2004 01:30:39 -0000
@@ -949,4 +949,62 @@
     return 1;
 }
 
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclFinalizeThreadAlloc --
+ *
+ *	This procedure is used to destroy all private resources used in
+ *	this file.
+ *
+ * Results:
+ *	None.
+ *
+ * Side effects:
+ *	None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+void
+TclFinalizeThreadAlloc()
+{
+    int i;
+    for (i = 0; i < NBUCKETS; ++i) {
+        TclpFreeAllocMutex(binfo[i].lockPtr); 
+        binfo[i].lockPtr = NULL;
+    }
+
+    TclpFreeAllocMutex(objLockPtr);
+    objLockPtr = NULL;
+
+    TclpFreeAllocMutex(listLockPtr);
+    listLockPtr = NULL;
+}
+
+#else
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclFinalizeThreadAlloc --
+ *
+ *	This procedure is used to destroy all private resources used in
+ *	this file.
+ *
+ * Results:
+ *	None.
+ *
+ * Side effects:
+ *	None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+void
+TclFinalizeThreadAlloc()
+{
+    Tcl_Panic("TclFinalizeThreadAlloc called when threaded memory allocator not in use.");
+}
+
 #endif /* TCL_THREADS */
Index: unix/tclUnixThrd.c
===================================================================
RCS file: /cvsroot/tcl/tcl/unix/tclUnixThrd.c,v
retrieving revision 1.23.2.6
diff -u -u -r1.23.2.6 tclUnixThrd.c
--- unix/tclUnixThrd.c	15 Jul 2004 22:04:29 -0000	1.23.2.6
+++ unix/tclUnixThrd.c	21 Jul 2004 01:30:39 -0000
@@ -886,19 +886,20 @@
  * Additions by AOL for specialized thread memory allocator.
  */
 #ifdef USE_THREAD_ALLOC
-static int initialized = 0;
+static volatile int initialized = 0;
 static pthread_key_t	key;
-static pthread_once_t	once = PTHREAD_ONCE_INIT;
+
+typedef struct allocMutex {
+    Tcl_Mutex       tlock;
+    pthread_mutex_t plock;
+} allocMutex;
 
 Tcl_Mutex *
 TclpNewAllocMutex(void)
 {
-    struct lock {
-        Tcl_Mutex       tlock;
-        pthread_mutex_t plock;
-    } *lockPtr;
+    struct allocMutex *lockPtr;
 
-    lockPtr = malloc(sizeof(struct lock));
+    lockPtr = malloc(sizeof(struct allocMutex));
     if (lockPtr == NULL) {
 	panic("could not allocate lock");
     }
@@ -907,20 +908,41 @@
     return &lockPtr->tlock;
 }
 
-static void
-InitKey(void)
+void
+TclpFreeAllocMutex(mutex)
+    Tcl_Mutex *mutex; /* The alloc mutex to free. */
+{
+    allocMutex* lockPtr = (allocMutex*) mutex;
+    if (!lockPtr) return;
+    pthread_mutex_destroy(&lockPtr->plock);
+    free(lockPtr);
+}
+
+void TclpFreeAllocCache(ptr)
+    void *ptr;
 {
     extern void TclFreeAllocCache(void *);
 
-    pthread_key_create(&key, TclFreeAllocCache);
-    initialized = 1;
+    TclFreeAllocCache(ptr);
+    /*
+     * Perform proper cleanup of things done in TclpGetAllocCache.
+     */
+    if (initialized) {
+        pthread_key_delete(key);
+        initialized = 0;
+    }
 }
 
 void *
 TclpGetAllocCache(void)
 {
     if (!initialized) {
-	pthread_once(&once, InitKey);
+	pthread_mutex_lock(allocLockPtr);
+	if (!initialized) {
+	    pthread_key_create(&key, TclpFreeAllocCache);
+	    initialized = 1;
+	}
+	pthread_mutex_unlock(allocLockPtr);
     }
     return pthread_getspecific(key);
 }
Index: win/tclWinThrd.c
===================================================================
RCS file: /cvsroot/tcl/tcl/win/tclWinThrd.c,v
retrieving revision 1.24.2.7
diff -u -u -r1.24.2.7 tclWinThrd.c
--- win/tclWinThrd.c	19 Jul 2004 19:23:03 -0000	1.24.2.7
+++ win/tclWinThrd.c	21 Jul 2004 01:30:39 -0000
@@ -1038,17 +1038,20 @@
  * Additions by AOL for specialized thread memory allocator.
  */
 #ifdef USE_THREAD_ALLOC
+static int once;
 static DWORD key;
 
+typedef struct allocMutex {
+    Tcl_Mutex        tlock;
+    CRITICAL_SECTION wlock;
+} allocMutex;
+
 Tcl_Mutex *
 TclpNewAllocMutex(void)
 {
-    struct lock {
-        Tcl_Mutex        tlock;
-        CRITICAL_SECTION wlock;
-    } *lockPtr;
+    struct allocMutex *lockPtr;
 
-    lockPtr = malloc(sizeof(struct lock));
+    lockPtr = malloc(sizeof(struct allocMutex));
     if (lockPtr == NULL) {
 	panic("could not allocate lock");
     }
@@ -1057,10 +1060,19 @@
     return &lockPtr->tlock;
 }
 
+void
+TclpFreeAllocMutex(mutex)
+    Tcl_Mutex *mutex; /* The alloc mutex to free. */
+{
+    allocMutex* lockPtr = (allocMutex*) mutex;
+    if (!lockPtr) return;
+    DeleteCriticalSection(&lockPtr->wlock);
+    free(lockPtr);
+}
+
 void *
 TclpGetAllocCache(void)
 {
-    static int once = 0;
     VOID *result;
 
     if (!once) {
@@ -1111,6 +1123,15 @@
           panic("TlsGetValue failed from TclWinFreeAllocCache!");
       }
     }
+
+    if (once) {    
+        success = TlsFree(key);
+        if (!success) {
+            Tcl_Panic("TlsFree failed from TclWinFreeAllocCache!");
+        }
+
+        once = 0; /* reset for next time. */
+    }
 }
 
 #endif /* USE_THREAD_ALLOC */
