1

I'm using RabbitMQ.Client for .NET. When I create connection and I don't stop it by using Dispose or Close methods then the connection obviously doesn't stop by itself. But the Main method is blocking the thread and the console doesn't get closed.

Why is that happening? Why the Finalizer method of class Foo doesn't get called too? I thought I could use Finalizers to close this connection if the user forgets to Dispose but as far as I can see it's not working this way. Is this a programming error inside this library, or is that expected and if it is, then why is that?

I also tried running GC.Collect() and stuff like that in various combinations, but it didn't help me.

My main question is - is it possible to pack the IConnection inside some kind of a supervisor which would Close it if necessary?

class Foo
{
    private IConnection _connection;

    public Foo()
    {
        var factory = new ConnectionFactory
        {
            HostName = "rancher.off",
            Port = 6672
        };

        _connection = factory.CreateConnection();
    }

    ~Foo()
    {
        System.Diagnostics.Trace.WriteLine("Foo's destructor is called.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        var t = new Foo();
        t = null;

        // GC.Collect(); <--- It seems this helps! Why? Why isn't GC called after getting out of the Main function?
    }
}
Kelu Thatsall
  • 2,494
  • 1
  • 22
  • 50
  • Just wanting to clarify: You mentioned "I also tried running GC.Collect() and stuff like that in various combinations, but it didn't help me.". However, in the code you have a comment: // GC.Collect(); <--- It seems this helps! Why? Why isn't GC called after getting out of the Main function? Is the destructor getting called when you include GC.Collect()? – David Glass Aug 02 '16 at 12:50
  • Thanks for response. Well I edited the code later, cause I checked and it seems in this configuration running GC manually works. Well I think I already figured out what's going on here (GC only gets called automatically when it's needed cause of too much memory used for instance), but I still don't know why the thread is blocked if I don't call garbage collection. The destructor is called if I include `GC.Collect()` indeed. GC isn't called automatically after leaving `Main` function, but I guess this is how it's supposed to be. – Kelu Thatsall Aug 02 '16 at 17:15
  • I ran across this on SO: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238 which explains in detail what I believe is happening. I have had fits myself with this and when there is a condition where finalizer is called it can happen much later than you would expect due to the garbage collector thread. Basic lesson they are trying to state is what you have already come up with though -- call Dispose() explicitly or use 'using' statement when you can so that objects are explicitly removed. – David Glass Aug 03 '16 at 00:20
  • @DavidGlass thanks, I already have seen that topic :) I learned a lot from it too, but I still have doubts why isn't the program stopped when I leave `Main` function. I mean I can guess it's because the connection is still open, but it seems like this is kind of a deadlock-ish situation – Kelu Thatsall Aug 03 '16 at 08:38

0 Answers0