CComPtr strange behaviour
In my project I have a method like this that returns instances of objects,
requested by IID, as IUnknown CComPtrs:
CComPtr<IUnknown> CSwishFolder::folder_item_object(
HWND hwnd, REFIID riid, UINT cpidl, PCUITEMID_CHILD_ARRAY apidl)
{
CComPtr<IUnknown> object;
if (riid == __uuidof(IContextMenu))
{
object = context_menu(hwnd, cpidl, apidl).p;
}
else if (riid == <some other IID>)
{
... snip
}
if (!object)
throw com_exception(E_NOINTERFACE);
return object;
}
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
invalid pointer.
Does anyone know why this behaves this way?
Many thanks.
Alex Lamaison
--
http://swish.sourceforge.net