none
Is it disposed before?

    Question

  • Hi

    I've a reference object in my hand. How can I find out if it has been disposed before or not, it has a valid reference yet?!

    Thanks,

     


    Amir
    Friday, December 23, 2011 12:40 PM

Answers

  •  

    Hi amir ,

              Another idea is

     try
    {
    //Access the reference object
    }
    catch (ObjectDisposedException ex)
     {
     // if exception of this type is catched the object is disposed

    }

     

     


    --------------------------------------------------------------------------------
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful". Happy Coding...


    • Edited by Ragu.R Friday, December 23, 2011 1:20 PM
    • Marked as answer by Amirreza Friday, December 23, 2011 4:33 PM
    Friday, December 23, 2011 1:18 PM
  • If it is not your code and it does provide any helpful property like IsDisposed then it is OK or you can handle  ObjectDisposedException  exception. I think, there is no other way.

    If it is your code and class implements IDisposable,  you can expose a property like IsDisposed (something like).

    You can check the object for its null condition but it does not mean completely that object is disposed.

    See http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx

     

    • Marked as answer by Amirreza Friday, December 23, 2011 4:33 PM
    Friday, December 23, 2011 1:44 PM
  • Instead of relying on try/catch blocks you can introduce your own *.IsDisposed property by extending the component to your own needs.

    	// 3rd party component that is lacking an IsDispose property
    	public sealed class Component : IDisposable
    	{
    		~Disposable()
    		{
    			Dispose(false);
    		}
    
    		public void Dispose()
    		{
    			Dispose(true);
    
    			GC.SuppressFinalize(this);
    		}
    
    		protected void Dispose(bool disposing)
    		{
    			if (disposing)
    			{
    				// Clean-up Managed resources
    			}
    			
    			// Clean-up Unmanaged resources
    		}
    	}
    
    	// Extending the component
    	class MyComponent : Component
    	{
    		IsDisposed { get; private set; }
    		
    		protected override void Dispose(bool disposing)
    		{
    			base.Dispose(disposing);
    			
    			IsDisposed = true;
    		}
    	}

    Now, this is just an example and because I don't really know what's the component that you're using I can't tell you exactly how to do that.

    P.s. In my opinion and experience sharing instances among threads is wrong and in most cases it tells something about the design or lack of it.


    Eyal (http://shilony.net), Regards.

    Saturday, December 24, 2011 7:26 PM
    Moderator

All replies


  • Hi Amireza,
             Check if the reference object is nothing . If so the object is disposed.
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful". Happy Coding...
    • Proposed as answer by JMCF125 Friday, December 23, 2011 12:48 PM
    Friday, December 23, 2011 12:44 PM
  •  

    Hi amir ,

              Another idea is

     try
    {
    //Access the reference object
    }
    catch (ObjectDisposedException ex)
     {
     // if exception of this type is catched the object is disposed

    }

     

     


    --------------------------------------------------------------------------------
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful". Happy Coding...


    • Edited by Ragu.R Friday, December 23, 2011 1:20 PM
    • Marked as answer by Amirreza Friday, December 23, 2011 4:33 PM
    Friday, December 23, 2011 1:18 PM
  • If it is not your code and it does provide any helpful property like IsDisposed then it is OK or you can handle  ObjectDisposedException  exception. I think, there is no other way.

    If it is your code and class implements IDisposable,  you can expose a property like IsDisposed (something like).

    You can check the object for its null condition but it does not mean completely that object is disposed.

    See http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx

     

    • Marked as answer by Amirreza Friday, December 23, 2011 4:33 PM
    Friday, December 23, 2011 1:44 PM
  • Hello,

    The real question is why would you want to know that ? generally when you're disposing an object you know that you did and calling dispose 10 thousand times should never raise an exception.

    On extreme cases where disposed can be made by dealers (that is 3rd party components) a property such as *.IsDisposed might be valuable, otherwise, it's just useless.


    Eyal (http://shilony.net), Regards.

    Friday, December 23, 2011 11:08 PM
    Moderator
  • Yes, it's the case generally but if there is a direct way to check the validity of an object, it is more easier than informing other methods about the current state of objects.

    For example in my multithread application, there are some cases which an object has disposed by a thread and after a while a callback method of another thread, that doesn't know anything about that disposition, try to use that object, so an exception rises.

    I wanted to know if there is any way to check the validity of an object reference before using it, instead of exception handling?

    As other friends said, there isn't any way except for IsDisposed which is not a general method.

    Do you know a better way?

     


    Amir
    Saturday, December 24, 2011 5:22 AM
  • Instead of relying on try/catch blocks you can introduce your own *.IsDisposed property by extending the component to your own needs.

    	// 3rd party component that is lacking an IsDispose property
    	public sealed class Component : IDisposable
    	{
    		~Disposable()
    		{
    			Dispose(false);
    		}
    
    		public void Dispose()
    		{
    			Dispose(true);
    
    			GC.SuppressFinalize(this);
    		}
    
    		protected void Dispose(bool disposing)
    		{
    			if (disposing)
    			{
    				// Clean-up Managed resources
    			}
    			
    			// Clean-up Unmanaged resources
    		}
    	}
    
    	// Extending the component
    	class MyComponent : Component
    	{
    		IsDisposed { get; private set; }
    		
    		protected override void Dispose(bool disposing)
    		{
    			base.Dispose(disposing);
    			
    			IsDisposed = true;
    		}
    	}

    Now, this is just an example and because I don't really know what's the component that you're using I can't tell you exactly how to do that.

    P.s. In my opinion and experience sharing instances among threads is wrong and in most cases it tells something about the design or lack of it.


    Eyal (http://shilony.net), Regards.

    Saturday, December 24, 2011 7:26 PM
    Moderator