CComPtr strange behaviour
In my project I have a method like this that returns instances of objects,
requested by IID, as IUnknown CComPtrs:
HWND hwnd, REFIID riid, UINT cpidl, PCUITEMID_CHILD_ARRAY apidl)
if (riid == __uuidof(IContextMenu))
object = context_menu(hwnd, cpidl, apidl).p;
else if (riid == <some other IID>)
Each method such as function calles in the if/else-if list returns a
CComPtr to the object's interface (e.g context_menu() returns a
CComPtr<IContextMenu>) and this is converted to an IUnknown by CComPtr
assignment (which, I believe, should call QueryInterface()).
Notice that those lines ends in '.p'. For some reason, without this, this
code returns invalid pointers and the program crashes when it tries to use
them! Why? This seems very strange! It's not that the QueryInterface
fails---that would just return NULL---but that it succeeds and returns an
Does anyone know why this behaves this way?