FIX: /Og Causes vfptr Initialization Problems (151500)
The information in this article applies to:
- Microsoft Visual C++, 32-bit Enterprise Edition 4.2
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 4.2
- Microsoft Visual C++, 32-bit Professional Edition 5.0
This article was previously published under Q151500 SYMPTOMS
Under some circumstances, virtually calling a class member function from
within the class's constructor may cause the wrong version of the member
function to be called when global optimizations are enabled (/Og or
#pragma optimize("g",on) ). Virtually calling a class member function
means assigning the value of this to a pointer variable and attempting to
call virtual member functions via that pointer. Please see the sample
code in the More Information Section below.
CAUSE
The vfptr is not being properly initialized. In the sample code below, p
in effect becomes an A* instead of a B*.
RESOLUTION
One workaround is to disable global optimizations for the class
constructor. Please note the sections of code that compile conditionally
for the constant 'WORKAROUND' in the sample code below.
Another workaround is not to call the member function(s) virtually. That
is call them directly via the this pointer, ( this->f() ), or to simply
call the function using the implied this pointer, ( f() ).
STATUS
Microsoft has confirmed this to be a bug in the Microsoft products
listed at the beginning of this article. This bug has been corrected in
Visual Studio 97 Service Pack 1.
For additional information about the Visual Studio 97 Service Pack 1,
please see the following article in the Microsoft Knowledge Base:
170365 INFO: Visual Studio 97 Service Packs - What, Where, and Why
Modification Type: | Major | Last Reviewed: | 12/10/2003 |
---|
Keywords: | kbBug kbcode kbCompiler kbfix kbVS97sp1fix KB151500 |
---|
|