Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Memory Leak in Time/Date-Functions

Closed / Invalid862924ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 23 years ago
Descriptionplain2003-12-19 13:15:52
I found some Memory leaks. Memory is allocated by
c-system-functions like localtime() and assigned to
variables, which are not freed afterwards. Might be,
there are many more leaks of this kind (?!)...

file tclGetDate.y, function NamedDay: Variable tm
file tclGetDate.y, function NamedMonth: Variable tm
file tclGetDate.y, function RelativeMonth: variable tm
file tclGetDate.y, function TclGetDate: variable tm
file tclUnixTime.c, function TclpGetTimeZone, variable
timeDataPtr and variable stm
dkf Ticket [862924ffff]: 2 changes [History diff]2003-12-19 21:34:59
tclfuchsplain2003-12-22 17:17:43
Logged In: YES 
user_id=934391

Could you please also have a look at unix/tclUnixTime.c.
Maybe it could be rewritten as
struct tm *TclpGetDate(time, useGMT)
    TclpTime_t time;
    int useGMT;
   struct tm *temp;
{
    CONST time_t *tp = (CONST time_t *)time;

    if (useGMT) {
        temp=gmtime(tp);
        free(tp);
        return temp;
    } else {
        temp=localtime(tp);
        free(tp);
        return temp;
    }
}

The same applies to /win/tclWinTime.c with variable tp, but
this function is a bit more tricky.

In /mac/tclMacTime.c the function TclpGetDate seems to be
not-threadsafe. A pointer to static variable "statictime" is
returned from this function, but this variable is influenced
by other threads. Even in single-threaded programms problems
arise, if not carefully used: The user has to copy the data
of the returned pointer to another memory location, because
the next call to TclpGetDate would override the result of
the previous call.
kennykbplain2004-03-05 23:30:21
Logged In: YES 
user_id=99768

The variable returned by 'localtime' is in fact supposed
to be static, rather than dynamically allocated.  If
a memory management tool is reporting the 'struct tm'
in these functions as being leaked, it is either the tool or
the C library that is in error.  Freeing the memory
returned by 'localtime' would be a catastrophic bug.

The code in tclUnixTime.c has been replaced with the
'ThreadSafeGMTime' and 'ThreadSafeLocalTime'
functions some time ago.  Those functions use
gmtime_r and localtime_r if available, and protect the
operation with a mutex if they are not available.

The Unix code for these functions uses a thread-specific
data block; these blocks are cleaned up automatically
in Tcl_FinalizeThread upon exit from the thread.

Since Classic Mac is not supported, and the proposal
is afloat to move the mac/ directory to the Attic,
I question whether it's necessary at the moment to
do anything with tclMacTime.c.  I'm assigning this bug
over to the last known maintainer of the Mac code
to see if he wants to do anything with it.

In short: I see no leaks; the Classic Mac code may have
issues with thread safety.
dasplain2004-03-08 10:15:15
Logged In: YES 
user_id=90580

thread safety in tclMacTime.c is not an issue as Mac Classic 
doesn't support pre-emptive threads... so I see no problem here.
in any case, as Kenny said, the mac classic code will be 
removed from 8.5.x.
Closing this as Invalid
Log in (anonymous is fine) to add a comment.