Memory Leak in setlocale

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.
harish_dixit1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pgnatyukCommented:
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
pgnatyukCommented:
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
pgnatyukCommented:
It is in VS 2005 SP1
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

harish_dixit1Author Commented:
Since my project is also in VS2005 with SP1. Then why dev partner tool is showing the memory leak.
0
pgnatyukCommented:
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
harish_dixit1Author Commented:
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
pgnatyukCommented:
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
harish_dixit1Author Commented:
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
pgnatyukCommented:
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
pgnatyukCommented:
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
harish_dixit1Author Commented:
I tried with Release build its not showing me memory leak but in debug build its showing with VS2005.
0
pgnatyukCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
harish_dixit1Author Commented:
Please find the code:

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

}
0
harish_dixit1Author Commented:
Any update?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.