Freeing a Pointer if memory isn't being referenced by anything else
<p>I have a method that has a few pointers as parameters.  This method can be called with either named pointers from the callee or dynamically create a pointer to a new object and pass it in as an argument directly as the method is being called.</p>

<pre><code>myClass *myPtr = new myClass(...);
myMethod(myPtr);
</code></pre>

<p>Verus</p>

<pre><code>myMethod(new myClass(...));
</code></pre>

<p>The problem is that if both of these are valid options, how does one properly free the passed in pointer?  Deleting myPtr within myMethod will cause a crash if myPtr is ever accessed again within the program.  If I don't delete myPtr, the second option will cause a memory leak if it is used.  There are benefits for using both options so both shouldn't break the program.</p>

<p>Aside from using STL, what are some solutions to this problem?  Would I have to implement my own garbage collector?</p>
