FIX: Explicit Call to Virtual Destructor Corrupts Stack (128805)
The information in this article applies to:
- Microsoft Visual C++, 32-bit Editions 1.0
- Microsoft Visual C++, 32-bit Editions 2.0
- Microsoft Visual C++, 32-bit Editions 2.1
This article was previously published under Q128805 SYMPTOMS
Making an explicit call to a destructor that has been declared virtual can
corrupt the stack. This problem may show up as: - Local variables change unexpectedly.
-or-
- Access violations occur on return from functions.
CAUSE
An explicit call to a virtual destructor generates a call to a helper
function called the scalar deleting destructor. In addition to calling the
destructor, the scalar deleting destructor also calls operator delete,
depending on the value of a flag that is pushed onto the stack before the
call.
Because the compiler is not correctly pushing the flags before calling the
scalar deleting destructor, four extra bytes are popped off the stack each
time an explicit call to a destructor is made. Also, if the last value
stored on the stack before the call happens to be 1, operator delete is
also called.
RESOLUTION
To work around this problem, use the scope resolution operator (::) to
specify which destructor should be called. This will avoid the call through
the virtual table, so that the destructor is called without calling the
scalar deleting destructor.
STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed
at the beginning of this article. This problem was fixed in Microsoft
Visual C++, 32-bit Edition, version 4.0.
Modification Type: | Minor | Last Reviewed: | 7/5/2005 |
---|
Keywords: | kbbug kbCompiler kbCPPonly kbfix KB128805 |
---|
|