Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spurious C++ destructor call under Visual Studio 2008 (absent under GCC)

Take the following (contrived) class hierarchy that prints to the console from the constructors and destructors:

#include <iostream>

class A { 
public:
  A() { std::cout << "A"; }
  ~A() { std::cout << "~A"; } 
};

class B : public A { 
public:   
  B() { std::cout << "B"; }
  ~B() { std::cout << "~B"; } 
};

void func(A a) { }

int main() {   
  B b;
  func(b);
  std::cout << "X";
  return 0; 
}

Compiled under linux with gcc, it prints AB~AX~B~A as expected (the ~A printed before X is a result of the pass-by-value to func which creates a copy that is destructed when the function returns).

But compiled under windows with VS2008 it prints AB~A~AX~B~A - where does the extra ~A come from? It vanishes if the copy xtor is explicitly defined A(A& that) {}; or if the destructor is declared virtual (as it should be, arguably).

like image 910
ScarletPumpernickel Avatar asked Jul 22 '15 20:07

ScarletPumpernickel


1 Answers

The comments suggest that MSVC 2008 uses a temporary that g++ doesn't, to pass the parameter. If so, this is a bug. From C++03 [dcl.init]/12:

The initialization that occurs in argument passing, function return, throwing an exception (15.1), handling an exception (15.3), and brace-enclosed initializer lists (8.5.1) is called copy-initialization and is equivalent to the form T x = a;

Now here is the crucial bit. In T x = a;, if a is not a T or derived from T, then that is equivalent to T x = T(a);, and an extra temporary is conceptually used. (This temporary is eligible for copy-elision).

However, if a is a T or derived from T, then there must not be an extra temporary.It is the same as T x(a);.

In this question's code, since B derives from A, there must not be a temporary.

The supporting text in C++03 is under [dcl.init]/14 (I have highlighted the parts relevant to this question's code sample):

If the destination type is a (possibly cv-qualified) class type:

  • If the class is an aggregate (8.5.1), and the initializer is a brace-enclosed list, see 8.5.1.
  • If the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, constructors are considered. The applicable constructors are enumerated (13.3.1.3), and the best one is chosen through overload resolution (13.3). The constructor so selected is called to initialize the object, with the initializer expression(s) as its argument(s). If no constructor applies, or the overload resolution is ambiguous, the initialization is ill-formed.
  • Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof are enumerated as described in 13.3.1.4, and the best one is chosen through overload resolution (13.3). If the conversion cannot be done or is ambiguous, the initialization is ill-formed. The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the destination type. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an imple- mentation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the object being initialized; see 12.2, 12.8.
like image 76
M.M Avatar answered Nov 07 '22 10:11

M.M