Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does IsDisposed return false after calling Dispose()?

I am trying to clear all items from a ToolStripDropDownButton. Since they are disposable, I call the dispose method on each of them. But I see that after calling the dispose() method, the IsDisposed property still returns false. Why is that and how can I check if Dispose() is called on any object ? It is not a problem (I hope) in my current project, but I would really like to know what is going on here...

my code so far :

private void ClearDropDownAccessConnections()
{
    ToolStripItem button = null;

    for (int i = toolStripDropDownButtonAccess.DropDownItems.Count - 1; i > 0; i--)
    {
        button = toolStripDropDownButtonAccess.DropDownItems[i] as ToolStripItem;
        if ((button.Tag != null) && ((int)button.Tag == 10))
        {
            toolStripDropDownButtonAccess.DropDownItems.Remove(button);
            button.Dispose();
            //IF I CHECk HERE THEN button.IsDisposed IS STILL FALSE                }
        }
    }
like image 502
GuidoG Avatar asked May 07 '14 10:05

GuidoG


People also ask

What will happen if we call Dispose () method directly?

The Dispose() methodThe Dispose method performs all object cleanup, so the garbage collector no longer needs to call the objects' Object. Finalize override. Therefore, the call to the SuppressFinalize method prevents the garbage collector from running the finalizer. If the type has no finalizer, the call to GC.

Does Dispose get called automatically C#?

Disposing with using-block We can make rule that disposable objects must be always defined within using-block like shown in the following example. Now Dispose() is automatically called in the end of using-block.

When dispose method is called?

It only occurs when there are objects in the Finalization Queue. It only occurs when a garbage collection occurs for Gen2 (which is approx 1 in every 100 collections for a well-written .

WHO calls Dispose method C#?

NET Garbage Collector (GC). Before the GC deallocates the memory, the framework calls the object's Finalize() method, but developers are responsible for calling the Dispose() method. The two methods are not equivalent.


1 Answers

For whatever their reason, the original .NET developers have decided to flip the IsDisposed flag only if the disposed ToolStripItem has non-null Owner property (which you're kindly indirectly setting to null line before). This doesn't seem to have any further impact - i.e. you can assume the ToolStripItem is safely disposed despite this weird behaviour.

As for your broader question - the IDisposable interface does not provide any way of checking if the object was disposed (and to make matters worse - classes implementing it do not have to guarantee an exception-free execution if it's called more than once (see MSDN)). You need to rely on the developers of said classes to provided some info if the object was actually disposed (which as can be seen in the ToolStripItem example is not a fool proof method), or track this somehow in your code.

Having said all that, it rarely becomes a problem in real-life scenarios (though it's useful to be aware of it).

like image 194
decPL Avatar answered Oct 11 '22 07:10

decPL