I am using few CComVariant type variables to store interface pointers.However, sometime I need to pass interface pointer as NULL.In this case when I do this:
CComVariant vAData,vBData;
......
....
CComQIPtr<IBData> pAData = vAData.punkVal; //vAData is {0, VT_I4} when I pass NULL
CComQIPtr<IBData>pBData = vBData.punkVal; //vBData is {0, VT_I4} when I pass NULL
The first line fails and throws exception since the vAData.punkVal = 0xffffffff00000000
But the second line passes fine with no error and it has valid vBData.punkVal value(0x0000000000000000).
I am wondering why the two punkVal are different when both are NULL??
Does anybody has any idea why this is happening??
This throws exception only on 64bit machines.
CComVariantcallsVariantInit()in constructor and that setsvttoVT_EMPTY, but leavespunkValuninitialized (doesn’t make it null).What you try to do is therefore undefined behavior since you try to construct a
CComQIPtrpassing it an uninitialized pointer.If you want a
CComVariantholding a nullIUnknown*you can do this:now it’s perfectly legal to construct a
CComQIPtr: