Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>To the first question.</strong> <code>CComPtr::Release()</code> has the same effect as assigning a null pointer to the object. You can call <code>Release()</code> (or assign a null pointer) if for whatever reason you want to release the object <em>before</em> the pointer goes out of scope. For example:</p> <pre><code>CComPtr&lt;ISomeInterface&gt; pointer; HRESULT hr = firstProvider-&gt;GetObject( &amp;pointer ); if( SUCCEEDED( hr ) ) { //use the object pointer.Release(); } HRESULT hr = secondProvider-&gt;GetObject( &amp;pointer ); if( SUCCEEDED( hr ) ) { //use the object } </code></pre> <p>You see, when <code>GetObject()</code> obtains the pointer it overwrites the value already stored in <code>CComPtr</code>. If <code>CComPtr</code> stores a non-null pointer it will just be overwritten (shallow copy) and the object pointed to by the original pointer will be leaked. You don't need <code>Release()</code> before the first <code>GetObject()</code> - the pointer is null at that point. And you don't need either after the second <code>GetObject()</code> - the object will be relased once the pointer goes out of scope.</p> <p><strong>To the second question</strong>. Yes, you can return <code>CComPtr</code> but only if the caller also accepts it into <code>CComPtr</code>. The following code:</p> <pre><code>ISomeInterface* pointer = YourFunctionReturningCComPtr(); </code></pre> <p>will not take ownership of the object, so the object will be released and <code>pointer</code> will become dangling. That's undefined behavior.</p> <p><strong>To the tird question</strong> <code>CComPtr</code> is for ownership. Usually there's no point in passing <code>CComPtr</code> as an "in" parameter unless you know why exactly you do it.</p>
    singulars
    1. This table or related slice is empty.
    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. 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