2

I have a COM object developed in C++. This class uses another (third-party) COM object, which throws an event EvtThirdParty. This third-party object is just a method of my class.

Now I'm using my COM object from a .NET (Visual Basic for what it's worth) application, and I would like to catch EvtThirdParty from the Visual Basic application.

I'm guessing there is no trivial way to do this, but let this information here just in case someone points one. So, I have added one event (EvtThirdPartyDummy) to my COM object, and whenever it captures EvtThirdParty, raises an EvtThirdPartyDummy to VB.NET.

So far, so good.

Now, when the application receives this event, it must make a couple of calls to my object, to get some information.

Here's where my problems begin. I get a weird message about a disconnected context:

Disconnected Context was detected

Message: Context 0x1b9351e0' is disconnected. Releasing the interfaces from the current context (context 0x1b934f90). This may cause corruption or data loss. To avoid this problem, please ensure that all contexts/apartments stay alive until the application is completely done with the RuntimeCallableWrappers that represent COM components that live inside them.

I'm not ABSOLUTELY sure this is the EXACT message I get, as my Visual Studio 2005 is in Spanish, and I found this in Google, but it seems close enough. There are some differences (e.g., the title of my message says "Visual Studio 2005", IIRC, but this could be due to a different IDE version).

For what I have found in Google, it seems like the thread in which the object was created has been destroyed, but I'm pretty sure this is not the case. It is created in the main application thread.

If I remove all the code from the event handler, everything works as a charm.

My best guess is that my event is being handled in a brand new thread, but AFAIK this shouldn't be the case.

What's going on?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
raven
  • 2,574
  • 2
  • 27
  • 49

2 Answers2

3

This is a warning from a Managed Debugging Assistant, the docs are here. While it is a warning, it is not one that you should ignore if it happens during an event callback. It is most typically a threading problem, the thread that owned a COM object has exited. Or called CoUnitialize() to tear down its apartment. Same thing, an exiting thread calls CoUnitialize().

There is little in your question that really helps coming up with a decent diagnostic for it. Do consider the possibility that this 3rd party COM component might have something to do with it, like raising the event on another thread. The Debug + Windows + Threads window should help with that. And pay attention to the Output window, it shows thread termination notifications.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • It seems that your guess is correct, the event runs in another thread. There's no thread termination notifications. Now the solution must be simple, I guess... Thank you very much, Hans, your answer is very helpful, as always. – raven Nov 18 '10 at 10:33
  • Well, I have solved it in a pretty unelegant way. But solved it, nevertheless. – raven Nov 18 '10 at 16:04
2

Firstly, this is not my forte but as you haven't had any responses yet...

Events are raised on different threads so what you've said about being on a different thread sounds reasonable. You can try calling Invoke() in your event handler to get the main thread to go back and examine your object after the event has been raised.

What type of project is it and what is the main thread doing?

If this is likely to be an issue performance-wise (locking UI/???) then it may make more sense to have a different thread crate the object and just sit waiting to be informed of events. Then in the event handler, notify the creator thread that it has work to do.

As I said, I'm far from an expert on this so perhaps others will be able to give you better answers.

Basic
  • 26,321
  • 24
  • 115
  • 201
  • Thank you very much for your response. There's no UI, it's just a server that listen to the network and answers accordingly. I didn't know of Invoke, will investigate it a bit further, thank you. – raven Nov 18 '10 at 10:36
  • I can't use Invoke because I don't have UI. Damn, it would be soooo convenient... – raven Nov 18 '10 at 15:49
  • Murphy's law strikes again :) – Basic Nov 18 '10 at 18:14
  • 1
    FWIW it seems you could create your own message pump for your thread and have the equivalent of `Invoke()`... http://stackoverflow.com/questions/2443867/message-pump-in-net-windows-service – Basic Nov 18 '10 at 18:22
  • Thank you very much, Basiclife, that's great. Wish I could vote you several more times ;-) – raven Nov 19 '10 at 09:57