Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

thread safe time formatting

25 views
Skip to first unread message

Serve Laurijssen

unread,
Apr 2, 2004, 9:27:36 AM4/2/04
to
The following code has a memory leak in a multithreaded environment (I don't
know why, it just is)

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?


Allan Bruce

unread,
Apr 2, 2004, 11:11:36 AM4/2/04
to

"Serve Laurijssen" <c...@nospam.comp.com> wrote in message
news:YFebc.3754$Y22.100402@zonnet-reader-1...

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


Tim Robinson

unread,
Apr 2, 2004, 12:46:19 PM4/2/04
to

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/


Servé La

unread,
Apr 3, 2004, 1:43:30 AM4/3/04
to
"Tim Robinson" <tim.at.gaat.f...@invalid.com> wrote in message
news:c4k8tb$2i81nu$2...@ID-103400.news.uni-berlin.de...

> Serve Laurijssen wrote:
> > The following code has a memory leak in a multithreaded environment
> > (I don't know why, it just is)
> >
> > 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?
>
> 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?

The threads are created by CreateThread (not under my control) and I think
that it leaks whenever a thread is destroyed.

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q104/6/41.ASP&NoWebContent=1


Sten Westerback

unread,
Apr 5, 2004, 1:54:44 AM4/5/04
to

"Servé La" <i...@benerwegvan.com> wrote in message
news:106sn8c...@corp.supernews.com...

> "Tim Robinson" <tim.at.gaat.f...@invalid.com> wrote in message
> news:c4k8tb$2i81nu$2...@ID-103400.news.uni-berlin.de...
> > Serve Laurijssen wrote:
> > > The following code has a memory leak in a multithreaded environment
> > > (I don't know why, it just is)
> > >
> > > 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?
> >
> > 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?
>
> 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

Servé La

unread,
Apr 5, 2004, 1:09:49 PM4/5/04
to
"Sten Westerback" <sten.westerback@NO_SPAMnokia.com> wrote in message
news:Ew6cc.13135$k4.2...@news1.nokia.com...

> > 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.

That's why I said that it's not under my control


0 new messages