Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CComPtr vs. _COM_SMARTPTR_TYPEDEF macro

1,699 views
Skip to first unread message

Back 9

unread,
Apr 2, 2008, 5:59:01 PM4/2/08
to
Hello,

Some people in my group say that using _COM_SMARTPTR_TYPEDEF macro is much
safer than using CComPtr.
I am just wondering how come it is true.
What is the strenths to use the macro over CComPtr?

Back9

The March Hare [MVP]

unread,
Apr 2, 2008, 6:07:27 PM4/2/08
to
On Wed, 2 Apr 2008 14:59:01 -0700, Back 9 wrote:

> Some people in my group say that using _COM_SMARTPTR_TYPEDEF macro is much
> safer than using CComPtr.

Why do they say that? I have not heard any problems with safety with
CComPtr.

--
Please read this before replying:
1. Dshow & posting help: http://tmhare.mvps.org/help.htm
2. Trim & respond inline (please don't top post or snip everything)
3. Benefit others: follow up if you are helped or you found a solution

Alessandro Angeli

unread,
Apr 2, 2008, 6:34:04 PM4/2/08
to
From: "Back 9"

None? The macro expands to a typedef that uses MSVC's
_com_ptr_t template while CComPtr is ATL's implementation of
a smart pointer. Using the macro is a time-safer when you
use MSVC's smart pointers, especially when you need to
declare several variables of the same type, since _com_ptr_t
has a nightmarish syntax. But that doesn't make it safer
than CComPtr, which is a completely separate (and simpler)
implementation.

_com_ptr_t throws _com_error while CComPtr I think throws
CAtlException.

_com_ptr_t calls QI while CComPtr doesn't (but CComQIPtr
does).


--
// Alessandro Angeli
// MVP :: DirectShow / MediaFoundation
// mvpnews at riseoftheants dot com
// http://www.riseoftheants.com/mmx/faq.htm


The March Hare [MVP]

unread,
Apr 2, 2008, 7:02:37 PM4/2/08
to
On Wed, 2 Apr 2008 18:34:04 -0400, Alessandro Angeli wrote:

> CComPtr is ATL's implementation of
> a smart pointer.

I think this is the reason some people, like G., prefer the macro. They
don't want to introduce the ATL dependency on client's projects. I don't
think it has to do with safety.

Alessandro Angeli

unread,
Apr 2, 2008, 9:06:36 PM4/2/08
to
From: "The March Hare [MVP]"

> I think this is the reason some people, like G., prefer
> the macro. They don't want to introduce the ATL
> dependency on client's projects. I don't think it has to
> do with safety.

It's not like _com_ptr_t is plain ANSI C++ either, since it
is an MSVC++ extension of the standard library. To be "pure"
you need to bring along your own smart pointer template or
not use smart pointers. I advocate the latter, but I am a
minority :-)

Back 9

unread,
Apr 3, 2008, 8:37:02 AM4/3/08
to
Then is there any memory leak issue with CComPtr that I should be aware of?
They say there is well-known issue of CComPtr. Unfortunately they could not
search the article on the web.

Thank,
Back9

The March Hare [MVP]

unread,
Apr 3, 2008, 10:04:31 AM4/3/08
to
On Thu, 3 Apr 2008 05:37:02 -0700, Back 9 wrote:

> Then is there any memory leak issue with CComPtr that I should be aware of?

Not that I am aware of. I've been using them in my app for over six years
and haven't seen one.

> They say there is well-known issue of CComPtr. Unfortunately they could not
> search the article on the web.

I did a search and didn't see anything about memory leaks with CComPtr.

Geraint Davies

unread,
Apr 11, 2008, 4:45:49 AM4/11/08
to
On Wed, 2 Apr 2008 17:02:37 -0600, "The March Hare [MVP]"
<themar...@alice.in.wonderland> wrote:

>I think this is the reason some people, like G., prefer the macro. They
>don't want to introduce the ATL dependency on client's projects. I don't
>think it has to do with safety.

[sorry -- bit behind with replying]

Yes, that and the behaviour is slightly different. The macro version
releases the object if you take the address. This sounds like a
hideous side-effect behaviour, but in fact it works just fine
pSomething->ReturnMeAPointer(&pMySmartPtr);
If the smart pointer was not null before the call, ATL will assert but
the MSVC macro will release the pointer.

There's also a difference in the null testing which works the other
way.

G

0 new messages