BUG: "System.NullReferenceException" exception while using the vector<bool> class (837698)
The information in this article applies to:
- Microsoft Visual C++ .NET (2003)
- Microsoft .NET Framework 1.1
SUMMARYThis article discusses a code bug in the vector file that causes a "System.NullReferenceException" exception and discusses how to resolve this bug. SYMPTOMSWhen you call the pop_back method of the vector<bool> template class to delete the last element that is stored in a vector<bool> template class object, you receive the following error message: An unhandled exception of type 'System.NullReferenceException' occurred in ApplicationName
Additional information: Object reference not set to an instance of an object. Note ApplicationName is a placeholder for the executable (.exe) file name of your program. CAUSEYou receive this error message because of a code bug in the vector<bool> template class definition in the vector file.RESOLUTIONTo 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:
#include <vector> - Replace the code in the _tmain function with the following code:
std::vector< bool > v;
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; - In the code window, right-click the #include <vector> statement, and then click Open Document <vector>.
This opens the vector include file in the code window. - 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);
} - 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);
} - On the File menu, click Save All to save all the files.
- On the Build menu, click Rebuild Solution.
- On the Debug menu, click Start to run the program in Debug mode.
The program runs without any errors.
STATUSMicrosoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.
Modification Type: | Minor | Last Reviewed: | 1/5/2006 |
---|
Keywords: | kbSTL kbLangCPP kbConsole kberrmsg kbcode kbbug KB837698 kbAudDeveloper |
---|
|