Reputation: 876
I have a function like this:
void cb( void *obj )
{
if(nullptr != obj)
{
auto f = static_cast< function<void()>* >(obj);
(*f)();
}
}
and I use it this way:
auto obj = new function<void()> ( bind(&AClass::AMethod, &x) );
cb(obj);
where AClass is a class, AMethod is a method of AClass and x is an instance of AClass.
Now the question is: why deleting the pointer to std::function inside cb makes the program crash:
void cb( void *o )
{
if(nullptr != o)
{
auto f = static_cast< function<void()>* >(o);
(*f)();
delete f; // <===
}
}
whilst deleting it after the call to cb does not?
auto obj = new function<void()> ( bind(&AClass::AMethod, &x) );
cb(obj);
delete obj; // <===
Upvotes: 2
Views: 1743
Reputation: 7132
This works fine on both g++ 4.8.1 and clang 3.4. Also both show nothing of interest valgrind. So maybe the problem is somewhere else in your code or related to your compiler version?
I tested deleteing at both of the mentioned places.
Upvotes: 2