Microsoft KB Archive/304497

= BUG: _wsetlocale Is Not Threadsafe =

Article ID: 304497

Article Last Modified on 10/22/2002

-

APPLIES TO


 * Microsoft Visual C++ 6.0 Service Pack 5

-



This article was previously published under Q304497



SYMPTOMS
A heap corruption can occur if _wsetlocale is called simultaneously from multiple threads.



CAUSE
_wsetlocale does not synchronize access to a local static variable, outwlocale.



RESOLUTION
To work around this, you must synchronize calls to _wsetlocale.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



MORE INFORMATION
The C Runtime locale mechanism in Microsoft Visual C++ is distinct from the locale mechanism in Microsoft Windows. Though Windows supports per-thread locales, C Runtime supports only a single global locale for any program.

If multiple threads call _wsetlocale, then no thread's locale can be trusted. For example, if one thread calls _wsetlocale for French and tries to use that locale for _wcslwr, and another thread calls _wsetlocale for English before the first thread executes the _wcslwr function call, then the first thread uses the English locale instead of the French locale.

Steps to Reproduce the Behavior

 * 1) define _UNICODE
 * 2) include 
 * 3) include 
 * 4) include 

void __cdecl test(void *i) { _wsetlocale(LC_ALL, L&quot;English&quot;); _wsetlocale(LC_ALL, L&quot;French&quot;); _wsetlocale(LC_ALL, L&quot;German&quot;); _wsetlocale(LC_ALL, L&quot;English&quot;); printf(&quot;%d exit\n&quot;,i); }

void wmain { for (int i=0; i<30; ++i) _beginthread(test,0,(void*)i); getchar; }

Keywords: kbbug kbfix KB304497

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.