How to determine if a reference to an IDisposable is located?

Is there a way or some other easy way to check if the link refers to the located object?

PS - This is just curiosity (sleep well, not in production code). Yes, I know that I can catch an ObjectDisposedException when trying to access a member of an object.

+68
c # idisposable dispose
Oct 10 '08 at 16:44
source share
7 answers

It depends on whether there are IDisposable objects that let you call the Dispose method as much as you want, and there are IDisposable objects that throw an ObjectDisposedException . In this case, these objects should monitor the state (usually implemented using the private boolean field isDisposed ).

-16
Oct 10 '08 at 16:51
source share

No - the default implementation of the IDisposable template does not support it

+38
Oct 10 '08 at 16:48
source share

System.Windows.Forms.Control has the IsDisposed property, which is set to true after Dispose() is called . In your own IDisposable objects, you can easily create a similar property.

+34
Oct 10 '08 at 16:49
source share

There is nothing that will allow this. You will need to open the boolean property IsDisposed, which reflects the internal flag.

 public class SimpleCleanup : IDisposable { private bool disposed = false; public bool IsDisposed { get { return disposed; } } public SimpleCleanup() { this.handle = /*...*/; } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { // free only managed resources here } // free unmanaged resources here disposed = true; } } public void Dispose() { Dispose(true); } } 
+16
Oct 17 '08 at 8:49
source share

If this is not your class, and it does not provide the IsDisposed property (or something like that is just a convention), then you cannot find out.

But if this is your class, and you follow the canonical implementation of IDisposable , then simply set the _disposed or _isDisposed field as a property and check that.

+8
Oct 10 '08 at 16:56
source share

The Dispose method is needed to perform any cleanup necessary to fail an object; if no cleaning is required, nothing needs to be done. Requiring the object to track whether it was deleted, even if the Dispose method would otherwise do nothing, would require many IDisposable objects to add a flag for very limited benefits.

Perhaps it would be useful if IDisposable included two properties - one of which indicated whether the object was needed for disposal, and one of them indicated that the object was not useless when deleted. For objects where recycling actually does something, both values ​​will initially be true and will become false after Dispose . For objects in which recycling does not require any cleaning, the first method can always return false, and the second always, without having to store the flag anywhere. However, I do not think that they can be added to .NET.

+2
Oct 08 '15 at 16:37
source share

What I like to do is declare objects without initializing them, but set their default values ​​to Nothing . Then, at the end of the cycle, I write:

 If anObject IsNot Nothing Then anObject.Dispose() 

Here is a complete example:

 Public Sub Example() Dim inputPdf As PdfReader = Nothing, inputDoc As Document = Nothing, outputWriter As PdfWriter = Nothing 'code goes here that may or may not end up using all three objects, ' such as when I see that there aren't enough pages in the pdf once I open ' the pdfreader and then abort by jumping to my cleanup routine using a goto .. GoodExit: If inputPdf IsNot Nothing Then inputPdf.Dispose() If inputDoc IsNot Nothing Then inputDoc.Dispose() If outputWriter IsNot Nothing Then outputWriter.Dispose() End Sub 

This is also great for placing your main objects at the top of the routine, using them inside the Try procedure, and then deleting them in the Finally block:

 Private Sub Test() Dim aForm As System.Windows.Forms.Form = Nothing Try Dim sName As String = aForm.Name 'null ref should occur Catch ex As Exception 'got null exception, no doubt Finally 'proper disposal occurs, error or no error, initialized or not.. If aForm IsNot Nothing Then aForm.Dispose() End Try End Sub 
-one
Dec 07 '13 at 20:43
source share



All Articles