Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2008-12-01
09:20 • Closed ticket [2286247fff]: Encoding cleanup causes memory errors when exiting</... artifact: 4016c99899 user: sf-robot
2008-11-16
10:38 • Pending ticket [2286247fff]. artifact: 7d1e091693 user: dgp
2008-11-14
20:51 • New ticket [2286247fff]. artifact: 4a5cb2d40a user: nobody

Encoding cleanup causes memory errors when exiting

Closed2286247fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 18 years ago
Descriptionplain2008-11-14 20:51:42
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);
}
dgpplain2008-11-16 10:38:40
Please report the version(s)
of Tcl where you see this issue.
sf-robotplain2008-12-01 09:20:07
This Tracker item was closed automatically by the system. It was
previously set to a Pending status, and the original submitter
did not respond within 14 days (the time period specified by
the administrator of this Tracker).
Log in (anonymous is fine) to add a comment.