COM Smart Pointers Revisited

Here’s my article in the February 2015 issue of MSDN Magazine:

COM Smart Pointers Revisited

After the second coming of COM, otherwise known as the Windows Runtime, the need for an efficient and reliable smart pointer for COM interfaces is more important than ever. But what makes for a good COM smart pointer? The ATL CComPtr class template has been the de facto COM smart pointer for what feels like decades. The Windows SDK for Windows 8 introduced the ComPtr class template as part of the Windows Runtime C++ Template Library (WRL), which some hailed as a modern replacement for the ATL CComPtr. At first, I also thought this was a good step forward, but after a lot of experience using the WRL ComPtr, I’ve come to the conclusion it should be avoided.

This is another example from Modern C++ for the Windows Runtime. It doesn’t play a central role in the library but comes in handy when you need to interop with other APIs not covered by the Modern compiler. You can read about one of the key building blocks of the Modern project in this article where I discuss the implementation of IUnknown and IInspectable with variadic templates.

4 thoughts on “COM Smart Pointers Revisited

  1. Paula

    I’m sending again my comments because I can not see the whole text.
    Hi, I have read your article that I found must instructive and I want to thank you.

    I think however that there is a little error in the following explanation:
    “Notice that GetAddressOf asserts that it doesn’t hold a reference before returning its address.”

    Should be :

    “Notice that GetAddressOf asserts that it does hold a reference before returning its address.”


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s