Re: CComPtr strange behaviour
On Mon, 27 Jul 2009 08:56:31 -0400, Igor Tandetnik wrote:
What happens to the pointer _after_ folder_item_object returns? I bet
the caller just casts it to the expected interface type, without using
QI.
Ah, indeed:
CFolder::GetUIObjectOf(..., REFIID riid, void** ppv) {
CComPtr<IUnknown> object;
object = folder_item_object(..., riid);
*ppv = object.Detach();
}
GetUIObjectOf takes an IUnknown* pointer it got from folder_item_object,
and tells the caller that it's really a pointer of type riid (which
amounts to a downcast). Due to a happy coincidence of implementation
details, this does happen to be the case when you use ".p" form in
folder_item_object, but not when you use assignment without ".p".
Aha! I think I see what you mean: so you're saying my implementation of
GetUIObjectOf has basically lied to the caller? The caller said "hey
CFolder, pass me a pointer to some object where the _pointer_ is of type
riid" in other words it wants a guarantee that it is safe to cast the
void** pointer without a QI? And I've lied to it by passing a pointer of
type IUnknown.
If my understanding is correct, this is the proper way to do it:
CFolder::GetUIObjectOf(..., REFIID riid, void** ppv) {
CComPtr<IUnknown> object;
object = folder_item_object(..., riid);
return object->QueryInterface(riid, ppv);
}
Is that right? This highlights to me the absence of type safety in a
void** out-param. Out of curiosity, why do these functions ask for void**
rather than IUnknown**?
Thanks for your help and wisdom Igor :)
Alex