0
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のデストラクタを3回呼び出す必要があるのはなぜですか?そして、ダブルフリーなので破損が発生します。このコードを修正する方法は?

環境:Ubuntu 10.04.4 LTS、gcc4.7.2

4

0 に答える 0