vendredi 27 décembre 2019

C++ member function callback and object lifetime

I have a zeromq subscriper class which handles events from any of my connectes publisher services. This works well but now I want class member function callbacks. The specific class which contains all the member functions has the subscriber class as member but how to manage object lifetime when registering member functions in the subscriber class.

I do not know exactly how to name this problem but imagine the following situation:

1) The subscriber class has a data structure which holds all callbacks. This is quite easy to implement using lambdas and std::function for binding to class members.

2) The class containing the subscriber class is destructed and goes into the destructor. After the destructor all members (including the subscriber) are destroyed. This is the situation which is critical because the server is still abre to send some data and the subscriber is still alive after the desructor has been called but calling a member function after the destructor might be dangerous.

How would you handle this situation?

Aucun commentaire:

Enregistrer un commentaire