Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why will std::function call destructor when an object was bound to a member function?

#include <functional>
#include <iostream>

using namespace std;

class test {
public:
  test(){ p = new int[10];}
 void print_info(double a)
  {
    cerr << a << endl;
  }
  ~test(){
    cerr << __LINE__ << endl;
    delete []p;
  }
private:
  int *p;
};

int main()
{
    test t;
    std::function<void(void)> f = std::bind(&test::print_info, t, 2.0);
    //std::function<void(void)> f = std::bind(&test::print_info, std::cref(t), 2.0);
    return 0;
}

It will crash, since test::~test() is called twice. But, if I replace t with std::cref(t) (or std::ref(t), &t), ~test() will be called only once when exiting main().

I didn't figure out the reason. I'm on Ubuntu 12.04 64bit, using gcc 4.6.3.

like image 941
tfjiang Avatar asked Oct 15 '25 04:10

tfjiang


2 Answers

You're binding a copy of the object t. Since your class tries to manage dynamic memory, but doesn't follow the Rule of Three, it has invalid copy semantics - both copies will try to delete the same dynamic array on destruction.

If you used a class like std::vector, with valid copy semantics, to manage the dynamic array, then all would be fine.

like image 128
Mike Seymour Avatar answered Oct 18 '25 03:10

Mike Seymour


Since you are binding a copy of object t, not t itself, that will be destructed when returning from bind object will be destructed and since you don't overload copy c-tor, default copy c-tor will make shallow copy and your p will be deleted twice. For binding t itself you should use std::ref.

like image 32
ForEveR Avatar answered Oct 18 '25 03:10

ForEveR