#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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With