FIX: Using CString::operator+= May Cause an Access Violation (142385)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft Visual C++, 32-bit Editions 4.0
This article was previously published under Q142385 SYMPTOMS
An access violation may result after using CString::operator+= where the
string on the right hand side of the operator is an empty string. If the
debug CRT libraries are installed, an Assertion Failure in Dbgheap.c line
1017 will occur. The "Sample Code" section in this article gives an example
that demonstrates this problem.
CAUSE
The CString implementation has been optimized in Visual C++ 4.0 to use
reference counting in order to minimize duplication in allocation of
memory. When operator+= is used to append an empty string, the code in
CString::ConcatInPlace() incorrectly decrements the reference count. If
more than one CString is pointing at this data, the call to delete in the
CString destructor causes an access violation.
RESOLUTION
To work around this problem, avoid using the += operator with an empty
string of the form:
str1 += _T("");
Check for empty CStrings prior to appending:
if (!str2.IsEmpty())
str1 += str2;
STATUS
Microsoft has confirmed this to be a bug in the Microsoft products
listed at the beginning of this article. This bug was corrected in
Visual C++ 4.1.
Modification Type: | Major | Last Reviewed: | 10/17/2003 |
---|
Keywords: | kbBug kbfix kbNoUpdate kbVC410fix KB142385 |
---|
|