memory leaks for gmtime() and ctime() 
Author Message
 memory leaks for gmtime() and ctime()

Hi,

    I am using gmtime() & ctime() functions very frequently in my programs.
However, I have not been successful in freeing the memory returned by these
two functions.  I have checked MSDN and it says all of these functions
actually use a shared single statically allocated buffer, but BoundsChecker
keeps complaining that there is a leak there.  I have tried to use free,
delete, CoTaskMemFree, etc to free the memory but to no avail.  I have
checked the API gmtime() and it seems that the memory is allocated by
_malloc_crt.

    Does anyone has any idea about these functions?  Any help will be
appreciated. Thanks!

regards,
Soon



Sun, 25 Apr 2004 10:29:02 GMT  
 memory leaks for gmtime() and ctime()

Quote:

>     I am using gmtime() & ctime() functions very frequently in my
> programs. However, I have not been successful in freeing the memory
> returned by these two functions.  I have checked MSDN and it says all of
> these functions actually use a shared single statically allocated buffer,
> but BoundsChecker keeps complaining that there is a leak there.  I have
> tried to use free, delete, CoTaskMemFree, etc to free the memory but to
> no avail.

Hi Soon,
  These functions do indeed use static buffers.  If you take a look at the
source code for the CRT, you'll find the definitions for the buffers.
Here's some snippets:

   === vc7/crt/src/asctime.c ===
   #define _ASCBUFSIZE   26
   static _TSCHAR buf[_ASCBUFSIZE];

   === vc7/crt/src/gmtime.c ===
   static struct tm tb = { 0 };

As to why BoundsChecker is complaining about this, I'm not sure.  I haven't
looked at this tool.  But, trying to free the memory is not going to work.
The buffer and structs are located in the data segment, not the heap.  The
memory can only be freed when the program is unloaded from memory (or the
CRT if you're using the DLL).

Quote:
> I have checked the API gmtime() and it seems that the memory is allocated
> by _malloc_crt.

You're probably referring to this:

   === vc7/crt/src/gmtime.c ===
   if ( (ptd->_gmtimebuf != NULL) || ((ptd->_gmtimebuf =
        _malloc_crt(sizeof(struct tm))) != NULL) )

Note that this is only for multithreaded versions of the CRT as it falls
inside the "#ifdef _MT" block.  Given the following:

   === vc7/crt/src/mtdll.h ===
   /* following pointers get malloc'd at runtime */

The _malloc_crt call is needed.  The allocated memory is freed by the
_freeptd function:

   === vc7/crt/src/tidtable.c ===
   if (ptd->_gmtimebuf)
        _free_crt((void *)ptd->_gm_timebuf);

The _freeptd function is called by both the _threadstart and _endthread
functions.  The _threadstart function is used in the _beginthread function
to maintain all the per thread data needed by the CRT.

Some common things to check for include:
  * Make sure you're not using the Win32 CreateThread, but instead are using
    the _beginthread function provided by the CRT.
  * Make sure you're not using the Win32 ExitThread, but instead are using
    the _endthread function provided by the CRT.  (Failure to do this will
    cause all the per thread data to leak).

I hope that helps get you started... if none of this helps, I'd look into
what is causing the BoundsChecker tool to suspect the memory leak.

--
Cheerio!
Brandon Bray                 Progam Manager in the Visual C++ Compiler Team

And now a word from the lawyers: This posting is provided "AS IS" with no
warranties, and confers no rights. You assume all risk for your use. ? 2001
Microsoft Corporation. All rights reserved.



Wed, 28 Apr 2004 03:35:06 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. mktime vs ctime and gmtime

2. Handle leak, memory leak...

3. Class object, returning pointers, dynamic memory, memory leaks

4. Finding memory leaks in shared memory pool

5. Memory leaks, DLL and memory allocation.

6. Tracking memory leaks and debugging memory problems

7. Memory leak when using Memory DC

8. Call back from DLL to EXE memory leaks with CTime member functions.

9. About memory leak in VC++, how can i know where my program has memory leak?

10. Tracking down memory leak?

11. Interop memory leak with VB.NET to VC++

12. MDI splitter child crame and memory leaks

 

 
Powered by phpBB® Forum Software