Solved

Disposing Dynamic Controls

Posted on 2010-09-09
4
657 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I use SQL Stored Procedure to return FedEx Zone 17 41
Vb.net dynamic formulas in runtime 11 61
Error on Add method 1 37
Close tabpage of custom control 9 23
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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now