Note that there are some explanatory texts on larger screens.

plurals
  1. POSome questions about using CComPtr (when use Release()? Can I return CComPtr?, ...)
    text
    copied!<p>I writing add-on for Internet Explorer(BHO) and I'm using CComPtr smart pointers. I wonder:</p> <ol> <li> When should I use CComPtr.Release() function? </li><br> In this <a href="http://msdn.microsoft.com/en-us/library/bb250489%28VS.85%29.aspx" rel="noreferrer">this</a> link it's used to release browser object. Where else should I use it? In 'normal' use (with my own classes) I don't need it. Should I use it in this situation:<br> I get document object using m_spWebBrowser->get_Document(&spDispDoc): <pre> void STDMETHODCALLTYPE CHelloWorldBHO::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL) { // Query for the IWebBrowser2 interface. CComQIPtr spTempWebBrowser = pDisp; // Is this event associated with the top-level browser? if (spTempWebBrowser && m_spWebBrowser && m_spWebBrowser.IsEqualObject(spTempWebBrowser)) { // Get the current document object from browser... CComPtr spDispDoc; <b>hr = m_spWebBrowser->get_Document(&spDispDoc);</b> if (SUCCEEDED(hr)) { // ...and query for an HTML document. CComQIPtr htmlDoc2 = spDispDoc; <b>m_spHTMLDocument = spHTMLDoc;</b> } } } </pre> Should I release spHTMLDocument in SetSite function like I do with m_spWebBrowser (like in link mentioned before)? <li>Can I return CComPtr from a function safely?</li> I mean like this: <pre> CComPtr getObjects(CComQIPtr&lt;IHTMLDocument3&gt; htmlDoc3) { CComPtr objects; hr = htmlDoc3->getElementsByTagName(CComBSTR(L"object"), &objects); if(SUCCEEDED(hr) && objects != NULL) { return objects; } return NULL; } </pre> <li>Should I never use normal pointer?</li> In previous link RemoveImages private function is declared this way: <pre>void RemoveImages(IHTMLDocument2 *pDocument); </pre> but invoked with smart pointer: <pre> CComQIPtr&lt;IHTMLDocument2&gt; spHTMLDoc = spDispDoc; if (spHTMLDoc != NULL) { // Finally, remove the images. RemoveImages(spHTMLDoc); } </pre> I would rather write it this way: <pre>void RemoveImages(CComPtr&lt;IHTMLDocument2&gt; document2);</pre> Is it better? </ol>
 

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