Click to See Complete Forum and Search --> : Reference count using C# .NET COM object


Wavemachine
March 11th, 2003, 07:24 AM
I have a problem when using com interop and trying to do any UI work using Windows forms or even a message box.

The COM object is a class library written in C# using Visual studio 7. The Client is an MFC Application written using VC 6.0.

All is fine untill I exit the App and then I get an assertion on Line 52 of CmdTarget.cpp which is a reference counting problem.

If I comment out the message box in the C# code the assert does not appear, similarly if I force a garbage collect all is okay.

The problem with the client does not occur if I remove the AfxOleInit an do a CoInitialize instead however our main project does need the OLE DLL's loaded to work correctly with the libraries it uses.

Any idea's will be appreciated as this is getting very critical now.

I have attached a sample that will illustrate the problem.

Thanx

Dave

Wavemachine
March 13th, 2003, 10:20 AM
Myself and a colleague have looked into this in quite a bit of depth and have since found other postings with a similar problem.

It seems the reference count is reduced but the object is not released straight away. A System.GC.Collect() at the end of the function works around the problem but obviously not ideal.

I Have tested it with VC7.1 beta and it still occurs with that so we will be raising this as a support incident with Microsoft.

Dave