473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

memory leak with localtime()?

I have a program that's simulating a POS system, and it writes every sale to a sales log, along with the timestamp. To get the timestamp, I'm passing a time_t struct through localtime(), and passing that through strftime.

However, when i run my program thorugh bcheck, I get:
---------
Blocks in use report (blocks in use: 1 total size 10428 bytes)
Allocation Call Stack:
get_zone < getsystemTZ < _localtime_r < printLog < purchase < main
-----------------
(obviously main, purchase and printlog are my functions)

Anyone know what's going on, here? Surely the standard library wouldn't have leaks?
May 13 '07 #1
8 14910
weaknessforcats
9,208 Expert Mod 8TB
Could we see the call, please? And perhaps the variables used as arguments.
May 13 '07 #2
I have a program that's simulating a POS system, and it writes every sale to a sales log, along with the timestamp. To get the timestamp, I'm passing a time_t struct through localtime(), and passing that through strftime.

However, when i run my program thorugh bcheck, I get:
---------
Blocks in use report (blocks in use: 1 total size 10428 bytes)
Allocation Call Stack:
get_zone < getsystemTZ < _localtime_r < printLog < purchase < main
-----------------
(obviously main, purchase and printlog are my functions)

Anyone know what's going on, here? Surely the standard library wouldn't have leaks?
i dont know the API,
but my guess...
there are API's which allocates the memory and return back a populated structure with the size.
its the duty of the user to deallocate the memory.
please check your API doc.
May 14 '07 #3
I've tried deallocating everything I could think of to do with it, and all i end up with is 'bad frees' ... the manpage doesn't specify anything from locatime() needs to be deallocated afterwards, either (you're not mallocing, so why would you need to free?) - though I can't get a man reference for get_zone, getsystemTZ or _localtime_r.

The call is:
Expand|Select|Wrap|Line Numbers
  1.    char timeString[TIMESTAMP_LEN];
  2.    char *format = {"%a %b %d %H:%M:%S %Y"};
  3.    struct tm *converted;
  4.    time_t now;
  5.  
  6.    time(&now);
  7.    converted = localtime(&now);
  8.    strftime(timeString, TIMESTAMP_LEN, format, converted);
  9.  
May 14 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. char timeString[TIMESTAMP_LEN];
  2.    char *format = {"%a %b %d %H:%M:%S %Y"};
  3.    struct tm *converted;
  4.    time_t now;
  5.  
  6.    time(&now);
  7.    converted = localtime(&now);
  8.    strftime(timeString, TIMESTAMP_LEN, format, converted);
  9.  
works fines. There are no free() calls because there are no malloc() calls. The tm variable used by the time functions is static and is used by all time functions.

You aren't using multiple threads are you?
May 14 '07 #5
This code:
Expand|Select|Wrap|Line Numbers
  1. char timeString[TIMESTAMP_LEN];
  2.    char *format = {"%a %b %d %H:%M:%S %Y"};
  3.    struct tm *converted;
  4.    time_t now;
  5.  
  6.    time(&now);
  7.    converted = localtime(&now);
  8.    strftime(timeString, TIMESTAMP_LEN, format, converted);
  9.  
works fines. There are no free() calls because there are no malloc() calls. The tm variable used by the time functions is static and is used by all time functions.

You aren't using multiple threads are you?
No multple threads. And it *works* no problem. But if you run it under bcheck -all, you'll find after the program has exited, there's a "block still in use" because of the function - if it's just a static variable, shoudln't it be discarded when the program ends?. Now, this is an assigment where I'll have marks deducted for "memory issues", but I can't work out a) why it does this, or b) what I can do about it.
May 15 '07 #6
MattFL
1
I get the same exact error using Visual Studio 2005 and CompuWare Bounds Checker. At least on this platform, I think the secret is in the runtime library. In particular in the function __getgmtimebuf().

