std::locale::global() / std::locale() thread-safety issue - by JoergRue

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 738919 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 4/26/2012 12:10:39 AM
Access Restriction Public


Calling std::locale::global() in one thread can lead to crashes in another thread which uses iostreams (e.g. creates an ifstream).

The problem seems to be that the std::locale()-Constructor first stores a pointer to the global locale through _Getgloballocale and then increases the reference count on the global locale through another call to _Getgloballocale:
	locale() _THROW0()
		: _Ptr(_Init())
		{	// construct from current locale
Between these two calls, a call of std::locale::global() can change the global locale, so that the ref count of the wrong object is increased and the _Ptr object could become invalid.

There are some shell extensions which call std::locale::global so with enough bad luck, it is called when a file open dialog is created without any opportunity for us to prevent it. In effect, this means that we can't use iostreams in background threads at the same time as file open dialogs are used.
Sign in to post a comment.
Posted by Microsoft on 5/3/2012 at 10:11 AM

Thanks for reporting this bug, and extra thanks for your correct analysis. We've fixed this, and the fix will be available in VC11 RTM.

Before the fix, this test case crashed in less than a minute for me. After the fix, it ran for over 5 hours without crashing (and was still running when I terminated it).

If you have any further questions, feel free to E-mail me at .

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by MS-Moderator10 [Feedback Moderator] on 4/26/2012 at 2:38 AM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by MS-Moderator01 on 4/26/2012 at 2:19 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(