#include <iostream>
class A {
public:
void Test() {
std::cout << "A::Test" << std::endl;
delete this;
}
};
class B : public A {
public:
void Test() {
std::cout << "B::Test" << std::endl;
}
};
int main() {
A* b = new B();
b->Test();
free(b);
return 0;
}
解释,当执行free(b);时会报错,因为在 b->Test();调用时已经执行了delete this;将堆区的内存释放掉,此时再次根据b的地址去释放会发现不存在,所以报错double free detected in tcache 2