FIX: Destructor Called on Non Constructed Temporary (129617)
The information in this article applies to:
- Microsoft Visual C++ 1.5
- Microsoft Visual C++ 1.51
- Microsoft Visual C++ 1.52
- Microsoft Visual C++, 32-bit Professional Edition 2.0
- Microsoft Visual C++, 32-bit Professional Edition 2.1
This article was previously published under Q129617 SYMPTOMS
The destructor of a class is called on a temporary that was never
constructed if all the following conditions exist:
- Two classes A and B are created.
- A global function takes an argument of class B by value.
- A conversion operator is used to convert from class A to class B.
- The global function is called with an item of class A.
The destructor being called for an object that was never constructed can
cause a general protection (GP) fault or other memory errors at run time.
WORKAROUND
Listed below in order of preference are three workarounds to this
problem:
- Define a conversion constructor in class B instead of a conversion
operator in class A.
-or-
- Define the function to take an argument of "const B &" instead of
taking class B by value.
-or-
- Explicitly instantiate a B object and call the global function with the
instance of the B object.
The first workaround produces much cleaner code than the other two
workarounds. See the sample code listed below for further details.
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 kbfix KB129617 |
---|
|