Ok, here is a complete program. First file is the form, second is the
UserControl in question.
Run it and click button1 repeatedly. Memory will keep growing and growing
with every click (500K at a time or so).
Stop it, and run it again. This time click button2 repeatedly. Memory will
grow at first, but remain steady - growing maybe 4-20K at a time - so barely
noticeable.
Thanks for your interest...
Form:
Public Class Form1
Inherits System.Windows. Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows. Forms.Button
Friend WithEvents Button2 As System.Windows. Forms.Button
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
Me.Button1 = New System.Windows. Forms.Button
Me.Button2 = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(24, 24)
Me.Button1.Name = "Button1"
Me.Button1.TabI ndex = 0
Me.Button1.Text = "Button1"
'
'Button2
'
Me.Button2.Loca tion = New System.Drawing. Point(24, 64)
Me.Button2.Name = "Button2"
Me.Button2.TabI ndex = 1
Me.Button2.Text = "Button2"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(552, 598)
Me.Controls.Add (Me.Button2)
Me.Controls.Add (Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout (False)
End Sub
#End Region
Dim curControl As UserControl
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
If Not IsNothing(curCo ntrol) Then
curControl.Disp ose()
End If
curControl = New UserControl1
End Sub
Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
If Not IsNothing(curCo ntrol) Then
Controls.Remove (curControl)
End If
curControl = New UserControl1
curControl.Loca tion = New Point(50, 50)
Controls.Add(cu rControl)
End Sub
End Class
UserControl:
Public Class UserControl1
Inherits System.Windows. Forms.UserContr ol
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() call
init()
End Sub
'UserControl1 overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
'
'UserControl1
'
Me.Name = "UserContro l1"
Me.Size = New System.Drawing. Size(392, 424)
End Sub
#End Region
Dim myData As New ArrayList
Public Sub init()
For i As Integer = 1 To 1000
Dim t As TextBox = New TextBox
t.Location = New Point(0, i * 40)
t.Text = i & "test"
Me.Controls.Add (t)
myData.Add(i & "test")
Next
End Sub
End Class
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Cor Ligthert <no**********@p lanet.nl> wrote: I thought that you would have done as you said and that that was your
start, in my opinion is this a typical question for Jon.
It seems as you said that the add and/or the remove are adding/removing
references more than only the reference to the parent.
However why would there be a reference.
I do not like this kind of problems anymore, it is following deep the
code and than reading somewhere else suddenly why this behaviour is.
Okay, I'll have a look.
Marina, could you come up with a short but complete example which
demonstrates the problem?
See http://www.pobox.com/~skeet/csharp/complete.html
(Don't worry, you don't need to write it in C# - I just want to be able
to paste your code into an empty text file and compile it from the
command line.)
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too