Important bug for C++ object dynamic delete in XCode Compiler

I have a sample code to test c++ virtual deconstructor like below:


#include <iostream>

class CA {
public:
  virtual ~CA() {
    std::cout<< "~CA" << std::endl;
  }
};

class CB :public CA {
  
public:
  virtual ~CB() {
    std::cout<< "~CB" << std::endl;
  }
};


int main(int argc, const char * argv[]) {
   
  CA *pb = new CB[3];
  delete []pb;
   
  return 0;
}
 

in xcode this code print out result is :

~CA
~CA
~CA

but in other compilers like visual studio C++ and gcc the print out result is:

~CB
~CA
~CB
~CA
~CB
~CA

I think this is a dynamic delete array object with virtual deconstructor bug in xcode compiler 。 because the print result is not conform to C++ form。

I think i find the answer: in the stackoverflow :

If the static type of the object that is being deleted differs from its dynamic type (such as when deleting a polymorphic object through a pointer to base), and if the destructor in the static type is virtual, the single object form of delete begins lookup of the deallocation function's name starting from the point of definition of the final overrider of its virtual destructor. Regardless of which deallocation function would be executed at run time, the statically visible version of operator delete must be accessible in order to compile. In other cases, when deleting an array through a pointer to base, or when deleting through pointer to base with non-virtual destructor, the behavior is undefined.

so in my case the different compiler handle this in different ways。

Important bug for C++ object dynamic delete in XCode Compiler
 
 
Q