Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 4a5cb2d40aedd9880332837a9a1d3e5bec1cc469
Ticket: 2286247fffffffffffffffffffffffffffffffff
Encoding cleanup causes memory errors when exiting
User & Date: nobody on 2008-11-14 20:51:42
Changes

  1. assignee changed to: "msofer"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    The cleanup function Tcl_FinalizeEncodingSubsystem (tclEncoding.c) causes Purify FMR and FMW errors when exiting tcl/tk.
    The problem occurs when an escape-driven encoding gets freed after its associated sub-encodings have been freed. In that case, the escape-driven encoding tries to free its sub-encodings, causing memory problems.
    To address this problem, one must guarantee that the escape-driven encodings are freed before their sub-encodings. This could be done by using a stack / linked list (instead of a hash table) to store the encodings in the order they are created. The encodings would have to be deleted in the reverse order. The other solution (implemented in the code below) is to traverse the hash table multiple times and to delete the escape-driven encodings first. This solution is more self-contained and involves less change in the code than using a different data structure.
    
    
    
    void
    TclFinalizeEncodingSubsystem()
    {
        Tcl_HashSearch search;
        Tcl_HashEntry *hPtr;
    /* SYNOPSYS_START */
    #ifndef TCL_ORIG
        int found = 1;
    #endif
    /* SYNOPSYS_END */
        Tcl_MutexLock(&encodingMutex);
        encodingsInitialized  = 0;
        FreeEncoding(systemEncoding);
    /* SYNOPSYS_START */
    #ifndef TCL_ORIG
        /*
         * Free all escape-driven encodings first. This assumes that escape-driven 
         * encodings do not reference other escape-driven encodings.
         */
        while (found) {
            found = 0;
    hPtr = Tcl_FirstHashEntry(&encodingTable, &search);
    while (hPtr != NULL) {
                Encoding *encodingPtr;
        encodingPtr = (Encoding *) Tcl_GetHashValue(hPtr);
        if (encodingPtr->freeProc == EscapeFreeProc) {
                    /* This is an escape-driven encoding. */
                    FreeEncoding((Tcl_Encoding) encodingPtr);
                    found = 1;
                    break;
                    /* Let's traverse the hash table again from the start to
                     * find another escape-driven encoding (possibly the same
                     * one if it has not been freed, its ref count not having
                     * reached zero yet). The reason for this multi-pass
                     * approach is that freeing an encoding means deleting
                     * its hash entry and we cannot do that while traversing
                     * the hash table with Tcl_NextHashEntry.
                     */
        }
        hPtr = Tcl_NextHashEntry(&search);
    }
        }
        /* What remains in the hash table are non-escape-driven encodings.
         * Let's free them.
         */
    #endif
    /* SYNOPSYS_END */
    
        hPtr = Tcl_FirstHashEntry(&encodingTable, &search);
        while (hPtr != NULL) {
    /*
     * Call FreeEncoding instead of doing it directly to handle refcounts
     * like escape encodings use.  [Bug #524674]
     * Make sure to call Tcl_FirstHashEntry repeatedly so that all
     * encodings are eventually cleaned up.
     */
    FreeEncoding((Tcl_Encoding) Tcl_GetHashValue(hPtr));
    hPtr = Tcl_FirstHashEntry(&encodingTable, &search);
        }
        Tcl_DeleteHashTable(&encodingTable);
        Tcl_MutexUnlock(&encodingMutex);
    }
    
  4. foundin changed to: "None"
  5. is_private changed to: "0"
  6. priority changed to: "5 Medium"
  7. resolution changed to: "None"
  8. status changed to: "Open"
  9. submitter changed to: "nobody"
  10. subsystem changed to: "10. Objects"
  11. title changed to: "Encoding cleanup causes memory errors when exiting"
  12. type changed to: "Bug"