1

Suppose I have private static MyClient _myClient that opens a connection to an external server. It implements IDisposable. The 'client' is created on first use:

if (_myClient == null)
    _myClient = new MyClient (_myClientHostAddress);

I'm assuming once the above statement is executed the class loader puts the instance _myClient on the application stack.

Is this usage correct:

using (_myClient)

and if so what happens to _myClient when the using statement is out of scope?

Is _myClient marked for gc or popped directly off of the application stack by IDisposable?

If marked for garbage collection does the garbage collector pop it off the stack?

Thanks!

vcsjones
  • 138,677
  • 31
  • 291
  • 286
FoolongC
  • 25
  • 3
  • 1
    Dipose() will be called on this instance. The very purpose and implementation of using is all about "be sure to call Dispose". I do not see at all how a singleton pattern and using could be compatible. – Christopher May 04 '18 at 20:02
  • 3
    Dispose() has nothing whatsoever to do with garbage collection. – glenebob May 04 '18 at 20:04
  • 1
    1. Your object is in a field. It is on the heap, not the stack. There is nothing to "pop off". 2. it's a static field, so the object will likely never be collected unless something sets the field to something else later. – vcsjones May 04 '18 at 20:06

0 Answers0