We have an unmanaged COM server application that aggregates a .NET component (which is exported to COM). Amongst some of our own interfaces, this .NET component is also responsible for the IDispatch interface.
Running our code with the beta2 framework now has a refcounting issue that was not there with any of the previous .NET frameworks, incl the beta1.
Say the unmanaged server class is called CUnmanaged and the aggregated .NET component implements a managed interface IManaged (which is also exported to COM)
1) behaviour in the old frameworks:
<clientcode>
{
CComPtr<IUnknown> pIUnk;
pIUnk.CoCreateInstance(__uuidof(CUnmanaged),0, CLSCTX_LOCAL_SERVER);
IDispatch* pIDispatch;
pIUnk->QueryInterface(__uuidof(IDispatch), (void**)&pIDispatch);
pIDispatch->Release();
}
</clientcode>
Note: The QI to IDispatch will start up the .NET framework and load the aggregated .NET component. The refcount on the unmanaged server object (CUnmanaged) is incremented by 1.
The release on the dispatch pointer will cause the aggregated .NET component to be destroyed, the refcount of the unmanaged server object is decremented and the servercod ethat monitors the refcount will shut down the server.
2) behaviour with the new beta2 framework
Running above client code now has a refcounting issue. The QI to IDispatch will increment the refcount of the unmanaged server object by 2(!). However, when the aggregated .NET object is destroyed, the refcout is only decremented by 1. Thus, our refcount monitor will think somebody is still using the server and not shut down the local server.
3) Additional evidence.
This problem does NOT occur when the client code does a direct QI to the IManaged interface instead of doing a QI to IDispatch. In this case, the refcount in the unmanaged server is only incremented by 1 and thus the automatic shutdown
of the server works fine.
What is going wrong here What is special about making a QI to IDispatch in contrast to a QI to our own interface Could this be a bug in the CCW of the aggregated .NET component with the beta2 framework Are we doing something illegal here Any ideas are mostly welcome.
Thanks
Joerg

Refcounting issue in COM-.NET interop with beta2 framework
mikeBearb
We will continue to investigate the bug and will post all updates and findings at that site.
Thanks,
Christopher Eck [MS]