关于使用CComPtr的一些问题(什么时候使用Release()?我可以返回CComPtr吗?,...)
我正在为 Internet Explorer(BHO)编写附加组件,并且正在使用 CComPtr 智能指针。我想知道:
- 什么时候应该使用 CComPtr.Release() 函数?
- 我可以从函数安全地返回 CComPtr 吗? I mean like this:
- 我不应该使用普通指针吗? In previous link RemoveImages private function is declared this way:
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)?
CComPtr getObjects(CComQIPtr<IHTMLDocument3> htmlDoc3) { CComPtr objects; hr = htmlDoc3->getElementsByTagName(CComBSTR(L"object"), &objects); if(SUCCEEDED(hr) && objects != NULL) { return objects; } return NULL; }
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:
- When should I use CComPtr.Release() function?
- Can I return CComPtr from a function safely?
- Should I never use normal pointer?
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)?
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; }
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第一个问题。
CComPtr::Release()
与将空指针分配给对象具有相同的效果。如果出于某种原因您想要在指针超出范围之前释放对象,您可以调用Release()(或分配空指针)。例如:您会看到,当
GetObject()
获取指针时,它会覆盖已存储在CComPtr
中的值。如果 CComPtr 存储非空指针,它将被覆盖(浅复制),并且原始指针指向的对象将被泄漏。您不需要在第一个GetObject()
之前使用Release()
- 此时指针为空。在第二个GetObject()
之后您也不需要 - 一旦指针超出范围,该对象就会被释放。第二个问题。是的,您可以返回
CComPtr
,但前提是调用者也将其接受到CComPtr
中。以下代码:不会取得该对象的所有权,因此该对象将被释放,并且
pointer
将变为悬空。这是未定义的行为。第三个问题
CComPtr
是所有权。通常,将CComPtr
作为“in”参数传递是没有意义的,除非您确切地知道这样做的原因。To the first question.
CComPtr::Release()
has the same effect as assigning a null pointer to the object. You can callRelease()
(or assign a null pointer) if for whatever reason you want to release the object before the pointer goes out of scope. For example:You see, when
GetObject()
obtains the pointer it overwrites the value already stored inCComPtr
. IfCComPtr
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 needRelease()
before the firstGetObject()
- the pointer is null at that point. And you don't need either after the secondGetObject()
- 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 intoCComPtr
. The following code: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 passingCComPtr
as an "in" parameter unless you know why exactly you do it.