How to check if an object has been deleted in C #

Possible duplicate:
How to determine if a link to IDisposable is set?

Is there any way to check if the object was deleted by other than

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

In my case, I am using the TcpClient class, which has a Close() method that provides an object, and this can happen in a piece of code in which I do not control. In this case, I would like to have a better solution, and then catch the exception.

+43
c # dispose
Aug 11 '10 at
source share
4 answers

A good way is to output 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 will not work if another code instantiated. Then you have to do something desperate, like using Reflection, to get the value of the private member m_CleanedUp. Or catch the exception.

Honestly, none of them are likely to achieve good results. You really wanted to write a TCP port. But you will not, this buggy code that you cannot control now controls your code. You have increased the impact of the error. Talking with the owner of this code and working on something is definitely the best solution.

+27
Aug 11 '10 at 11:05
source share

A reliable solution catches an ObjectDisposedException.

The solution for writing an overridden implementation of the Dispose method does not work, because there is a race condition between the Dispose call method and the one that accesses the object, after checking the hypothetical IsDisposed property, the object can be really deleted, throwing an exception anyway.

Another approach could be to disclose a hypothetical Disposed event (such as this ), which is used to notify a disposal object for each object of interest, but this can be difficult to plan depending on the software design.

+15
Aug 11 '10 at 11:07
source share

If you are not sure whether the object was deleted or not, you should call the Dispose method itself, not methods like Close . Although the structure does not guarantee that the Dispose method must be executed without exception, even if the object was previously deleted, it is a common template and, as far as I know, is implemented on all disposable objects within the framework.

Typical template for Dispose , according to Microsoft :

 public void Dispose() { Dispose(true); // Use SupressFinalize in case a subclass // of this type implements a finalizer. GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { // If you need thread safety, use a lock around these // operations, as well as in your methods that use the resource. if (!_disposed) { if (disposing) { if (_resource != null) _resource.Dispose(); Console.WriteLine("Object disposed."); } // Indicate that the instance has been disposed. _resource = null; _disposed = true; } } 

Check out the _disposed check. If you called the Dispose method that implements this template, you could call Dispose as many times as you like without being thrown into exceptions.

+13
Aug 11 '10 at
source share

Best practice is to implement it yourself using a local logical field: http://www.niedermann.dk/2009/06/18/BestPracticeDisposePatternC.aspx

0
Aug 11 '10 at
source share



All Articles