What you describe is the classic COM pattern for object lifetime control. You need a smart pointer that keeps reference counter on the object - boost::shared_ptr, boost::intrusive_ptr or ATL's CComPtr class. I would suggest shared_ptr, as the other two will require your own implementation of the ref counting.
↧
Answer by Franci Penov for Publish/Subscribe and Smart pointer
↧