Microsoft KB Archive/822369

= BUG: Access violation occurs when copied objects contain pointers to their own members while using the CArray class in Visual Studio .NET or in Visual Studio 2005 =

Article ID: 822369

Article Last Modified on 1/18/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition

-





SYMPTOMS
The CArray class may not handle C++ classes correctly. When you copy objects in memory without using constructors and destructors of the objects, and you access a member that is a pointer to another member in an object, you may receive an access violation, or an invalid value may be returned.



CAUSE
The CArray Microsoft Foundation Classes (MFC) class uses the memcpy function to copy the objects in memory. This behavior causes invalid references. When the copied objects contain pointers to their own members (&quot;this&quot; pointer), the memcpy function may cause invalid references.



WORKAROUND
To work around this problem, do not use members in an object that are pointers to other members in the object or to the object itself (&quot;this&quot; pointer) with an element that is copied.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



MORE INFORMATION
The CArray class supports arrays that are similar to C arrays, but that can dynamically become smaller or larger as necessary. CArray::SetSize uses the memcpy function. You must use CArray::SetSize to establish the size and to allocate memory before you use this array. Otherwise, adding elements to your array may cause frequent reallocation and copying. Frequent reallocation and copying are inefficient and can fragment memory.

The following sample code may reproduce this behavior: //Header File (TestCopy.h)
 * 1) pragma once
 * 2) include &quot;resource.h&quot;

class MyClass { public:

MyClass { mySelf = this; }

MyClass( MyClass *mySelf ) { mySelf = this; }  MyClass *mySelf; }; //Implementation File (TestCopy.cpp)
 * 1) include &quot;stdafx.h&quot;
 * 2) include &quot;TestCopy.h&quot;
 * 3) include 

void main {   MyClass *myObj = new MyClass;

if (myObj == myObj->mySelf) printf(&quot;Addresses are matching\n&quot;); else printf(&quot;Addresses are NOT matching\n&quot;);

CArray m_myArray;

m_myArray.Add(myObj); m_myArray.SetSize(32,128);

MyClass *myObj1 = (MyClass *) m_myArray.GetData;

if (myObj1 == myObj1->mySelf) printf(&quot;Addresses are matching even after CArray Operation \n&quot;); else printf(&quot;Addresses are NOT matching after CArray Operation \n&quot;); } //Output is as follows Addresses are matching Addresses are NOT matching after CArray Operation

Additional query words: CArray AV violation pointer copy

Keywords: kbbug kbprogramming kbcollectionclass KB822369

-

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

© Microsoft Corporation. All rights reserved.