Destroying an object that someone else has a reference to
<p>Suppose my application is using a library object <code>Server</code> which has a callback <code>onConnected</code>, which passes a reference to a <code>Connection</code> object when a new connection is established with a client; the <code>Server</code> retains ownership of the <code>Connection</code>. I would like to destroy the <code>Connection</code> when it is closed (whether locally or remotely), but I'm not sure how best to deal with notifying the application that is using the connection.</p>

<p>The obvious solution is to have some sort of <code>onDisconnected</code> callback and either <code>delete</code> it immediately, allowing the application to crash if it tries to continue using it, or pass it as <code>shared_ptr</code> and just let the object linger until the application cleans it up. Neither of these approaches feels particularly nice to me. The former just feels unsafe, and the latter muddies the concept of who owns the connection and makes leaks possible if the application ignores the callback.</p>

<p>Is there some idiomatic solution to this that I'm missing, or just a better way of doing it?</p>
