You shouldn't be calling the destructor here:
arr.~Array();
When arr
goes out of scope, the destructor is called a second time. That leads to undefined behaviour when delete[]
is called on the pointer data member arr
. When you set that to 0
, you side-step the problem, but the fact remains that you shouldn't be invoking the destructor like that.
Here's an example showing how the lifetime of an automatic storage object is bound to its scope:
#include <iostream>
struct Foo
{
~Foo() { std::cout << "Foo destructor\n";
};
int main()
{
std::cout << in main()\n";
{
Foo f;
} // f gets destroyed here
std::cout << "Exiting main()\n";
};