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

Article translations Article translations
Article ID: 281446 - View products that this article applies to.
This article was previously published under Q281446
Expand all | Collapse all

On This Page

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 "Applies to" 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
#include <windows.h>
#include <set>

typedef std::set<int> 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 );
} 
				

Properties

Article ID: 281446 - Last Review: August 7, 2007 - Revision: 3.5
APPLIES TO
  • The C Run-Time (CRT), when used with:
    • Microsoft Visual C++ 6.0 Service Pack 4
Keywords: 
kbbug kbcpponly kbfix kbstl kbvc600fix kbvs600sp5fix KB281446
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com