Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Disposing Dynamic Controls

Posted on 2010-09-09
4
Medium Priority
?
680 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 31

Accepted Solution

by:
MlandaT earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

719 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