struct Base {
Base() {
std::cout << "Inside: " << __PRETTY_FUNCTION__ << std::endl;
}
~Base() {
std::cout << "Inside: " << __PRETTY_FUNCTION__ << std::endl;
}
};
struct BaseWrapper {
const Base &b;
};
int main()
{
{
auto *w = new BaseWrapper{{}};
std::cout << "Inside: " << __PRETTY_FUNCTION__ << std::endl;
delete w;
}
return 0;
}
The above code works as I expected when I compile it with C++11 or C++14, but when I compile it with C++17, it gives me something like this:
Inside: Base::Base()
Inside: int main()
As you can see, Base::~Base() was never called. Which doesn't make much sense to me. I have tested this code with GCC 7.3.0 on Ubuntu 18.04.1 LTS, and also with OnlineGDB. They all give the same result.
I'm just wondering if this is a new feature in C++17 or it is a bug?
Update:
I'm well aware that w->b
is a dangling reference. Actually, I deliberately wrote this piece of code just to show that. Then, I found this issue while testing it.
What I really want to know is how severe this issue is, in case I have to stick with GCC 7.3? or if there is any other ways to reproduce the same issue? or the defect report?