Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Disposing Dynamic Controls

Posted on 2010-09-09
4
Medium Priority
?
687 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 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

879 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