Pregunta

I've got a strange question,and let's start from a piece of code:

foreach (var para in paras)
{
    var specificSubject = string.Format(subject, paras);
    _listener = new Listener(Queue.Default, transport, specificSubject, new object());
    _listener.MessageReceived += OnMessage;
}

As you can see, I create a Tibco Listener object, subscribe it to some remote daemon and then, move the reference to the next Tibco Listener object I create.

I expect the Garbage Collector collects all the listener objects, except the last one, as _listener references it. However, running results suggest that ALL the listener objects are working.

So, why? Is is simply because I haven't run my application for long enough so that GC hasn't got time to collect the listener objects, or is there some inherent things that are referencing the "should-be-orphaned" listener objects?

Please enlighten me! Thanks.

¿Fue útil?

Solución

In general, you can never be sure when the garbage collector will run, so in the absence of any other data, the most likely explanation is your first guess, that the GC hasn't collected the listener yet.

My RV documentation (from release 7.2) states that

A listener object continues listening for messages until the program destroys it. The method Listener.Destroy destroys a listener explicitly, immediately canceling interest in messages. You can also destroy a listener implicitly by deleting all references to it, but the garbage collector might introduce a delay before it destroys the object and cancels interest.

(highlighting mine). So it confirms your guess.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top