CComPtr vs _com_ptr_tWhat's the difference between CComPtr and _com_ptr_t?<br/><br/>Why isn't _com_ptr_t::Release just a no-op when the pointer is NULL?© 2009 Microsoft Corporation. All rights reserved.Wed, 08 Jul 2009 03:12:41 Z1f51e953-822f-4144-9415-cb2540a900d1http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#1f51e953-822f-4144-9415-cb2540a900d1http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#1f51e953-822f-4144-9415-cb2540a900d1Olaf van der Spekhttp://social.msdn.microsoft.com/Profile/en-US/?user=Olaf%20van%20der%20SpekCComPtr vs _com_ptr_tWhat's the difference between CComPtr and _com_ptr_t?<br/><br/>Why isn't _com_ptr_t::Release just a no-op when the pointer is NULL?Wed, 01 Jul 2009 10:29:47 Z2009-07-01T10:31:42Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#ea3d4e3e-a1dc-4782-a2f8-04b172478c3ahttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#ea3d4e3e-a1dc-4782-a2f8-04b172478c3aNibu Thomashttp://social.msdn.microsoft.com/Profile/en-US/?user=Nibu%20ThomasCComPtr vs _com_ptr_tCComPtr is an ATL class is used with using ATL COM implementations. The other one is one of the few COM support classes to support non ATL COM development IMO.<br/> <br/> The other classes similar to _com_ptr_t are<br/> <br/> _bstr_t<br/> _com_error<br/> _com_ptr_t<br/> _variant_t<br/> <br/> Answer to second part of your question from MSDN...<br/> <p>Calls <strong>IUnknown::Release</strong> on the encapsulated interface pointer, raising an <strong>E_POINTER</strong> error if this interface pointer is <strong>NULL</strong> .</p> <hr class=sig> <a href="https://mvp.support.microsoft.com/profile=1649F202-DC61-48FA-927F-F6CC62BF3A32">Microsoft MVP - Visual C++</a> <br/> <a href="http://nibuthomas.com">Blog: http://nibuthomas.com</a> <hr> Posts are provided as is without warranties or guaranties.Wed, 01 Jul 2009 10:45:49 Z2009-07-01T10:49:01Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#86637b38-b529-4fd0-bd3b-d30b5a8f838bhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#86637b38-b529-4fd0-bd3b-d30b5a8f838bOlaf van der Spekhttp://social.msdn.microsoft.com/Profile/en-US/?user=Olaf%20van%20der%20SpekCComPtr vs _com_ptr_t<p>So besides the name there's basically no difference? Nice! :(<br/><br/>&gt; raising an <strong>E_POINTER</strong> error if this interface pointer is <strong>NULL</strong> .<br/><br/>Why? A no-op in that case seems much more convenient.</p>Wed, 01 Jul 2009 10:49:29 Z2009-07-01T10:49:29Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#695994b6-0086-4ce4-8489-de2c9465231ehttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#695994b6-0086-4ce4-8489-de2c9465231eNibu Thomashttp://social.msdn.microsoft.com/Profile/en-US/?user=Nibu%20ThomasCComPtr vs _com_ptr_tAlso the definitions of these classes go along with the tlb files and probably that's why they are called as COM support classes. Also their definition can be found in comdef.h too.<br/><hr class="sig"><a href="https://mvp.support.microsoft.com/profile=1649F202-DC61-48FA-927F-F6CC62BF3A32">Microsoft MVP - Visual C++</a><br/> <a href="http://nibuthomas.com">Blog: http://nibuthomas.com</a> <hr> Posts are provided as is without warranties or guaranties.Wed, 01 Jul 2009 11:04:11 Z2009-07-01T11:04:11Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#4bbece97-4481-42b7-b492-e2e4232b3c90http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#4bbece97-4481-42b7-b492-e2e4232b3c90nobugzhttp://social.msdn.microsoft.com/Profile/en-US/?user=nobugzCComPtr vs _com_ptr_tThe IUnknown::Release() method doesn't return an HRESULT.  No way to signal an error.  Calling Release() on a null pointer is a bug in your code, not a bug in the wrapper.<br/><hr class="sig">Hans Passant.Wed, 01 Jul 2009 12:11:29 Z2009-07-01T12:11:29Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#2f2ab9e9-8a91-45f4-bfe2-cbb86ae0b79ehttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#2f2ab9e9-8a91-45f4-bfe2-cbb86ae0b79eViorel_http://social.msdn.microsoft.com/Profile/en-US/?user=Viorel_CComPtr vs _com_ptr_t<blockquote>Why isn't _com_ptr_t::Release just a no-op when the pointer is NULL?</blockquote> <br/> <p class=MsoNormal style="margin:0cm 0cm 0pt"><span style="font-size:12pt;line-height:115%" lang=EN-US><span style="font-family:Calibri">If you assign NULL or zero to a <em>_com_ptr_t</em> value, then this should work as no-op if the value is already empty. <span style="font-size:12pt;font-family:'Calibri','sans-serif'" lang=EN-US>Otherwise it acts as <em>Release</em>.</span></span></span></p>Wed, 01 Jul 2009 12:20:59 Z2009-07-01T12:20:59Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#165e3177-7e5f-401d-8c5c-5943e4a6423chttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#165e3177-7e5f-401d-8c5c-5943e4a6423cOlaf van der Spekhttp://social.msdn.microsoft.com/Profile/en-US/?user=Olaf%20van%20der%20SpekCComPtr vs _com_ptr_t<blockquote> <blockquote>Why isn't _com_ptr_t::Release just a no-op when the pointer is NULL?</blockquote> <br/> <p class=MsoNormal style="margin:0cm 0cm 0pt"><span style="font-size:12pt;line-height:115%" lang=EN-US><span style="font-family:Calibri">If you assign NULL or zero to a <em>_com_ptr_t</em> value, then this should work as no-op if the value is already empty. <span style="font-size:12pt;font-family:'Calibri','sans-serif'" lang=EN-US>Otherwise it acts as <em>Release</em>.</span></span></span></p> </blockquote> <br/>Ah, nice work around! Still, I'm curious why Release works as it does.Wed, 01 Jul 2009 12:43:37 Z2009-07-01T12:43:37Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#9dbd82e4-5df3-4ffb-921c-53087c29bedchttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#9dbd82e4-5df3-4ffb-921c-53087c29bedcGiovanni Dicaniohttp://social.msdn.microsoft.com/Profile/en-US/?user=Giovanni%20DicanioCComPtr vs _com_ptr_t<blockquote>What's the difference between CComPtr and _com_ptr_t?<br/> <br/></blockquote> <br/> One of the differences is that CComPtr is guaranteed not to throw exceptions, instead I think that _com_ptr_t can throw exceptions.<br/> <br/> Moreover, you could put CComPtr in an STL container like std::vector using CAdapt class, instead I think that you can't do that with _com_ptr_t.<br/> <br/> Giovanni<br/> <br/>Thu, 02 Jul 2009 22:14:39 Z2009-07-02T22:14:39Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#080a9e8f-5d6c-4933-a6c6-1ea3333385a2http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#080a9e8f-5d6c-4933-a6c6-1ea3333385a2Brian Muthhttp://social.msdn.microsoft.com/Profile/en-US/?user=Brian%20MuthCComPtr vs _com_ptr_tGiovanni has stated the most important differences, IMO.<br/><br/>Always, always embed _com_ptr_t code inside a try...catch construct.<br/><br/>Thu, 02 Jul 2009 22:45:38 Z2009-07-02T22:45:38Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#a4682e66-f1c8-4516-831e-e8fbb934e4c2http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#a4682e66-f1c8-4516-831e-e8fbb934e4c2Doug Harrisonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Doug%20HarrisonCComPtr vs _com_ptr_t<blockquote>Moreover, you could put CComPtr in an STL container like std::vector using CAdapt class, instead I think that you can't do that with _com_ptr_t.<br/></blockquote> IIRC, the reason for that is _com_ptr_t's funky destructive operator&amp;:<br/> <br/> <em><strong>operator&amp;</strong>    Releases any encapsulated interface pointer, replacing it with <strong>NULL</strong> , and returns the address of the encapsulated pointer. This allows the smart pointer to be passed by address to a function that has an <strong>out</strong> parameter through which it returns an interface pointer.<br/> </em> <br/> I understand the motivation, but way too cute, IMO.<br/> <br/><hr class="sig">Doug Harrison (Visual C++ MVP)Thu, 02 Jul 2009 22:54:56 Z2009-07-02T22:54:56Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#66b35087-c343-4004-b13a-adb660e845e1http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#66b35087-c343-4004-b13a-adb660e845e1OlafvdSpekhttp://social.msdn.microsoft.com/Profile/en-US/?user=OlafvdSpekCComPtr vs _com_ptr_t<blockquote> <blockquote>What's the difference between CComPtr and _com_ptr_t?<br/> <br/></blockquote> <br/> One of the differences is that CComPtr is guaranteed not to throw exceptions, instead I think that _com_ptr_t can throw exceptions.<br/> <br/> Moreover, you could put CComPtr in an STL container like std::vector using CAdapt class, instead I think that you can't do that with _com_ptr_t.<br/> <br/> Giovanni<br/> <br/></blockquote> <br/> Why do you think those two things?<br/>Fri, 03 Jul 2009 10:05:03 Z2009-07-03T10:05:03Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#cefeb72c-8d8f-4b32-90f0-5d94430e54afhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#cefeb72c-8d8f-4b32-90f0-5d94430e54afOlafvdSpekhttp://social.msdn.microsoft.com/Profile/en-US/?user=OlafvdSpekCComPtr vs _com_ptr_t<blockquote> <blockquote>Moreover, you could put CComPtr in an STL container like std::vector using CAdapt class, instead I think that you can't do that with _com_ptr_t.<br/></blockquote> IIRC, the reason for that is _com_ptr_t's funky destructive operator&amp;:<br/></blockquote> That's a very dangerous operator indeed. I guess containers are free to invoke operator&amp;, but I'm not sure they do.<br/>Fri, 03 Jul 2009 10:07:05 Z2009-07-03T10:07:05Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#ef89c1d0-2d91-43c1-b2f8-db7ff2548008http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#ef89c1d0-2d91-43c1-b2f8-db7ff2548008Giovanni Dicaniohttp://social.msdn.microsoft.com/Profile/en-US/?user=Giovanni%20DicanioCComPtr vs _com_ptr_t<blockquote> <blockquote>One of the differences is that CComPtr is guaranteed not to throw exceptions, instead I think that _com_ptr_t can throw exceptions.<br/> <br/> Moreover, you could put CComPtr in an STL container like std::vector using CAdapt class, instead I think that you can't do that with _com_ptr_t.<br/> <br/> Giovanni<br/> <br/></blockquote> <br/> Why do you think those two things?<br/></blockquote> <br/> Reading <em>_com_ptr_t</em> template class source code in <em>&lt;comip.h&gt;</em> (I'm using VS2008), you can note that in case of failures, <em>_com_issue_error</em> is called. As a consequence of that, a <em>_com_error</em> exception is thrown.<br/> <br/> Instead, if you read <em>CComPtr</em> template definition in <em>&lt;atlcomcli.h&gt;</em> , you can note that <em>CComPtr</em> methods are marked as <strong><em>throw()</em> </strong> , that means that these methods will not throw an exception.<br/> (You may want to read &quot;<a title="http://msdn.microsoft.com/en-us/library/wfa0edys(VS.80).aspx" href="http://msdn.microsoft.com/en-us/library/wfa0edys(VS.80).aspx" title="http://msdn.microsoft.com/en-us/library/wfa0edys(VS.80).aspx">Exception Specifications</a> &quot; on MSDN.)<br/> <br/> HTH,<br/> Giovanni<br/> <br/>Fri, 03 Jul 2009 16:31:59 Z2009-07-04T14:57:21Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#de87875e-5479-4721-b98f-e12e2c953395http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#de87875e-5479-4721-b98f-e12e2c953395Viorel_http://social.msdn.microsoft.com/Profile/en-US/?user=Viorel_CComPtr vs _com_ptr_t<p class=MsoNormal style="margin:0cm 0cm 0pt"><span style="line-height:115%;font-size:12pt" lang=EN-GB><span style="font-family:Calibri">I think that “operator &amp;” is offered by both of <em>_com_ptr_t</em> and <em>CComPtr</em> classes and is not a clear impediment to use STL collections (excepting collections of pointers).</span></span></p>Fri, 03 Jul 2009 18:17:55 Z2009-07-03T18:17:55Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#17288728-7e5d-4a2d-9ed1-8b15c2c7079dhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#17288728-7e5d-4a2d-9ed1-8b15c2c7079dDoug Harrisonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Doug%20HarrisonCComPtr vs _com_ptr_tThey don't have to use it, but it is a requirement for containers that &amp;t return T*, T being the type of object held by the container. See 23.1/3 in the standard and follow it to 20.1.3. In VC2008's STL, various iterators' operator-&gt; use &amp;* to obtain a pointer to the item, which is incompatible with _com_ptr_t. Other places to look include the use of placement new.<br/><hr class="sig">Doug Harrison (Visual C++ MVP)Fri, 03 Jul 2009 18:34:32 Z2009-07-03T18:34:32Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#dc4016b1-54de-4f95-8adc-9a84c729e5b4http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#dc4016b1-54de-4f95-8adc-9a84c729e5b4OlafvdSpekhttp://social.msdn.microsoft.com/Profile/en-US/?user=OlafvdSpekCComPtr vs _com_ptr_t<blockquote> <blockquote> <blockquote>One of the differences is that CComPtr is guaranteed not to throw exceptions, instead I think that _com_ptr_t can throw exceptions.<br/> <br/> Moreover, you could put CComPtr in an STL container like std::vector using CAdapt class, instead I think that you can't do that with _com_ptr_t.<br/> <br/> Giovanni<br/> <br/></blockquote> <br/> Why do you think those two things?<br/></blockquote> <br/> Reading <em>_com_ptr_t</em> template class source code in <em>&lt;comip.h&gt;</em> (I'm using VS2008), you can note that in case of failures, <em>_com_issue_error</em> is called. As a consequence of that, a <em>_com_error</em> exception is thrown.<br/> <br/> Instead, if you read <em>CComPtr</em> template definition in <em>&lt;atlcomcli.h&gt;</em> , you can note that <em>CComPtr</em> methods are marked as <strong><em>throw()</em> </strong> , that means that these methods will not throw an exception.<br/> (You may want to read &quot;<a title="http://msdn.microsoft.com/en-us/library/wfa0edys(VS.80).aspx" href="http://msdn.microsoft.com/en-us/library/wfa0edys(VS.80).aspx" title="http://msdn.microsoft.com/en-us/library/wfa0edys(VS.80).aspx">Exception Specifications</a> &quot; on MSDN.)<br/> <br/> Doug answered about the problem of using _com_ptr_t in STL containers.<br/> <br/> HTH,<br/> Giovanni<br/> <br/></blockquote> <br/> Have you looked at CComPtrBase::operator*? CComPtr can throw too it seems.<br/> Besides, doesn't _com_ptr_t only throw in case of bugs? I don't see a problem with that.<br/>Fri, 03 Jul 2009 20:34:50 Z2009-07-03T20:34:50Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#ff618017-70aa-4507-b887-b37e3e2f0cc8http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#ff618017-70aa-4507-b887-b37e3e2f0cc8Giovanni Dicaniohttp://social.msdn.microsoft.com/Profile/en-US/?user=Giovanni%20DicanioCComPtr vs _com_ptr_t<blockquote>Have you looked at CComPtrBase::operator*? CComPtr can throw too it seems.<br/> Besides, doesn't _com_ptr_t only throw in case of bugs? I don't see a problem with that.<br/></blockquote> <br/> I read the use of <em>ATLENSURE </em> inside CComPtrBase::operator* implementation, and so yes it may throw.<br/> I used to think that the ATL smart pointers like CComPtr were designed without exceptions in mind, but just considering HRESULT's as a way to signal errors.<br/> <br/> I'm not sure (I'm not an ATL expert), but probably if <strong>_ATL_NO_EXCEPTIONS</strong> is defined, no exception will be thrown in release builds, though.<br/> <br/> Note that I've never written that _com_ptr_t does a wrong thing in throwing exceptions; I just used to think that the ATL smart pointer classes like CComPtr had a different &quot;philosophy&quot; about error management, based on HRESULT's instead of exceptions; instead C++ COM support helper classes like _com_ptr_t used exceptions.<br/> (The original question was about the difference(s) between CComPtr vs. _com_ptr_t.)<br/> <br/> Note also that it seems that there are some contexts in which exceptions are not considered a good way of managing errors.<br/> For example it seems that some of the core Windows developers like Larry Osterman <a title="http://blogs.msdn.com/larryosterman/archive/2009/01/09/xkcd-tries-windows-7-and-loves-it.aspx#9308025" href="http://blogs.msdn.com/larryosterman/archive/2009/01/09/xkcd-tries-windows-7-and-loves-it.aspx#9308025" title="http://blogs.msdn.com/larryosterman/archive/2009/01/09/xkcd-tries-windows-7-and-loves-it.aspx#9308025">do not use C++ exceptions</a> in their code for particular technical reasons.<br/> <br/> So, I think that having two different kind of smart pointer classes, one HRESULT-based and non-throwing, and another one exception-based is fine. The developer can use the right tool for the right job.<br/> <br/> Giovanni<br/> <br/>Fri, 03 Jul 2009 22:00:25 Z2009-07-03T22:00:25Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#0a16f5e2-a323-4916-b80e-317421c6cdf1http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#0a16f5e2-a323-4916-b80e-317421c6cdf1OlafvdSpekhttp://social.msdn.microsoft.com/Profile/en-US/?user=OlafvdSpekCComPtr vs _com_ptr_t<blockquote>So, I think that having two different kind of smart pointer classes, one HRESULT-based and non-throwing, and another one exception-based is fine. The developer can use the right tool for the right job.<br/></blockquote> Eh, both use HRESULT for some operations. IMO the difference is *not* in the exceptions department.<br/>Sat, 04 Jul 2009 11:32:22 Z2009-07-04T11:32:22Zhttp://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#f5a510ca-4817-4e48-9df3-d7dc0d7d8476http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1f51e953-822f-4144-9415-cb2540a900d1#f5a510ca-4817-4e48-9df3-d7dc0d7d8476Giovanni Dicaniohttp://social.msdn.microsoft.com/Profile/en-US/?user=Giovanni%20DicanioCComPtr vs _com_ptr_t<blockquote>Moreover, you could put CComPtr in an STL container like std::vector using CAdapt class, instead I think that you can't do that with _com_ptr_t.<br/> <br/></blockquote> <br/> I'd like to rectify something I wrote.<br/> <br/> I've never used _com_ptr_t in a STL collection using CAdapt, but after private communication with ATL and COM expert Igor Tandetnik, it seems that it is possible.<br/> <br/> To be more precise, he pointed out that, due to broken implementation of _com_ptr_t::operator&lt;, _com_ptr_t cannot be used as a key in collections like std::map (unless custom comparison is provided), but it can be used as value.<br/> <br/> There is also a related thread here:<br/> <br/> &quot;_com_ptr_t in a C++ ordered container&quot;<br/> http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/b6bb8805-bea9-4e73-8d42-86d85da53f61<br/> <br/> Thanks,<br/> Giovanni<br/> <br/>Sat, 04 Jul 2009 15:11:52 Z2009-07-04T15:11:52Z