Does `wil::com_ptr` overload operator &, aka "address of"? - c++

I found this code snippet here.
wil::com_ptr<IStream> stream;
CHECK_FAILURE(SHCreateStreamOnFileEx(
L"assets/EdgeWebView2-80.jpg", STGM_READ, FILE_ATTRIBUTE_NORMAL,
FALSE, nullptr, &stream));
According to the manual of SHCreateStreamOnFileEx, the type of the last argument is supposed to be IStream **. However the code snippet passes a wil::com_ptr<IStream> * to the function instead of a IStream **. I'm wondering how it works. Does wil::com_ptr<IStream> overload operator & (aka "address of")?
By the way, I cannot find the online manual of wil::com_ptr. Is it the same as winrt::com_ptr struct template (C++/WinRT)? Thanks.

The Windows Implementation Libraries (WIL) has its documentation published through its repository's wiki. The wil::com_ptr_t class template1 is described on the WinRT and COM wrappers page.
The section on Object management methods lists three class members that allow clients to get the address of the stored interface pointer:
T** addressof()
Returns the address of the internal pointer without releasing the current COM object. Do not use this for _Out_ parameters2 because it will leak the current COM object. For _Out_ parameters, use the & operator, which releases the current COM object before returning the address.
T** put()
Releases the current COM object and returns the address of the internal pointer. Use this method when passing the com_ptr_t as a _Out_ parameter.
T** operator&()
Same as put().
So to answer the literal question: Yes, wil::com_ptr_t does overload operator&() to return the address of the internal interface pointer after doing some housekeeping.
wil::com_ptr_t is unrelated to the winrt::com_ptr class template3, part of the C++/WinRT library. Neither of them are related to ATL's CComPtr or Visual Studio's _com_ptr_t. Those are the four official implementations, and while all of them have the same goal of automating lifetime management of COM objects they are subtly different in their implementations, with surprising consequences4.
The differences are in the following areas:
Constructors with an interface pointer argument
Implementations get to choose whether they assume ownership of the passed in interface pointer, or model shared ownership leaving the caller with the responsibility of Release()-ing the incoming interface pointer.
Assignment operators accepting an interface pointer
Same as above concerning the incoming pointer, with the added twist of having to handle the case where the instance is already holding an interface pointer. Implementations can:
Silently drop the currently held interface pointer (the fact that this is a bug doesn't mean that you won't see it)
Silently Release() the current interface prior to assigning the incoming interface pointer
Throw an exception in case the instance is holding an interface pointer
Retrieving the address of the raw interface pointer
Similar to the assignment above, the case where the smart pointer instance is already holding an interface needs to be handled, one way or another:
Silently drop the stored interface (e.g. addressof())
Silently Release() the current interface (e.g. put())
Throw an exception in case the stored pointer is not a nullptr
If you decide to use a COM smart pointer implementation, make sure you have a firm grasp on its semantics. The same literal piece of C++ code will potentially behave differently depending on the library in use.
1 wil::com_ptr is an alias template for wil::com_ptr_t with the err_policy template argument set to err_exception_policy.
2 The snippet in the question could have safely passed stream.addressof() even though ppstm is marked as an _Out_ parameter. I'm guessing that the author used operator&() instead for consistency, readability, and maintainability.
3 While the WIL and C++/WinRT are independent libraries they can interoperate with surprisingly little effort.
4 We're using a smart pointer, so we can't possibly be the source of the leak

Related

COM Reference Counting Questions

