I have a C++ client using a C# server (with events) via COM interop,
and it crashes on exit with an unhandled access violation in
mscorwks.dll. There are first-chance access violations in
mscorwks.dll before the crash. If the client does not attach to the
server's events, then no exceptions (first-chance or unhandled) occur.
If the client does not call CoInitializeEx with the
COINIT_MULTITHREADED option, no exceptions occur, but my client runs
in a multi-threaded apartment so this is required.
Behaviour is also affected by adding extra private member data to my
Client class along with the smart pointer to the server object, so
there appears to be memory issues. Creating ten client objects in
main is not required for the program to crash, but this makes the
crash repeatable, not sporadic.
Sample client code
==================
_ATL_FUNC_INFO OnTheEventInfo = {CC_STDCALL, VT_EMPTY, 1, {VT_BSTR}};
class Client : public IDispEventSimpleImpl<1, Client,
&DIID__DEventInterface>
{
public:
BEGIN_SINK_MAP(Client)
SINK_ENTRY_INFO(1, DIID__DEventInterface, 1, OnTheEvent,
&OnTheEventInfo)
END_SINK_MAP()
void __stdcall OnTheEvent(BSTR aData)
{
::SysFreeString(aData);
}
Client(void)
{
mspManagedServer = IManagedServerPtr(__uuidof(ManagedServer));
DispEventAdvise((IUnknown*)mspManagedServer);
}
~Client(void)
{
DispEventUnadvise((IUnknown*)mspManagedServer);
}
private:
IManagedServerPtr mspManagedServer;
// Extra data declared here will affect whether or not the program
crashes.
};
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
Client lClient1;
Client lClient2;
Client lClient3;
Client lClient4;
Client lClient5;
Client lClient6;
Client lClient7;
Client lClient8;
Client lClient9;
Client lClient10;
CoUninitialize();
return 0;
}
Sample Server Code
==================
using System;
using System.Runtime.InteropServices;
namespace ManagedServer
{
[Guid("22B65510-6E3F-4f7f-9A98-043BD41655C6")]
public interface IManagedServer
{
void Initialize();
}
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[Guid("2415AE3B-D9CB-4ad1-B7F5-2060F08F24BC")]
public interface _DEventInterface
{
[DispId(1)] void TheEvent(string msg);
}
[ComSourceInterfaces(typeof(_DEventInterface))]
[Guid("CE850092-9CCB-4073-BC0D-DC36DC1DB7BC")]
[ClassInterface(ClassInterfaceType.None)]
public class ManagedServer : IManagedServer
{
public ManagedServer(){}
[ComVisible(false)]
public delegate void MyEventTarget(string msg);
public event MyEventTarget TheEvent;
[ComVisible(false)]
public void FireTheEvent()
{
TheEvent("Message from ManagerServer");
}
public void Initialize()
{
FireTheEvent();
}
}
}
The call stack is either:
> 00fc0a18()
NTDLL.DLL!ExecuteHandler@20() + 0x26
NTDLL.DLL!_RtlDispatchException@8() + 0x69
NTDLL.DLL!_KiUserExceptionDispatcher@8() + 0xe
or
> mscorwks.dll!CtxEntry::Release() + 0x1
mscorwks.dll!IUnkEntry::Free() + 0x6d
mscorwks.dll!ComPlusWrapper::ReleaseAllInterfaces() + 0xa
mscorwks.dll!ComPlusWrapper::ReleaseAllInterfacesCallBack() + 0xae
mscorwks.dll!ComPlusWrapper::Cleanup() + 0x14
mscorwks.dll!ComPlusContextCleanupGroup::CleanUpWrappers() + 0x19
mscorwks.dll!ComPlusApartmentCleanupGroup::ReleaseCleanupGroup() +
0xe
mscorwks.dll!ComPlusApartmentCleanupGroup::ReleaseCleanupGroupCallback()
+ 0xe9
mscorwks.dll!ComPlusApartmentCleanupGroup::CleanUpWrappers() + 0x61
depending on the extra private member data. Any help or insight would
be much appreciated. This occurs developing on both Windows 2000 and
Windows XP, using Microsoft Visual Studio .NET 2003 and .NET Framework
1.1.
Thanks,
Ruth