title: Deleting caller object safely within a callback function
body: <p>Code segment from the library:</p>
  
  <pre><code>class Client{
  public:
      class CallBack {
      public:
          virtual void onData(Client* caller, std::string&amp; data) =0;
      };
  
      Client(CallBack* callback):m_callBack(callback){}
      virtual ~Client(){}
      void onData(std::string data) {
          m_callBack-&gt;onData(this, data);
          m_totalDataVol += data.size();
      }
  
  private:
      CallBack* m_callBack;
      int m_totalDataVol = 0;
  }
  </code></pre>
  
  <p>Code segment from the application:</p>
  
  <pre><code>class AppHandler: public Client::Callback {
      void onData(Client* caller, std::string&amp; data) {
          /* Some complex logic and check certain conditions*/
              delete caller; // Application will crash, due to 
                             // accessing member of deleted object (m_totalDataVol)
      }
  }
  </code></pre>
  
  <p>In addition Caller object (instance of Client class) is owned by the application, and Application no restriction to delete that.</p>
  
  <p>How do I overcome this problem?</p>
  
  <p>Very complex scenario: 
  <code>Client</code> class of the base library can be extended by a another library (<code>ClientEx</code> class) and the application might use that extended library (Not the base library)</p>