I am writing code that utilizes COM interfaces. I am basing my code on examples that I have found online. I do not want to utilize smart pointers in this case because I want to understand the basics of COM and not just have a smart pointer class do all of the work for me.
In order to frame my questions, let's assume I have a class similar to the following:
public class TestClass
{
private:
IUnknown *m_pUnknown;
public:
TestClass();
void AssignValue();
}
TestClass::TestClass()
{
m_pUnknown = NULL;
}
void TestClass::AssignValue()
{
IUnknown *pUnknown = NULL;
//Assign value to pUnknown here - not relevant to my questions
m_pUnknown = pUnknown;
pUnknown->Release();
}
Now on to my specific questions.
1) The examples I've seen to not use AddRef() when initializing a value, such as in the class constructor. Does the AddRef() happen "automatically" behind the scenes when a COM pointer is first assigned a value?
2) Although my code example does not show it, it is my understanding that in the AssignValue() method, when you assign a second value to overwrite the value of pUnknown (originally set in the class constructor), Release() is automatically called. After assigning the new value to pUnknown its reference count stands at zero. I need to call pUnknown->AddRef() immediately after the reassignment. Is my understanding correct?
Notes: I assume we are ignoring exceptions for simplicity here. If this was for real, you would want to use smart pointers to help keep things straight in the presence of exceptions. Similarly, I am not worrying about proper copying or destruction of instances of your example class or multi-threading. (Your raw pointers cannot be used from different threads as simply as you might assume.)
First, You need to make any necessary calls to COM. The only way anything might happen "automatically" behind the scenes would be if you were using smart pointers to do them.
1) The examples you refer to have to be getting their COM interface pointers from somewhere. This would be by making COM calls, e.g., CoCreateInstance() and QueryInterface(). These calls are passed the address of your raw pointer and set that raw pointer to the appropriate value. If they weren't also implicitly AddRef'ed, the reference count might be 0 and COM could delete the associated COM object before your program could do anything about it. So such COM calls must include an implicit AddRef() on your behalf. You are responsible for a Release() to match this implicit AddRef() that you instigated with one of these other calls.
2a) Raw pointers are raw pointers. Their value is garbage until you arrange for them to be set to something valid. In particular, assigning a value to one will NOT auto-magically call a function. Assigning to a raw pointer to an interface does not call Release() - you need to do that at the appropriate time. In your post, it appears that you are "overwriting" a raw pointer that had previously been set to NULL, hence there was no existing COM interface instance in the picture. There could not have been an AddRef() on something that doesn't exist, and must not be a Release() on something that isn't there.
2b)
Some of the code you indicated by a comment in your example is very relevant, but can easily be inferred. You have a local raw pointer variable, pUnknown. In the absent code, you presumably use a COM call that obtains an interface pointer, implicitly AddRefs it, and fills in your raw pointer with the proper value to use it. This gives you the responsibility for one corresponding Release() when you are done with it.
Next, you set a member raw pointer variable (m_pUnknown) with this same value. Depending on the previous use of this member variable, you might have needed to call Release() with its former value before doing this.
You now have 2 raw pointers set to the value to work with this COM interface instance and responsibility for one Release() due to 1 implicit AddRef() call. There are two ways to deal with this, but neither is quite what you have in your sample.
The first, most straightforward, and proper approach (which others have correctly pointed out & I skipped passed in the first version of this answer) is one AddRef() and one Release() per pointer. Your code is missing this for m_pUnknown. This requires adding m_pUnknown->AddRef() immediately after the assignment to m_pUnknown and 1 corresponding call to Release() "someplace else" when you are done using the current interface pointer from m_pUnknown. One usual candidate for this "someplace else" in your code is in the class destructor.
The second approach is more efficient, but less obvious. Even if you decide not to use it, you may see it, so should at least be aware of it. Following the first approach you would have the code sequence:
m_pUnknown = pUnknown;
m_pUnknown->AddRef();
pUnknown->Release();
Since pUnknown and m_pUnknown are set the same here, the Release() is immediately undoing the AddRef(). In this circumstance, eliding this AddRef/Release pair is reference count neutral and saves 2 round trips into COM. My mental model for this is a transfer of the interface and reference count from one pointer to the other. (With smart pointers it would look like newPtr.Attach( oldPtr.Detach() ); ) This approach leaves you with the original/not shown implicit AddRef() and needing to add the same m_pUnknown->Release() "someplace else" as in the first alternative.
In either approach, you exactly match AddRefs (implicit or explicit) with Releases for each interface and never go to a 0 reference count until you are done with the interface. Once you do hit 0, you do not attempt to use the value in the pointer.
Avi Berger already posted a great answer, but here is the same thing stated another way in case it helps with understanding.
In COM, reference counting is done within the COM object. The COM runtime will destruct and free an object whose reference count reaches 0. (This might be delayed by some time from the point of the count hitting 0).
Everything else is a convention. The usual convention amongst C++ COM programmers is that raw interface pointers should be treated as owning pointers. This concept means that any time a pointer points to a COM object, the pointer owns that object.
Using this terminology, the object may have multiple owners at any one time, and the object will be destroyed when nobody owns it.
However, raw pointers in C++ don't have ownership semantics built in. So you have to implement it yourself by making function calls:
Call AddRef on an interface pointer when that pointer takes ownership of an object. (You'll need to be aware of which Windows API functions or other library functions already do this, to avoid you doing it twice)
Call Release on an interface pointer when that pointer is about to stop owning an object.
The benefit of smart pointers is that they make it impossible for you to forget to call Release when an interface pointer stops owning an object. This includes the following cases:
Pointer goes out of scope.
Pointer is made to stop pointing to the object, by using assignment operator.
So, looking at your sample code. You have the pointer m_pUnknown. You want this pointer to take ownership of the object, so the code should be:
m_pUnknown = pUnknown;
m_pUnknown->AddRef();
You will also need to add code to your class destructor and your class assignment operator to call m_pUnknown->Release(). I would very strongly recommend wrapping these calls in the smallest class possible (that is, write your own smart pointer and make TestClass have that smart pointer as a member variable). Assuming of course you don't want to use an existing COM smart pointer class for pedagogical reasons.
The call pUnknown->Release(); is correct because pUnknown currently owns the object, and the pointer is about to stop owning the object due to the fact that it will be destroyed when the function block ends.
You may observe that it would be possible to remove both of the lines m_pUnknown->AddRef() and pUnknown->Release(). The code will behave exactly the same. However , it is better to follow the convention outlined above. Sticking to a convention helps yourself to avoid errors and it also helps other coders to understand your code.
To put it another way, the usual convention is to think of the pointer as having a reference count of either 0 or 1, even though the reference counting is not actually implemented that way.
First, my apologies. My attempt to simplify my code for the sake of clarity turned out to be misguided. However, I believe my questions were answered. If I may, I will summarize.
1) Any COM object that is assigned a value other than NULL needs to be immediately followed by AddRef() unless the AddRef() was implicitly handled (as is the case with some Windows API calls).
2) Any reassignment of value to a COM pointer, assuming that the "before" value is not NULL must be immediately proceeded by Release(). AddRef() would then by needed as mentioned in #1.
3) Any COM variable whose value needs to be preserved beyond its current scope requires that it have a reference count of at least 1 upon exiting its said scope. This may mean that an AddRef() is required.
Would this be a fair summary? Did I miss anything?

