Solved

Disposing Dynamic Controls

Posted on 2010-09-09
4
653 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

16 Experts available now in Live!

Get 1:1 Help Now