4 ms·
A possible problem with this is that now the callback keeps this alive. A possible solution (in some situations) is to use a weak pointer and check if the objec
by tsbinz 8y ago
A possible problem with this is that now the callback keeps this alive. A possible solution (in some situations) is to use a weak pointer and check if the object is still alive before using the callback, but there's some additional work to make this work nicely ...
- quietbritishjim 8y agoIn some situations, that is indeed a problem. In other situations, that is exactly what you want. For example, say you are passing a message along a sequence of threads (e.g. read from database in one thread, CPU-bound processing in the next, write back to database in the third). In that case, you probably want the lifetime of the request object to exactly match the time where there is an outstanding callback alive (either being called it waiting in a thread's queue). The ownership model in that case is very clear: threads own queues which own callbacks which own shared pointers which own message handling objects.