6

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?

2
  • 2
    What do you mean by "Weakreference" collecting its objects? it is just a weak reference, i.e. the object it is pointing to may be collected by the garbage collector. In this case all you know about the garbage collector applies... Commented May 15, 2010 at 18:21
  • Like Frank said the object will just be collected by the garbage collector. This in turn will trigger the finalizer, if you have such a one. The Dispose method will never be triggered though. Commented May 15, 2010 at 18:24

4 Answers 4

8

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Actually, the "standard" finalizer only calls Dispose(bool); Dispose() is not called, so any cleanup that depends on managed code (e.g., flushing underlying file streams) cannot be done.
@StephenClearly Per the pattern Dispose(bool) is called by both the finalizer and Dispose, with neither implementing the work.
6

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

1 Comment

Your design may be better served by replacing WeakReference with RefCountDisposable from the Rx library.
2

No. Simple like that ;)

Comments

1

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.

1 Comment

Because Dispose was never called.. If a type has any managed resources the it must correctly implement a finalizer (or always be properly disposed).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.