Solved

Disposing Dynamic Controls

Posted on 2010-09-09
4
663 Views
Last Modified: 2012-05-10
I started running the code analysis tool in Visual Studio 2010 to try to improve my code.  One thing it's complaining about is that I'm not dispossing of some objects (specifically dynamically created controls). For example,

dim control as new Label()
containerControl.Controls.Add(control)

The code analyzer complains that not all code paths dispose control. I tried changing my code to this

using control as new Label()
   containerControl.Controls.Add(control)
end using

This satified the code analyzer complaint.  However, this seems wrong to me.  Shouldn't the control continue to exist in the containerControl.Controls list and wouldn't trying to dispose of it before page processing is done cause problems?  However, from my testing everything still seems to work fine.

I suppose another approach would be to store all dynamically created controls in an array and dispose of them in Unload().

Does anyone have any advice for this?

Thanks,
0
Comment
Question by:ScottMacMaster
  • 2
4 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 33640212
the array.unload would be good, but would be sort of high maintenance...you would have to ensure that all the controls all would need to be disposed at the same time...or that would mean leaving some controls around that aren't needed.  As long as the control implements the disposable interface,  you should use using because using will dispose of the item itself without you having to maintain it yourself.
0
 
LVL 21

Expert Comment

by:silemone
ID: 33640259
In container, you should still have a copy of the control in that container...if you iterate through the control container or get a count, it will be there...however, having one locally and having one in the array is like two copies, yes?  so if you need an instance of that label, you should be able to iterate through the ContainerControl List and get an instance of if you need it...
0
 

Author Comment

by:ScottMacMaster
ID: 33640583
I know using using is simplier.  My concern is that I'm calling Dispose() on an object after passing the object to other code would cause problems.  I was wondering if keeping my own list of the objects and waiting to Unload() would be a safe place to call Dispose()

The documentation seems pretty clear that Add() adds the instance to the list not a copy.  So there'll only be one copy of the object.  After adding the control to the control stack I have no need to reference it again.  I'm assuming objects added to the control tree will automatically be cleaned up after the page is done processing.  So I shouldn't have to explicitly Dispose() of the control, is that right?  If so, I don't think I need to call Dispose() and I just need to do things in the proper way to satify the code analyser.

Anyway, I did some more searching I think I found a solution that satifies the code analyser while still properly disposing of objects.

dim control as Label = nothing
try
   control = new Label()
   ' set control properties
  containerControl.Controls.Add(control)
   control = nothing
finally
  if control isnot nothing then
     control.Dispose()
  end if
end try

Thanks
0
 
LVL 30

Accepted Solution

by:
MlandaT earned 500 total points
ID: 33648176
I think you need ot relax that  rule in the code analyser. Whilst most of the guidance the code analyser provides is good, you need to ultimately decide what rules you want to follow. I am not aware of any need to dispose of the dynamic controls you are creating here. Once you add the control to the forms controls collection or to your container control, the form would call Dispose() on all its child controls for you when it is Disposed at the end of the service request itself.

Using the array is in fact not a good idea. You just need to add the controls to the controls collection on you container and things should be fine.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question