Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does calling Clear disposes the items also?

Tags:

Many times there is a clear method, that removes all the items from the collections, are these items disposed also.

Like,

toolStripMenuItem.DropDownItems.Clear(); 

is sufficient, or should I have to call like that:

foreach (ToolStripItem item in toolStripMenuItem.DropDownItems) {   toolStripMenuItem.DropDownItems.Remove(item);   item.Dispose(); } 

Edit: Well ToolStripItem is an example not a question, for those who says Clear is enough I found another example, TabControl has also item collection and clear method. But TabControls can have complex controls (at least I have), which needs to be explicitly Dispose (even if they are Disposed automatically at some point by GC, cause they take huge memory). I guess the best answer is divo comment to dispose the items, and then call clear.

like image 709
Priyank Bolia Avatar asked Dec 28 '09 11:12

Priyank Bolia


People also ask

Does using call Dispose?

The using declaration calls the Dispose method on the object in the correct way when it goes out of scope. The using statement causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and can't be modified or reassigned.

Does Dispose get called automatically?

By default, the garbage collector automatically calls an object's finalizer before reclaiming its memory. However, if the Dispose method has been called, it is typically unnecessary for the garbage collector to call the disposed object's finalizer.

Does .NET GC Call Dispose?

The GC does not call Dispose , it calls your finalizer (which you should make call Dispose(false) ).

Will the Dispose () of IDisposable be called when GC runs in case the instance is not created within using block?

Answer: None. Calling Dispose can release unmanaged resources, it CANNOT reclaim managed memory, only the GC can do that.


2 Answers

Q: Does?

A: No - Clear does not dispose the items (they could be used in other parts of your application).

So, if your ToolStripItems are standard .NET ones, should Clear be sufficient? After some reflection I'd say "probably not".

Yeah, this is true that if you will have any references to the ToolStripItem in other part of your application, the .NET GarbageCollector will destroy(use the class destructor) it automatically. But, it will not call the Dispose(true) method, that is, however, required for the form's IDisposable components.

Read a propos this and this.

Actually, I believe that you will, however, need to explicitly Dispose your Items, like the ToolStrip's Dispose method does (replace this by yourToolStrip):

if (!this.Items.IsReadOnly) {     for (int i = this.Items.Count - 1; i >= 0; i--)     {         this.Items[i].Dispose();     }     this.Items.Clear(); } 

EDIT

I also created the following thread to clarify this question more generally.

like image 62
serhio Avatar answered Oct 22 '22 09:10

serhio


You should rely on Dispose() call when you're dealing with unmanaged memory, shared resources or large memory areas. Doesn't seems this case.

like image 27
Rubens Farias Avatar answered Oct 22 '22 09:10

Rubens Farias