DOCUMENT:Q172398 05-MAY-2001 [visualc] TITLE :FIX: Debug Assertion When Assigning to STL String PRODUCT :Microsoft C Compiler PROD/VER:winnt:5.0 OPER/SYS: KEYWORDS:kbVC500bug kbVC600fix ====================================================================== ------------------------------------------------------------------------------- The information in this article applies to: - The Standard C++ Library, used with: - Microsoft Visual C++, 32-bit Enterprise Edition, version 5.0 - Microsoft Visual C++, 32-bit Professional Edition, version 5.0 ------------------------------------------------------------------------------- SYMPTOMS ======== When you assign a shorter string to an existing string that originally contained a longer string, the assignment corrupts the heap. When running a debug build, you may see an assertion similar to the following: Debug Error! Program DAMAGE: after Normal block (#NNN) at 0xNNNNNNNN CAUSE ===== This problem is due to a bug in the Standard C++ Library basic_string class implementation. When assigning a shorter string to an existing string that originally contained a longer string, the heap is corrupted. The assignment can be done either through operator=() or assign(). RESOLUTION ========== To work around the problem, call the string::erase member function before assigning the new value to the existing string. STATUS ====== Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem was corrected in Visual C++ version 6.0 for Windows. MORE INFORMATION ================ Steps to Reproduce Behavior --------------------------- //Compile options needed: /GX #include #include int main() { std::string str, str2; str = "abcdefghijklmnopqrstuvwxyzabcdefghij" ; str2 = str; //Workaround, uncomment the following line //str.erase() ; str = "zyxw" ; _CrtCheckMemory() ; return 0; } ====================================================================== Keywords : kbVC500bug kbVC600fix Technology : kbVCsearch kbAudDeveloper kbVCLibrary Version : winnt:5.0 Issue type : kbbug Solution Type : kbfix ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 2001.