Take the 2-minute tour ×

I've been reading about various usage advice relating to the new c++ standard smart pointers unique_ptr, shared_ptr and weak_ptr and generally 'grok' what they are about when I'm writing my own code that declares and consumes them.

However, all the discussions I've read seem restricted to this simple usage situation where the programmer is using smart in his/her own code, with no real discussion on techniques when having to work with libraries that expect raw pointers or other types of 'smart pointers' such as COM interface pointers.

Specifically I'm learning my way through C++ by attempting to get a standard Win32 real-time game loop up and running that uses Direct2D & DirectWrite to render text to the display showing frames per second.

My first task with Direct2D is in creating a Direct2D Factory object with the following code from the Direct2D examples on MSDN:

ID2D1Factory* pD2DFactory = nullptr;

pD2DFactory is obviously an 'out' parameter and it's here where I become uncertain how to make use of smart pointers in this context, if indeed it's possible. My inexperienced C++ mind tells me I have two problems:

  1. With pD2DFactory being a COM interface pointer type, how would smart_ptr work with the Add() / Release() member functions for a COM object instance?
  2. Are smart pointers able to be passed to functions in situations where the function is using an 'out' pointer parameter technique?

I did experiment with the alternative of using _com_ptr_t in the comip.h header file to help with pointer lifetime management and declared the pD2DFactory pointer with the following code:

_com_ptr_t<_com_IIID<pD2DFactory, &__uuidof(pD2DFactory)>> pD2DFactory = nullptr;

and it appears to work so far but, as you can see, the syntax is cumbersome :)

So, I was wondering if any C++ gurus here could confirm whether smart pointers are able to help in cases like this and provide examples of usage, or point me to more in-depth discussions of smart pointer usage when needing to work with other code libraries that know nothing of them. Or is it simply a case of my trying to use the wrong tool for the job? :)

share|improve this question
This probably belongs on Stack Overflow, since it's entirely a programming question that is not better answered by someone in game development. –  Jonathan Hobbs Oct 28 '12 at 0:55
"but, as you can see, the syntax is cumbersome" That's what typedefs are for. –  Nicol Bolas Oct 28 '12 at 1:12
add comment

migrated from gamedev.stackexchange.com Oct 28 '12 at 16:58


1 Answer

up vote 2 down vote accepted

You'll want to use a typedef to avoid having to repeat this error-prone declaration. Which is so common that comdef.h has a macro for it:

_COM_SMARTPTR_TYPEDEF(ID2D1Factory, __uuid(ID2D1Factory));

Now you can use:

ID2D1FactoryPtr pD2DFactory;
// etc..
share|improve this answer
Thanks for the suggestions. I was just about to write a macro for the _com_ptr_t template but that _COM_SMARTPTR_TYPEDEF in comdef.h does just the trick. My sample code is compiling and appears to be running fine. I'd be interested to know if these techniques are used in commercial games to handle DirectX COM pointer requirements? –  Darren Oct 29 '12 at 1:19
add comment

Your Answer


