views:

24

answers:

3

Since WebControls inherit from Control which implement IDisposable. Is it necessary to call Dispose or wrap these WebControls in using statements to prevent memory leaks or does ASP.NET automatically handle this?

A: 

ASP.NET handles it. It has to since Disposing a control that is attached to the control tree will cause unexpected consequences. You don't know when it is truly done with the tree, so it's best to let ASP.NET get rid of it when it's done.

Now if you don't attach it to the control tree, that is something different, but I would guess that is a real outside case.

Kevin
Thats what i thought. Thanks Kevin!
Brian
+1  A: 

Only dispose what you create. You don't create the control instances, the ASP.NET runtime does, therefore you should not interfere with their lifetime.

Christian Hayter
A: 

Most of the time there is no need to explicitly call the Dispose() method of an object. The GC will do it for you when it needs to.

It may be advisable to do so in heavily memory-intensive processes in order to speed up the GC's work, but otherwise it will be called regardless, without your intervention, at some point.

rakuo15