หมายเลขบทความ (Article ID): 813810 - รีวิวครั้งสุดท้าย: 14 มกราคม 2554 - Revision: 2.0 STL std::string class causes crashes and memory corruption on multi-processor machines
เนื้อหาบนหน้านี้อาการWhen you build applications in Microsoft Visual C++ 6.0 that use the supplied Standard Template Library
(STL), memory corruption may occur, or your computer may stop responding. These symptoms occur more frequently on multi-processor computers.
Previously, the same code may have worked without such issues on a
single-processor computer. When you examine the faulting thread in a debugger, you typically see the failure in a memory management function. Frequently you see thebasic_string<char...></char...>class methods in the stack trace. Because memory
corruption is also a symptom, failures may appear in areas that are unrelated to string
processing. The following are examples of stack traces where this problem was the cause of a crash: สาเหตุThe Standard Template Library (STL) that is included with Microsoft
Visual C++ 6.0 is not safe for multi-threaded applications. In particular, the
implementations of thestd::stringclass depend on thebasic_string<...></...>template class. กระบวนการbasic_string<...></...>template class reference
counts copies of a hidden character buffer. กระบวนการbasic_string<...></...>template class stores the count in an 8-bit
unsigned char. The following general issues occur after this implementation:
การแก้ไขYou must rebuild the application after you make the STL
thread-safe. The preferred method to obtain a thread-safe STL is to upgrade
the STL to a newer version that is based on the current Visual C++ standard. However, the STL that is based on
the current Visual C++ standard is not identical to the STL that was available at the time
that Microsoft Visual C++ 6.0 was released as a new product. However, upgrading to a new
version may be trivial depending on the STL functions that your application uses. To obtain new versions of the thread-safe STL, use one of the following methods: Method 1: Use Microsoft Visual C++ .NET (versions 7.0 and later)Open each Visual C++ project in your application, allowthe project to automatically convert to the new project format, and then rebuild it. กระบวนการstd::stringclass implementation in this version is thread-safe for the described problem. If you use the DLL run-time library feature in your any one of the projects in your application, you must distribute the new Visual C++ run-time components (such as Msvci7x.dll, Msvcp7x.dll, and Msvcr7x.dll) with your rebuilt application.หมายเหตุ:You do not have to distribute the Microsoft .NET Framework to client computers to use Microsoft Visual C++ .NET. Method 2: Use Microsoft Visual C++ 6.0 with a replacement STL from a third partyThe details of integration vary by product, and the individual vendors provide support. One source for a successor STL version is Dinkumware, Ltd., the company where Microsoft licenses the Visual C++ 6.0 STL. It is claimed that it can integrate with existing build processes. For more information, and for a list of known bugs and workarounds, visit the following Dinkumware Web site:www.dinkumware.com
(http://www.dinkumware.com)
Microsoft provides third-party
contact information to help you find technical support. This contact
information may change without notice. Microsoft does not guarantee the
accuracy of this third-party contact information.The third-party products that this article discusses are
manufactured by companies that are independent of Microsoft. Microsoft makes no
warranty, implied or otherwise, regarding the performance or reliability of
these products.การหลีกเลี่ยงปัญหาWork around the std::string class issue in Microsoft Visual C++ 6.0 STLIf you do not upgrade to a new version of the STL, you can try to correct thestd::stringclass thread-safety issue in the standard Microsoft Visual C++ 6.0 installation. Although there are multi-threading issues with several of the classes in the Microsoft Visual C++ 6.0 STL, by far the most common and problematic class is thestd::stringคลาสที่ The following steps and workarounds are stopgap measures to make sure that an application is working correctly, and the measures provide time to investigate other alternatives. Consider that these instructions will create new code paths and behavior perhaps throughout your whole application. Thoroughly test the rebuilt application in accordance with a company's or an individual's software policies before widespread deployment.Disable string reference countingEach of the workarounds that is documented in this section require that you first disable the reference-count mechanism. To disable reference counting, you must modify the<xstring></xstring>header file and set the_FROZENenumeration constant to0. In default installations, the <xstring> header file is in the following location:</xstring>C:\Program files\Microsoft Visual Studio\VC98\Include การเปลี่ยนแปลงนั้น_FROZENenumeration constant to0ในการ<xstring></xstring>header file at line 62 so that it looks
similar to the following:Method 1: Use static CRT linkage onlyModify the project settings in all your projects that use thestd::stringclass to link to the static version of the Microsoft run-time library (CRT). You cannot use this approach if your project also has theUse MFC in a Shared DLLsetting enabled. For each project, follow these steps:
Method 2: Use dynamic CRT linkageIf your project code must link to the runtime library (CRT) as a DLL, you must take a different approach. Dynamic CRT linkage is the default setting for DLL projects. Dependencies on other components such as MFC or third-party libraries that are licensed for use with your application, typically require dynamic linkage to the CRT. If your only dependency is MFC, you can use theUse MFC in a Static Libraryoption, and apply Method 1. By default, when you create a new project in Microsoft Visual C++ 6.0, the project uses the CRT from a DLL.The dynamic CRT linkage project setting links your application to implementations for somestd::stringclass methods in the pre-built Microsoft CRT DLL that is named Msvcp60.dll. Because Microsoft built that DLL by using the unmodified<xstring></xstring>header file, the change to the_FROZENconstant that you made to the local copy of<xstring></xstring>is not honored for functions that are called out of that library. These include functions such as_Tidy()และassign()that are supplied in the Msvcp60.dll file for the<char></char>และ<short></short>instantiations of thebasic_stringคลาสที่ กระบวนการbasic_stringclass is the base for thestd::stringคลาสที่ To use static implementations of thestd::stringclass in your modules instead of the Microsoft-supplied implementations in the Msvcp60.dll file, follow these steps:
Method 3: Using a clever hack to avoid linkage issuesCreate a typedef forunsigned char, and use that instead of the existingstd::stringclass typedef. The typedef may take a form that is included in a header file in the source files of the application that use thestd::stringคลาสที่ The typedef may appear similar to the following:Method 4: Using a custom std::string DLLThis option gains you the benefit of smallest code size by putting thestd::stringclass implementations in a single DLL. Create a DLL project that exports thestd::stringคลาสที่ Link to that DLL instead of to the standard Msvcp60.dll file. You must redistribute this new DLL together with your application. This is an advanced option.ข้อมูลเพิ่มเติมThe following C++ code examples demonstrate one scenario
that may occur when there is a lack of synchronization: Thread1 starts to create a new character buffer for the new assignment to A1, recognizes a positive reference count on its previous shared character buffer, and then decrements that count by 1 to 0. At the same time, Thread2 is also in the process of assignment to B. B shares the character buffer of A2, and raises the reference count on the character buffer of A2, trying to increment it to 2 just before Thread1 writes a 0 to the reference count. The reference count is now 0 instead of 1. The reference count would have been 0 if access to the reference counter had been synchronized. When Thread2 assigns a new value to A2, Thread2 sees the reference count of 0 and discards the original shared character buffer that B still references. The memory that held the character buffer is now available for other uses in the application. However, std::string B still holds a pointer to the character buffer. The following scenarios cause corruption and crashes:
ข้อมูลอ้างอิงFor more information about Visual C++ language and
compiler issues, see theIs the STL included with VC++ thread-safe?topic at the following Microsoft Most Valuable Professional (MVP) Web site: http://www.mvps.org/vcfaq
(http://www.mvps.org/vcfaq)
แปลโดยคอมพิวเตอร์ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ ต่อไปนี้เป็นฉบับภาษาอังกฤษของบทความนี้:813810
(http://support.microsoft.com/kb/813810/en-us/
)
| ทรัพยากรอื่นๆ ไซต์การสนับสนุนอื่นๆ
ชุมชนการแปลบทความ |






Windows Live
Facebook
Twitter
Linkedin
Digg it
Yahoo
Delicious
StumbleUpon
Yammer
Reddit
Technorati
FriendFeed
Email



กลับไปด้านบน