Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming you are looking to delete the pointer myObject and avoid memory leaks, your code can still fail to do this if there is a "return" statement in the code where you say <code>// Do something with myObject.</code> (I am assuming real code would be here)</p> <p>RAII techniques have the relevant action that is equivalent to a "finally" block, in a particular object's destructor:</p> <pre><code>class ResourceNeedingCleanup { private: void cleanup(); // action to run at end public: ResourceNeedingCleanup( /*args here*/) {} ~ResourceNeedingCleanup() { cleanup(); } void MethodThatMightThrowException(); }; typedef boost::shared_ptr&lt;ResourceNeedingCleanup&gt; ResourceNeedingCleanupPtr; // ref-counted smart pointer class SomeObjectThatMightKeepReferencesToResources { ResourceNeedingCleanupPtr pR; void maybeSaveACopy(ResourceNeedingCleanupPtr&amp; p) { if ( /* some condition is met */ ) pR = p; } }; // somewhere else in the code: void MyFunction(SomeObjectThatMightKeepReferencesToResources&amp; O) { ResourceNeedingCleanup R1( /*parameters*/) ; shared_ptr&lt;ResourceNeedingCleanup&gt; pR2 = new ResourceNeedingCleanup( /*parameters*/ ); try { R1.MethodThatMightThrowException(); pR2-&gt;MethodThatMightThrowException(); O-&gt;maybeSaveACopy(pR2); } catch ( /* something */ ) { /* something */ } // when we exit this block, R1 goes out of scope and executes its destructor // which calls cleanup() whether or not an exception is thrown. // pR2 goes out of scope. This is a shared reference-counted pointer. // If O does not save a copy of pR2, then pR2 will be deleted automatically // at this point. Otherwise, pR2 will be deleted automatically whenever // O's destructor is called or O releases its ownership of pR2 and the // reference count goes to zero. } </code></pre> <p>I think I have the semantics correct; I haven't used shared_ptr much myself, but I prefer it to auto_ptr&lt;> -- a pointer to an object can only be "owned" by one auto_ptr&lt;>. I've used COM's <a href="http://msdn.microsoft.com/en-us/library/aa266806(VS.60).aspx" rel="nofollow noreferrer">CComPtr</a> and a variant of it that I've written myself for "regular" (non-COM) objects that is similar to shared_ptr&lt;> but has Attach() and Detach() for transfer of pointers from one smart pointer to another.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload