Solved

Memory Leak in setlocale

Posted on 2009-07-15
14
947 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
Comment Utility
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
Comment Utility
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
Comment Utility
It is in VS 2005 SP1
0
 

Author Comment

by:harish_dixit1
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:harish_dixit1
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Any update?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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…
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…

772 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