简体   繁体   中英

Weak References and Disposable objects

In C# it is possible to create weak references to objects as described here:

WeakReference Class

In .net some classes also implement the IDisposable interface. Calling the Dispose method of this interface is performed to manually dispose of any managed or unmanaged resources currently being held onto. An example might be a Bitmap object or class.

If I assign an object that implements IDisposable to a weak reference, will Dispose be called if the weak reference collects the object?

GC does not ever call Dispose. Dispose must be called by user code.

In general, the answer is indeed No.

However, a properly implemented class that implements IDisposable using the IDisposable pattern (hopefuly all .NET classes do this). Would also implement finalizer which is called when the object is garbage collected and inside the finalizer, it would call Dispose . So, for all proper implementations of IDisposable , the Dispose method will be called.

(Note: the counter-example by Fernando is not implementing IDisposable properly)

不,很简单;)

No. Check this test:

class Program {
        static void Main(string[] args) {
            Test test = new Test();
            Console.WriteLine(test.Disposable == null);
            GC.Collect();
            Console.WriteLine(test.Disposable == null);
            Console.ReadLine();
        }
    }

    public class Test {
        private WeakReference disposable = new WeakReference(new WeakDisposable());
        public WeakDisposable Disposable {
            get { return disposable.Target as WeakDisposable; }
        }
    }

    public class WeakDisposable : IDisposable {
        ~WeakDisposable() {
            Console.WriteLine("Destructor");
        }
        public void Dispose() {
            Console.WriteLine("Dispose");
        }
    }

The output is:

False
True
Destructor

As you can see, the execution never hits the Dispose method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM