Microsoft KB Archive/281446

= FIX: Access violation with std::map::erase or std::set::erase in a multithreaded application =

Article ID: 281446

Article Last Modified on 8/7/2007

-

APPLIES TO


 * The C Run-Time (CRT), when used with:
 * Microsoft Visual C++ 6.0 Service Pack 4

-



This article was previously published under Q281446



SYMPTOMS
When you call the std::map::erase and std::set::erase member functions at the same time from multiple threads, an access violation may be generated.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.

This bug has been fixed in Microsoft Visual Studio 6.0 Service Pack 5 (SP5).

To download the latest service pack for Visual Studio, visit the Visual Studio Product Updates page at the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/vstudio/Aa718353.aspx



MORE INFORMATION
The bug was introduced in Visual Studio 6.0 Service Pack 4 (SP4) when the bug that is documented in Q248477 was corrected:

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

248477 FIX: Two threads may deadlock when you use the STL map or set in a multithreaded application

Steps to Reproduce Behavior
The following sample code demonstrates the bug: // Test.cpp // Compiler option needed: /MT or /MD
 * 1) include 
 * 2) include

typedef std::set TestSet;

DWORD WINAPI Worker( LPVOID ) { TestSet set; int g_cIters =4;

for( int i=0; i < g_cIters; i++ ) {       int r = rand; int j;

for( j=0; j < r; j++ ) {       set.insert( j ); }

for( j=0; j < r; j++ ) {           TestSet::iterator it = set.find( j ); set.erase( it ); }   }

return 0; }    void main {   int cThreads = 10;

HANDLE aThreads[10];

for( int i=0; i < cThreads; i++ ) {       aThreads[i] = CreateThread( NULL, 0, Worker, NULL, 0, NULL ); }

WaitForMultipleObjects( cThreads, aThreads, TRUE, INFINITE ); }

Keywords: kbbug kbcpponly kbfix kbstl kbvc600fix kbvs600sp5fix KB281446

-

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

© Microsoft Corporation. All rights reserved.