Facing a crash at mutex lock. this is the simulated scenario. The actual code passes the function pointer of a member function to another process and by the time that process calls this member function like a callback, objects created for this class are all deleted. the code crashes while locking of a mutex with error: Caught system_error with code generic:22 meaning Invalid argument My main concern is to handle this crash. is it possible to do any kind of null check to mutex in this scenario?
typedef std::function<void (const char *)> ResponseHandler;
class crash
{
int i;
std::recursive_mutex m_mutex;
public:
void onEvent(string s)
{
std::lock_guard<std::recursive_mutex> lock(m_mutex);
cout << __FUNCTION__ << __LINE__ << " " << s << " " << i << endl;
}
ResponseHandler subscribe()
{
ResponseHandler handler = std::bind(&crash::onEvent, this, std::placeholders::_1);
return handler;
}
crash()
{
cout << __FUNCTION__ << __LINE__ << endl;
i = 100;
}
~crash()
{
cout << __FUNCTION__ << __LINE__ << endl;
}
};
int main()
{
crash *c = new crash();
ResponseHandler rh = c->subscribe();
delete c;
rh("hello");
return 0;
}
this code produces the output as :
crash25
~crash30
onEvent16 hello 0
Aucun commentaire:
Enregistrer un commentaire