Solved

Memory Leak in setlocale

Posted on 2009-07-15
14
961 Views
Last Modified: 2013-12-04
Hi,

I am making _tsetlocale calls to set the locale information in my application. Memory Leak Analyzer is showing memory leak for the following code:

        _tsetlocale(LC_COLLATE,      _T(""));
      

Its showing memory leak in the following function in the file :
"C:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src\wsetloca.c"

wchar_t * __cdecl _wsetlocale (
        int _category,
        const wchar_t *_wlocale
        )

Inside this function its showing leak at the following line:
 refcount = (int *)_malloc_crt(size * sizeof(wchar_t) + sizeof(int));

Its showing 58 bytes leak for one call of _tsetlocale.
0
Comment
Question by:harish_dixit1
  • 8
  • 6
14 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24859445
Here someone reports about the same issue: http://blog.kalmbachnet.de/?postid=47
But that was in 2005. Here people say that it's fixed: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=99285
Here are few examples if you need:
http://msdn.microsoft.com/en-us/library/x99tb11d.aspx
http://www.tenouk.com/ModuleG1.html
 But looks like you can release the memory? I will try to check it with BoundChecker.
 
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24860425
I tested in Purify+ - it does not show the leak in setlocale. It always finds a reason to complain, but I get the same report with setlocale in the code and without it.
 

#include <stdio.h>

#include <locale.h>

#include <time.h>
 

int main()

{

	time_t ltime;

	struct tm *testime;

	unsigned char locstr[100];
 

	setlocale(LC_ALL, "italian");

	time(&ltime);

	testime = gmtime(&ltime);
 

	if(!strftime((char *)locstr, 100, "%#x", (const struct tm *)testime))
 

		printf("strftime failed!\n");
 

	else

		printf("In Italian locale, strftime returns \"%s\"\n", locstr);
 

	setlocale(LC_ALL, "french");

	time(&ltime);

	testime = gmtime(&ltime);
 

	if(!strftime((char *)locstr, 100, "%#x", (const struct tm *)testime))

		printf("strftime failed!\n");
 

	else

		printf("In French locale, strftime returns \"%s\"\n", locstr);
 

	setlocale(LC_ALL, "C");

	time(&ltime);

	testime = gmtime(&ltime);
 

	printf("Back to default...\n");

	if(!strftime((char *)locstr, 100, "%#x", (const struct tm *)testime))

		printf("strftime failed!\n");
 

	else

		printf("In 'C' locale, strftime returns \"%s\"\n", locstr);
 

	return 0;

}

Open in new window

0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24860431
It is in VS 2005 SP1
0
 

Author Comment

by:harish_dixit1
ID: 24866777
Since my project is also in VS2005 with SP1. Then why dev partner tool is showing the memory leak.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24866793
I will check soon with the BoundChecker.
Try to comment this line with the setlocale and check. I think you will see the same leak.
0
 

Author Comment

by:harish_dixit1
ID: 24867032
I already tried it. When i comment this line there is no memory leak in file  ""C:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src\wsetloca.c""
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24873831
Looks like: setlocale(LC_ALL, "");  in the end is the fix.
Here you can find more info: http://binglongx.spaces.live.com/blog/
Actually Microsoft says that there is a leak and it is fixed in VS2008. One MVP here:
http://www.tech-archive.net/Archive/VC/microsoft.public.vc.stl/2005-02/0058.html
says that it does not happen in VS 7.1.
Here is a guy saying that the detection time is wrong: http://groups.google.com/group/microsoft.public.vc.stl/msg/337756da516a8a43?pli=1
free memory happens a bit later.
Here is even a screenshort from the BoundChecker:  http://www.codeguru.com/FORUM/showthread.php?t=472654
#include <stdlib.h>
#include <tchar.h>
#include <crtdbg.h>
#include <locale.h>

int _tmain()
{
    setlocale(LC_ALL, "");
    _CrtDumpMemoryLeaks();
}
This code didn't show the leak.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:harish_dixit1
ID: 24877097
we don't call LC_ALL as it breaks SQLite3 by replacing the '.' in floats/DATEs with a ',' in some languages.
I am using SQLite3 database.

It is confirmed that in VS2008 this issue is fixed. Not in VS 2005.

Is there any other way to get rid of this memory leak.

One more question related to memory leak:
Some memory leaks come in debug build but not in release build. Does it mean that when we ship our application (release build) then these memory leaks will not come?


0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24877165
Leak is the leak. If you use different source code, different libraries for the debug and release configurations, ..., everything's possible.
But better do not hope that the release version does not have a leak if the debug does.
The release is much smaller then the debug. So the leak can make a trouble later.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24902143
I've tested the code with setlocale I posted in my first comments with the latest BoundChecker (9.0.2) - no leaks.
setlocale-boundChecker.bmp
0
 

Author Comment

by:harish_dixit1
ID: 24913030
I tried with Release build its not showing me memory leak but in debug build its showing with VS2005.
0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 250 total points
ID: 24913185
I tested the debug configuration.
You can post a code here - better a small independent sample with the parameters you use in your app. Interesting if this sample will have the same problem.
0
 

Author Comment

by:harish_dixit1
ID: 25070616
Please find the code:

void CNL::SynchronizeLocales()
{
      /
      _tsetlocale(LC_COLLATE,      _T(""));
      _tsetlocale(LC_CTYPE,      _T(""));
      _tsetlocale(LC_TIME,      _T(""));
      _tsetlocale(LC_MONETARY,_T(""));

}
0
 

Author Comment

by:harish_dixit1
ID: 25086714
Any update?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2015 locks debug executable 9 136
countEvens challenge 2 78
twoTwo  challenge 35 93
fix34  challenge 9 105
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now