Related smart pointers sharing a reference count

I have a section of C++ Qt code that receives a network message and decodes it into a Google protobuf that is managed by a smart pointer. The function does some minimal structural parsing of the protobuf to see if optional message fields are present, dispatching signals if certain pieces of the message are present.
Currently, those signals contain copies of the smart pointer that contains the entire message, taking advantage of reference counting. However, I'd like to dispatch smart pointers to specific sections of the message so that the downstream handlers don't need to re-parse the entire protobuf. I cannot simply create a new smart pointer to the relevant message section because it will try to free that section when the the new pointer goes out of scope.
An attempt at illustrating this, omitting some safety checks:
void Interface::processProtobuf(QByteArray const & networkData) {
QSharedPointer<proto_message> msg(new proto_message);
msg->ParseFromArray(networkData.data(), networkData.length());
if (msg->has_configuration()) {
// This will eventually attempt to free, thus causing corruption
// of msg.
QSharedPointer<config_message> cfg(msg->mutable_configuration());
emit configurationChanged(cfg);
// I resorted to this, which forces the receiver to re-parse
// the data structure (which might be expensive for a deeply-nested
// message) to get the desired 'configuration' pointer.
emit configurationChanged(msg);
}
}
Do do this, I really need a way to create a "related" sub-pointer that inherits (and increments) the reference count on the parent pointer so that the data destructor isn't invoked until all the subs and the parent are out of scope. Is this functionality available in one of the standard smart pointer implementations, or have I created an unnecessary special case? The closest thing I've found is Qt's QSharedDataPointer<>, but I don't think it helps in the case of creating a sub-pointer.
Qt solutions aren't necessary. This was more of an academic question, as my workaround will be fine for my current case.
std::shared_ptr (or boost::shared_ptr if you don't have C++11) has a constructor which takes a shared pointer r and a pointer p. The constructed shared pointer will point to *p, but share ownership with r. This should be what you need.
The constructor's signature is
template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;

Passing a CComPtr to a function with a raw pointer prototype

I'm going through some legacy C++ code dealing with the Windows Imaging Component library and I observed this:
void setProperties(IPropertyBag2* const pBag)
{
pBag->Write(...);
}
void other_function()
{
CComPtr<IPropertyBag2> pBag;
//Code to initialize pBag
setProperties(pBag);
}
The setProperties method simply writes a bunch of properties to the property bag.
The code compiles and runs fine because I think it calls the appropriate typecasting operator.
My question is whether such an interface is recommended or is there a better way of passing the pointer.
For example, is there any difference (in terms of safety/ performance) if the signature was changed to:
void setProperties(const CComPtr<IPropertyBag2>& pBag)
Raw interface pointers are the canonical way to work with COM objects. They are also the most flexible. Using a reference to a CComPtr will tie you into using CComPtr always.
Any COM pointer, even a dumb one, is automatically a smart pointer since the object itself implements AddRef and Release. If the function isn't keeping a copy of the pointer there's no need even to worry about that.
The CComPtr type will automatically cast itself to a raw pointer for convenience.
There aren't many advantages to using a CComPtr parameter (unless it's non-const and you're going to modify it). CComPtr is more useful for local variables and instance variables.
But it's OK to do it, if only as a matter of style/consistency.

MSXML get_documentElement() & returning pointers to COM interfaces

I was wondering if it is standard practice in COM libraries to call Addref on an COM interface, that is returned from a function. For instance:
IXMLDOMElement* domElement = NULL;
document_->get_documentElement(&domElement); // does get_documentElement() call Addref on domElement?
// ...
// do something with domElement
// ..
domElement.Release(); // correct?
// (btw. member variable document_ is of type CComPtr<IXMLDOMDocument2>
or with a smart pointer:
CComPtr<IXMLDOMElement> domElement;
document_->get_documentElement(&domElement);
Btw. I found that in the docs of MSXML for "Windows media 9 series" it says that Addref is called: http://msdn.microsoft.com/en-us/library/ms751196(v=vs.85).aspx
But in the official documentation nothing is mentioned about it:
http://msdn.microsoft.com/en-us/library/ms759095(v=vs.85).aspx
The function that returns an interface pointer must call AddRef() on it before exiting, not the function that is receiving the object. The function that receives the interface pointer must use it as-is and then call Release() on it. Which means that get_documentElement() will call AddRef(), so do not call it yourself.
The rules for who - the caller or the callee - is responsible for doing what in regards to reference counting and memory management in COM are clearly defined in COM's documentation on MSDN:
The Rules of the Component Object Model
Reference Counting Rules
Yes you are supposed to addref before returning a COM object, as the caller is going to have an new interface pointer referencing the object, so the reference count needs to be increased by one. This is the rule, not the exception.
Documenting the internal addref is the exception, however, as reference counting is one of the fundamentals of COM. Probably the documentation was written when a lot of callers of this method don't know the rule and caused too many memory leaks.
When you, as a caller, no longer need the received object, you need to call Release directly or indirectly (e.g. through a class destructor), and stop using the reference pointer (many people set the pointer to null to prevent dangling pointers).

what is difference between allocating pointer object by the following 2 ways?

I have created a COM componenet named as Test.dll in that I have created an interface IDiscover.
Now i came to 2 ways of allocating the memory as
IDiscoverPtr id(__uuid(TestClass)); and Test::IDiscover *id=NULL;
What is the differnce between these two....Any Information is appreciated..
First one is a Smart Pointer and the Second one is a normal pointer.
You don't need to worry about Releasing a Smart Pointer whereas you have to explicitly Release() a normal COM Interface Pointer.
For more details on Smart Pointers, Look Here
Apart from that, the first one will try to find a GUID from registry for your Class named TestClass and will try to create an Instance of this class through CoCreateInstance API Call. It will try to get the IDiscover interface pointer through the said CoCreateInstance call and will throw an error if it fails to do so. On successful execution of the line IDiscoverPtr id(__uuid(TestClass));, you should have a valid IDiscover interface pointer in id.
The Second one is simply declaration of an Interface pointer, nothing else. You will have to instantiate it yourself through (most of the times) CoCreateInstance or one of it's variants.
The first variant is a smart pointer, the second one is a raw (ordinary pointer). The smart pointer will call IUnknown::Release() of the connected object when it itselft goes out of scope. The raw pointer will not do so and you will possibly get a memory leak unless you call IUnknown::Release() of the conected object explicitly at a suitable moment.
The first variant will try to instantiate the COM object upon its own construction (since you use the constructor parameterised with the class id) and throw an exception if that can't be done. See sources for _com_ptr_t (comip.h) for details. The second variant will give you a pointer initialized to null - not connected to any object.
Smart pointers for COM objects have a set of member function useful for instantiating objects and calling QueryInterface() - this can often reduce the amount of code needed to write. With a raw pointer you will have to call CoCreateInstance() with a handful of parameters most of which you will set to default values and this will force you to write more code. Again see the comip.h for the full sources of _com_ptr_t - they are quite readable.
The general recommendation is to use smart pointers unless you have real reasons to do otherwise.