Microsoft KB Archive/837698

= BUG: &quot;System.NullReferenceException&quot; exception while using the vector class =

Article ID: 837698

Article Last Modified on 1/5/2006

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.1

-





SUMMARY
This article discusses a code bug in the vector file that causes a &quot;System.NullReferenceException&quot; exception and discusses how to resolve this bug.



SYMPTOMS
When you call the pop_back method of the vector  template class to delete the last element that is stored in a vector  template class object, you receive the following error message:

An unhandled exception of type 'System.NullReferenceException' occurred in

Additional information: Object reference not set to an instance of an object.

Note  is a placeholder for the executable (.exe) file name of your program.



CAUSE
You receive this error message because of a code bug in the vector  template class definition in the vector file.



RESOLUTION
To resolve this bug, modify the code in the vector file. The vector file is located in the vc7\include folder under the folder where you installed Microsoft Visual Studio .NET 2003. To modify the code, follow these steps:  Start Microsoft Visual Studio .NET 2003. On the File menu, point to New, and then click Project. Under Project Types, click Visual C++ Projects, and then click Console Application (.NET) under Templates. In the Name box, type Vector, and then click OK. In Solution Explorer, expand all the folders, and then in the Source Files folder, double-click Vector.cpp.  Add the following code to the code window after the #using statement:  Replace the code in the _tmain function with the following code: std::vector v;
 * 1) include </li>

for( int i = 0; i < 32; ++i ) v.push_back( true );

for( int j = 0; j < 32; ++j ) v.pop_back; // crashes here return 0; </li> In the code window, right-click the #include  statement, and then click Open Document .

This opens the vector include file in the code window.</li>  In the vector file, find, and then comment the following code: const_iterator& operator+=(difference_type _Off) {  // increment by integer this->_Myoff += _Off; this->_Myptr += this->_Myoff / _VBITS; this->_Myoff %= _VBITS; return (*this); } </li>  Add the following code after the code that you commented in step 10: const_iterator& operator+=(difference_type _Off) { // increment by integer if (_Off < 0 && this->_Myoff < 0 - (size_type)_Off) { /* add negative increment */ this->_Myoff += _Off; this->_Myptr -= 1 + ((size_type)(-1) - this->_Myoff) / _VBITS; this->_Myoff %= _VBITS; }   else { /* add non-negative increment */ this->_Myoff += _Off; this->_Myptr += _Myoff / _VBITS; this->_Myoff %= _VBITS; }   return (*this); } </li> On the File menu, click Save All to save all the files.</li> On the Build menu, click Rebuild Solution.</li> On the Debug menu, click Start to run the program in Debug mode.

The program runs without any errors.</li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.

<div class="moreinformation_section">

Steps to reproduce the behavior

 * 1) Uncomment the code that you commented in step 9 of the &quot;Resolution&quot; section.
 * 2) Remove or comment the code that you added in step 10 of the &quot;Resolution&quot; section.
 * 3) On the File menu, click Save All to save all the files.
 * 4) On the Build menu, click Rebuild Solution.
 * 5) On the Debug menu, click Start Without Debugging to run the program.

Keywords: kberrmsg kbbug kblangcpp kbconsole kbstl kbcode KB837698

-

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

© Microsoft Corporation. All rights reserved.