A call to localtime() definitely results in allocated memory, it's allocated in __getgmtimebuf() as shown below (look for _malloc_crt()). What happens is the library will allocate memory on the first call, then keep a pointer to the allocated memory and not allocate it a second time. This is why you should NOT free the pointer returned to you by localtime(). If you do, then you'll end up with all sorts of randomness on successive calls to localtime(), and if you try to free the pointer returned by localtime() a second time you'll get a debug run time error about a bad pointer (notice every time you call localtime() you're returned a pointer to the same address). Also notice that if you free the pointer, then call localtime() again, your memory check tool isn't catching the fact that the library is now writing to unallocated memory! These tools are not perfect..

So I think you've got 1 of two situations: (a) There really is a leak and it's in the library, or (b) the library is cleaning up but for some reason our tools aren't catching it. Checking out the source for the library would give you the definite answer, unfortunately I don't have time at the moment to do this myself.

stack:
  • __getgmtimebuf
  • _localtime32
  • localtime <----- returns pointer to memory allocated in __getgmtimebuf

Expand|Select|Wrap|Line Numbers
  1. struct tm * __cdecl __getgmtimebuf ()
  2. {
  3.  
  4.         REG2 struct tm *ptb;            /* will point to gmtime buffer */
  5.         _ptiddata ptd = _getptd_noexit();
  6.         if (!ptd) {
  7.             errno = ENOMEM;
  8.             return (NULL);
  9.         }
  10.  
  11.         /* Use per thread buffer area (malloc space, if necessary) */
  12.  
  13.         if ( (ptd->_gmtimebuf != NULL) || ((ptd->_gmtimebuf =
  14.             _malloc_crt(sizeof(struct tm))) != NULL) )
  15.                 ptb = ptd->_gmtimebuf;
  16.         else
  17.         {
  18.             errno = ENOMEM;
  19.         }
  20.         return ptb;
  21. }
May 30 '08 #7
RRick
463 Expert 256MB
When I first looked at localtime, it looked like it was leaking memory by returning a pointer value to a structure tm. The linux man pages claim that that localtime returns the following
The return value points to a statically allocated struct which might be overwritten by subsequent calls to any of the date and time functions
Another test would be to the the localtime_r routine instead. This has the added benefit of being thread safe, ands puts the results in a structure you pass to it.

BTW: Is much being leaked? Does it grow over time?
May 31 '08 #8
Hello

any final conclusion here :

I do see the same equivalent problem 88 buffers allocated with 16384 bytes each one:



umem_alloc_16384 leak: 88 buffers, 16384 bytes each, 1441792 bytes total
ADDR BUFADDR TIMESTAMP THREAD
CACHE LASTLOG CONTENTS
5d78c0 5ec000 322e2df09fc1a 1
468468 43ed80 0
libumem.so.1`umem_cache_alloc_debug+0x12b
libumem.so.1`umem_cache_alloc+0xc8
libumem.so.1`umem_alloc+0xaf
libumem.so.1`malloc+0x2e
libc.so.1`get_zone+0x55
libc.so.1`getsystemTZ+0xad
libc.so.1`localtime_r+0x2b
libc.so.1`localtime+0x36
tzMgmtDB::calcTZOffSet+0x1c2
tzMgmtDB::updateTZRelativeTime+0x298
main+0x83d
_start+0x6c

>


Thanks!
Valdemar
Nov 12 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Elbert Lev | last post by:
#When I'm running this script on my windows NT4.0 box, #every time dialog box is reopened there is memory growth 384K. #Bellow is the text I sent to Stephen Ferg (author of easygui) # I have...
3
by: Jeremy Lemaire | last post by:
Hello, I am working on cross platform code that is displaying a huge memory leak when compiled on 11.00 HPUX using the aCC -AA flag. It is not leaking on NT, LINUX, Solaris, or HPUX without the...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
7
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a...
17
by: Mike | last post by:
Hello, I have following existing code. And there is memory leak. Anyone know how to get ride of it? function foo has been used in thousands places, the signature is not allowed to change. ...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.