title: Should I delete local pointers in functions? (C++)
body: <p>A question:</p>
  
  <p>Should I delete pointers which are fetched in functions (not created, just fetched)? Example:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;SomeObject&gt;
  
  #define SAFE_DELETE(p) { if (p) { delete (p); (p) = NULL; } }
  
  class DraftObject
  {
  public:
  
      DraftObject() : _x(0) {}
      ~DraftObject(){}
  
      int CalculateSomething()
      {
          AnotherObject* aObj = SomeObject::getInstance()-&gt;getAObjPointer();
  
          /* Do some calculations and etc... */
          _x += aObj-&gt;GetSomeIntValue();
  
          SAFE_DELETE(aObj) // &lt;-- Would you recomend this here?
  
          return _x;
      }
  
  protected:
      int _x;
  };
  </code></pre>
  
  <p>The aObj would be re-used in other cases as well in the SomeObject instance. I could go on and always call <code>SomeObject::getInstance()-&gt;getAObjPointer()</code> for everything I need it for, but <code>SomeObject::getInstance()-&gt;getAObjPointer()-&gt;GetSomeIntValue()</code> is not as readable as <code>aObj-&gt;GetSomeIntValue()</code> in my personal opinion.
  I know that I would not need to worry if I used something from boost (shared_ptr, weak_ptr or even auto_ptr), but I am more curious about the way this works. Will not deleting the pointer create a memory leak situation or will the deletion of the pointer delete it from the memory so that it will be gone in other scopes (the instance object as well as anywhere else it might be used) ?</p>
  
  <p>Any thoughts? </p>
  
  <p>Cheers.</p>