class Obj1 {
public:
Obj1(int input):input_(input) {
cout << __func__ << endl;
p = new int;
}
~Obj1() {
cout << __func__ << endl;
if (p != NULL) {
delete p;
p = NULL;
}
}
void operator()() {
cout << input_ << endl;
}
private:
int input_;
int* p;
};
int main(int argc, char** argv) {
thread t{Obj1{10}};
t.join();
return 0;
}
此代码无法正常运行。为什么Obj1的析构函数应该被调用三次? 并且因为双重自由而存在腐败。如何修复此代码?
环境:Ubuntu 10.04.4 LTS,gcc4.7.2