title: Is there any way to check if pointer is dangling?
body: <p>I have a code where I use a pointer to access some datablock. In some rare cases, a few members of the datablock are empty, and as a result the pointer becomes dangling. In fact, I get the correct pointer but the program crashes when trying to do something with the pointer.</p>
  
  <p>The usual advice would be to avoid this type of usage. But sadly, the framework I use requires that I use this type of data access methods.</p>
  
  <p>Is there a way I can "check" if the pointer is invalid before doing any operation with it? Checking that the pointer is not equal to NULL did not work, obviously. I also tried this:</p>
  
  <pre><code>try
  {
      CString csClassName = typeid(*pMyPointer).name();  // Check error condition
      // The line below fails due to dangling pointer (data block is not valid).
      hr = pMyPointer-&gt;MyPointerMethod(); 
  }
  catch(bad_typeid)
  {
      return E_FAIL;
  }
  catch(...)
  {
      return E_FAIL;
  }
  </code></pre>
  
  <p>Is it the correct way?</p>