title: When to delete pointer in try-catch block
body: <p>Quick best-practice question (note I am not allowed to use any smart pointers in this code). I am under the impression that if I pass a pointer to a function and an exceptional case occurs, then the memory is leaked if it's never deleted in either the called function or the function in which the memory was first allocated. Is it safe to delete the memory in the catch blocks or should I delete the memory in the calling function? </p>
  
  <p>Example: </p>
  
  <pre><code>int main() {
    object* myPtr = new object(); //dynamic memory on heap
    foo(*myPtr);  //pass pointer to function foo
  
    return 0;
  }
  
  void foo(object &amp;pointer) {
    try {
      /* do stuff here
      with the pointer */
    }
    catch (const char &amp;e) {
      cout&lt;&lt; "An error occured: " &lt;&lt; e &lt;&lt; endl;
    }
    catch (...)
      cout&lt;&lt; "Caught unknown exception." &lt;&lt; endl;
    }
  }
  </code></pre>
  
  <p>Should I delete the pointer after the function returns? </p>
  
  <pre><code> int main() {
        object* myPtr = new object(); //dynamic memory on heap
        foo(*myPtr);  //pass pointer to function foo
        delete myPtr;
  
        return 0;
      }
  </code></pre>
  
  <p>Or in the try-catch blocks? </p>
  
  <pre><code>void foo(object &amp;pointer) {
        try {
          /* do stuff here
          with the pointer */
        }
        catch (const char &amp;e) {
          cout&lt;&lt; "An error occured: " &lt;&lt; e &lt;&lt; endl;
          delete &amp;pointer;
        }
        catch (...)
          cout&lt;&lt; "Caught unknown exception." &lt;&lt; endl;
          delete &amp;pointer;
        }
      }
  </code></pre>