关于使用CComPtr的一些问题(什么时候使用Release()?我可以返回CComPtr吗?,...)

发布于 2024-10-05 02:40:31 字数 2086 浏览 8 评论 0原文

我正在为 Internet Explorer(BHO)编写附加组件,并且正在使用 CComPtr 智能指针。我想知道:

  1. 什么时候应该使用 CComPtr.Release() 函数?

  2. In this this 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:
    I get document object using m_spWebBrowser->get_Document(&spDispDoc):
    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;
            hr = m_spWebBrowser->get_Document(&spDispDoc);
            if (SUCCEEDED(hr))
            {
                // ...and query for an HTML document.
                CComQIPtr htmlDoc2 = spDispDoc;
                m_spHTMLDocument = spHTMLDoc;
            }
        }
    
    }
    
    Should I release spHTMLDocument in SetSite function like I do with m_spWebBrowser (like in link mentioned before)?
  3. 我可以从函数安全地返回 CComPtr 吗?
  4. I mean like this:
    CComPtr getObjects(CComQIPtr<IHTMLDocument3> htmlDoc3)
    {
     CComPtr objects;
     hr = htmlDoc3->getElementsByTagName(CComBSTR(L"object"), &objects);
     if(SUCCEEDED(hr) && objects != NULL)
     {
      return objects;
     }
     return NULL;
    }
    
  5. 我不应该使用普通指针吗?
  6. In previous link RemoveImages private function is declared this way:
    void RemoveImages(IHTMLDocument2 *pDocument); 
    but invoked with smart pointer:
    CComQIPtr<IHTMLDocument2> spHTMLDoc = spDispDoc;
    if (spHTMLDoc != NULL)
    {
     // Finally, remove the images.
     RemoveImages(spHTMLDoc);
    }
    
    I would rather write it this way:
    void RemoveImages(CComPtr<IHTMLDocument2> document2);
    Is it better?

I writing add-on for Internet Explorer(BHO) and I'm using CComPtr smart pointers. I wonder:

  1. When should I use CComPtr.Release() function?
  2. In this this 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:
    I get document object using m_spWebBrowser->get_Document(&spDispDoc):

    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;
            hr = m_spWebBrowser->get_Document(&spDispDoc);
            if (SUCCEEDED(hr))
            {
                // ...and query for an HTML document.
                CComQIPtr htmlDoc2 = spDispDoc;
                m_spHTMLDocument = spHTMLDoc;
            }
        }
    
    }
    

    Should I release spHTMLDocument in SetSite function like I do with m_spWebBrowser (like in link mentioned before)?

  3. Can I return CComPtr from a function safely?
  4. I mean like this:

    CComPtr getObjects(CComQIPtr<IHTMLDocument3> htmlDoc3)
    {
     CComPtr objects;
     hr = htmlDoc3->getElementsByTagName(CComBSTR(L"object"), &objects);
     if(SUCCEEDED(hr) && objects != NULL)
     {
      return objects;
     }
     return NULL;
    }
    
  5. Should I never use normal pointer?
  6. In previous link RemoveImages private function is declared this way:

    void RemoveImages(IHTMLDocument2 *pDocument); 

    but invoked with smart pointer:

    CComQIPtr<IHTMLDocument2> spHTMLDoc = spDispDoc;
    if (spHTMLDoc != NULL)
    {
     // Finally, remove the images.
     RemoveImages(spHTMLDoc);
    }
    

    I would rather write it this way:

    void RemoveImages(CComPtr<IHTMLDocument2> document2);

    Is it better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2024-10-12 02:40:31

对于第一个问题。 CComPtr::Release() 与将空指针分配给对象具有相同的效果。如果出于某种原因您想要在指针超出范围之前释放对象,您可以调用Release()(或分配空指针)。例如:

CComPtr<ISomeInterface> pointer;
HRESULT hr = firstProvider->GetObject( &pointer );
if( SUCCEEDED( hr ) ) {
   //use the object
   pointer.Release();
}
HRESULT hr = secondProvider->GetObject( &pointer );
if( SUCCEEDED( hr ) ) {
   //use the object
}

您会看到,当 GetObject() 获取指针时,它会覆盖已存储在 CComPtr 中的值。如果 CComPtr 存储非空指针,它将被覆盖(浅复制),并且原始指针指向的对象将被泄漏。您不需要在第一个 GetObject() 之前使用 Release() - 此时指针为空。在第二个 GetObject() 之后您也不需要 - 一旦指针超出范围,该对象就会被释放。

第二个问题。是的,您可以返回 CComPtr,但前提是调用者也将其接受到 CComPtr 中。以下代码:

ISomeInterface* pointer = YourFunctionReturningCComPtr();

不会取得该对象的所有权,因此该对象将被释放,并且pointer将变为悬空。这是未定义的行为。

第三个问题 CComPtr 是所有权。通常,将 CComPtr 作为“in”参数传递是没有意义的,除非您确切地知道这样做的原因。

To the first question. CComPtr::Release() has the same effect as assigning a null pointer to the object. You can call Release() (or assign a null pointer) if for whatever reason you want to release the object before the pointer goes out of scope. For example:

CComPtr<ISomeInterface> pointer;
HRESULT hr = firstProvider->GetObject( &pointer );
if( SUCCEEDED( hr ) ) {
   //use the object
   pointer.Release();
}
HRESULT hr = secondProvider->GetObject( &pointer );
if( SUCCEEDED( hr ) ) {
   //use the object
}

You see, when GetObject() obtains the pointer it overwrites the value already stored in CComPtr. If CComPtr 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 Release() before the first GetObject() - the pointer is null at that point. And you don't need either after the second GetObject() - the object will be relased once the pointer goes out of scope.

To the second question. Yes, you can return CComPtr but only if the caller also accepts it into CComPtr. The following code:

ISomeInterface* pointer = YourFunctionReturningCComPtr();

will not take ownership of the object, so the object will be released and pointer will become dangling. That's undefined behavior.

To the third question CComPtr is for ownership. Usually there's no point in passing CComPtr as an "in" parameter unless you know why exactly you do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文