time_t ttime = time(NULL);
tm *t = std::localtime(&ttime);
strftime(timestamp, 128, fmt, t);
How can I rewrite this, possibly with win32 functions, that makes the
memleak go away?
Program your own something along the lines of:
void Utils::FormatTime(time_t xiTime, char *xoOut)
{
int lDays=0, lHrs=0, lMins=0, lSecs=0;
lSecs = (int)xiTime%60;
xiTime -= lSecs;
xiTime /= 60;
lMins = (int)xiTime%60;
xiTime -= lMins;
xiTime /= 60;
lHrs = (int)xiTime%24;
xiTime -= lHrs;
xiTime /= 24;
lDays = (int)xiTime;
if (lDays)
sprintf(xoOut, "%dd %dh %dm %ds", lDays, lHrs, lMins, lSecs);
else if (lHrs)
sprintf(xoOut, "%dh %dm %ds", lHrs, lMins, lSecs);
else if (lMins)
sprintf(xoOut, "%dm %ds", lMins, lSecs);
else
sprintf(xoOut, "%ds", lSecs);
}
but remember to make it threadsafe. (the above assumes that xoOut has enough
memory allocated to it)
HTH
Allan
Of course this depends on how the C runtime is implemented, but it should be
OK. With the Microsoft libraries, localtime returns a pointer to a
per-thread struct tm. No memory is allocated.
How do you know it's leaking memory?
--
Tim Robinson (MVP, Windows SDK)
http://mobius.sourceforge.net/
The threads are created by CreateThread (not under my control) and I think
that it leaks whenever a thread is destroyed.
Sure it does.. that's why _beginthread() should be used if CRT functions is
used.
- Sten
That's why I said that it's not under my control