0

I am debugging a larger issue but I have narrowed down to a specific scenario.
Firstly:

XamlRuntimeInitialize();

IXRApplication* pApp;
res=GetXRApplicationInstance(&pApp);

This works fine, Then:

IUnknown* pUnk;
res=pApp->QueryInterface(IID_IUnknown, (void**)&pUnk);

This executes and even returns S_OK however the address returned in pUnk is not the same as pApp(exactly 4bytes less), unexpected but technically not an issue

After That:

UINT cnt=pUnk->AddRef();

This executes and returns 0 but from this point on if I try to call pUnk->Release or pUnk->QueryInterface it crashes. If I call pUnk->Release before pUnk-AddRef it runs but again after pUnk->AddRef any call crashes. It seems as though the AddRef is actually destroying the object. The crash appears to be a null reference exception.

EDIT:

So after learning how to debug on a WEC7 Emulator I have found what looks like the issue. The assembly for AddRef() on the IUnknown looks like this

xor eax, eax
retn 0x0C

Thats all, so every call into AddRef() corrupts the stack. Its interesting because sometimes it would work, and other times not but it turns out it had more to do with the stack i.e. Stack allocated variables.

I have no idea how to work this now. I am trying to avoid a native wrapper because of portability issues, but I dont think there is any way get around this with managed code alone.

On another note what the heck was Microsoft thinking. Doesn't this violate there own rules of COM. I am having a hard time understanding how they could release code that corrupts the stack like that.

MDK
  • 495
  • 1
  • 6
  • 18
  • Firstly, it is quite possible that QueryInterface returns slightly different pointers for different interfaces. This is why the correct way to compare two COM objects for equality is to query IUnknown on both objects and then to compare the IUnknown pointers. That is described [here](https://stackoverflow.com/questions/23403777/check-com-pointers-for-equality). – Phil Jollans May 23 '18 at 06:54
  • 1
    Secondly, there is an interesting question [here](https://stackoverflow.com/questions/22209110/why-addref-returns-zero) about why AddRef might return zero. It seems that this might not be unusual if the object is a singleton. The application instance sounds like it would be a singleton, so this might apply. – Phil Jollans May 23 '18 at 06:58
  • Thirdly, why do you want to query IUnknown anyway? Do you get similar problems using the interface IXRApplication? – Phil Jollans May 23 '18 at 07:00
  • @PhilJollans I understand that the IUnknown pointer does not have to be the same as the IXRApplication, I merely was suprised considering it directly implements IUnknown, and only IUnknown. I also know the rules about AddRef return values are no way fixed. To your third point I am actually trying to Marshal IXRApplication but the .NETCF insists on Query the IUnknown and adding a reference first. – MDK May 23 '18 at 15:24

0 Answers0