Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 20th, 2005, 03:45 AM
Registered User
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Correctly disposing dynamically created controls

I have a user control (A) that contains children of another type of user control (B). Now I've added code in the A.Dispose() to remove the B children. The problem is that about 1 in 100 occurence of running this Windows app, I get the error of my app trying to reference a B that's already disposed. The error happens randomly, so I cannot replicate the error by doing a certain step. So I'm supposing that I'm not disposing my controls correctly.

Could someone please help tell me if there is a correct procedure of disposing of dynamically created controls? Thanks.

Old December 20th, 2005, 09:35 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma

 Omar, I'm not sure if I've been able to get in on the ground floor of this issue with the
provided information, because all I have is a Control called A and its children so
intuitively named Bs. :)

 But it could be a problem stemming from the Garbage Collection’s nature of happening at
'its own will', and even if your program has officially disposed off the objects, its at
this stage that the objects actually gets destroyed .

 I think if you just force the garbage collection once you are through with Bs you will
be more sure of when you 'dont have' those objects anymore.

 You can force the garbage collections with


Ankur Verma
Old December 20th, 2005, 08:49 PM
Registered User
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks Verma.

I suspect it's a Garbage collector problem too. That's why I wonder if I should dispose the children controls myself or just let the GC do it when the parent control gets disposed of.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help me about dynamically created controls vingo_mail ASP.NET 2.0 Basics 1 March 24th, 2008 12:54 PM
Problems with Dynamically created controls Preeti_Singh .NET Framework 1.x 1 March 7th, 2007 01:30 PM
Event creation for dynamically created controls sk80 Pro VB 6 2 February 7th, 2006 07:55 AM
Dynamically created controls katsarosj ASP.NET 1.0 and 1.1 Basics 4 February 4th, 2004 09:13 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.