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

SUMMARY

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

SYMPTOMS

When 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.

CAUSE

You receive this error message because of a code bug in the vector<bool> 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:
  1. Start Microsoft Visual Studio .NET 2003.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual C++ Projects, and then click Console Application (.NET) under Templates.
  4. In the Name box, type Vector, and then click OK.
  5. In Solution Explorer, expand all the folders, and then in the Source Files folder, double-click Vector.cpp.
  6. Add the following code to the code window after the #using statement:
    #include <vector>
  7. 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;
  8. 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.
  9. 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);
    	}
  10. 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);
    	}
  11. On the File menu, click Save All to save all the files.
  12. On the Build menu, click Rebuild Solution.
  13. On the Debug menu, click Start to run the program in Debug mode.

    The program runs without any errors.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to reproduce the behavior

  1. Uncomment the code that you commented in step 9 of the "Resolution" section.
  2. Remove or comment the code that you added in step 10 of the "Resolution" 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.

Modification Type:MinorLast Reviewed:1/5/2006
Keywords:kbSTL kbLangCPP kbConsole kberrmsg kbcode kbbug KB837698 kbAudDeveloper