Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if object has been disposed in C# [duplicate]

Tags:

c#

.net

dispose

Possible Duplicate:
How does one tell if an IDisposable object reference is disposed?

Is there a method to check if object has been disposed different then

try {     myObj.CallRandomMethod(); } catch (ObjectDisposedException e) {     // now I know object has been disposed } 

In my case I'm using TcpClient class that has Close() method which disposes object and this can happen in piece of code I don't have control of. In this case I would like to have better solution then catching exception.

like image 994
jethro Avatar asked Aug 11 '10 10:08

jethro


People also ask

How do you check if an object is disposed or not C#?

Notice the check on _disposed . If you were to call a Dispose method implementing this pattern, you could call Dispose as many times as you wanted without hitting exceptions.

How do you know if DbContext is disposed?

You don't need to check if it's disposed or not because if your DbContext is injected by the DI system then you'll never need to dispose it yourself because it will only be disposed at the end of the controller's lifespan (i.e. after the Action completes but before the View renders.

What dispose does in C?

What Does Dispose Mean? In the context of C#, dispose is an object method invoked to execute code required for memory cleanup and release and reset unmanaged resources, such as file handles and database connections.

Is Dispose method called automatically?

Dispose() will not be called automatically. If there is a finalizer it will be called automatically. Implementing IDisposable provides a way for users of your class to release resources early, instead of waiting for the garbage collector.


2 Answers

The reliable solution is catching the ObjectDisposedException.

The solution to write your overridden implementation of the Dispose method doesn't work, since there is a race condition between the thread calling Dispose method and the one accessing to the object: after having checked the hypothetic IsDisposed property , the object could be really disposed, throwing the exception all the same.

Another approach could be exposing a hypothetic event Disposed (like this), which is used to notify about the disposing object to every object interested, but this could be difficoult to plan depending on the software design.

like image 133
Luca Avatar answered Oct 13 '22 20:10

Luca


A good way is to derive from TcpClient and override the Disposing(bool) method:

class MyClient : TcpClient {     public bool IsDead { get; set; }     protected override void Dispose(bool disposing) {         IsDead = true;         base.Dispose(disposing);     } } 

Which won't work if the other code created the instance. Then you'll have to do something desperate like using Reflection to get the value of the private m_CleanedUp member. Or catch the exception.

Frankly, none is this is likely to come to a very good end. You really did want to write to the TCP port. But you won't, that buggy code you can't control is now in control of your code. You've increased the impact of the bug. Talking to the owner of that code and working something out is by far the best solution.

EDIT: A reflection example:

using System.Reflection; public static bool SocketIsDisposed(Socket s) {    BindingFlags bfIsDisposed = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty;    // Retrieve a FieldInfo instance corresponding to the field    PropertyInfo field = s.GetType().GetProperty("CleanedUp", bfIsDisposed);    // Retrieve the value of the field, and cast as necessary    return (bool)field.GetValue(s, null); } 
like image 45
Hans Passant Avatar answered Oct 13 '22 20:10

Hans Passant