RichTextBox memory problem

  • Thread starter Thread starter John Broderick
  • Start date Start date
J

John Broderick

I've got a problem with the RichTextBox control in .Net
where the garbage collector doesn't reclaim its memory
after I've finished with it.

My program adds a series of rtb's to the control array of a
panel to make a basic text editor. When the form that the
panel is on is closed these rtb's are still in memory.

If you want to see the probelm try this:
create a form which has a panel on it. Create a class,
myTB, which derives from RichTextBox and has a static
integer field that keeps track of the number of instances
(increment in constructor and decrement in destructor, you
can display this value somewhere on the form and set a
timer to update it). The second form has three buttons,
one (Add) adds an instance of myTB to the panel, the
second (Remove) removes myTB from the panel and the third
(Collect) calls GC.Collect().
Now click Add a couple of time to create some myTB's and
click remove to remove them, then click Collect. The count
value should stay the same.

If you want to see how it should work, try changing myTB to
derive from TextBox and try the same thing. The count will
be decremented when you force the garbage collect.

Does anyone know anymore about this or know if there is a
way to explicitly delete an object in C#. It's killing my
app but I need the richtextbox for formatting

thanks

john b
 
Hi John

When you remove myTB from the panel, are you calling Dispose? Since myTB inherits from RichTextBox, it should also implement the IDisposable interface, and call its
parent class' Dispose. This will free up resources without waiting for the GC to call myTB's finalizer.

Hope that helps
-Chris

--------------------
Content-Class: urn:content-classes:message
From: "John Broderick" <[email protected]>
Sender: "John Broderick" <[email protected]>
Subject: RichTextBox memory problem
Date: Fri, 29 Aug 2003 09:11:48 -0700
Lines: 34
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Thread-Index: AcNuSD/w+WOFRTBsT9yLoMl/CQKKoQ==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.public.dotnet.general
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:106606
NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
X-Tomcat-NG: microsoft.public.dotnet.general

I've got a problem with the RichTextBox control in .Net
where the garbage collector doesn't reclaim its memory
after I've finished with it.

My program adds a series of rtb's to the control array of a
panel to make a basic text editor. When the form that the
panel is on is closed these rtb's are still in memory.

If you want to see the probelm try this:
create a form which has a panel on it. Create a class,
myTB, which derives from RichTextBox and has a static
integer field that keeps track of the number of instances
(increment in constructor and decrement in destructor, you
can display this value somewhere on the form and set a
timer to update it). The second form has three buttons,
one (Add) adds an instance of myTB to the panel, the
second (Remove) removes myTB from the panel and the third
(Collect) calls GC.Collect().
Now click Add a couple of time to create some myTB's and
click remove to remove them, then click Collect. The count
value should stay the same.

If you want to see how it should work, try changing myTB to
derive from TextBox and try the same thing. The count will
be decremented when you force the garbage collect.

Does anyone know anymore about this or know if there is a
way to explicitly delete an object in C#. It's killing my
app but I need the richtextbox for formatting

thanks

john b


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
Thanks Chris but I've tried this already. The problem is
releasing the managed rich textbox from memory rather than
any unmanaged resources it may be using, which Dispose()
will take care of. Seeing as the rich textbox is a managed
resource I shouldn't really have to worry about freeing it
up beacuase the garbage collector should get rid of it once
there are no more references to it, but this doesn't seem
to be happening.

-----Original Message-----
Hi John

When you remove myTB from the panel, are you calling
Dispose? Since myTB inherits from RichTextBox, it should
also implement the IDisposable interface, and call its
parent class' Dispose. This will free up resources
without waiting for the GC to call myTB's finalizer.
Hope that helps
-Chris
confers no rights. Use of included script samples are
subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all
responses to this message are best directed to the
newsgroup/thread from which they originated.
 
Hi John,

I don't know what causes the problem, but the workaround is for you to
maintain a cache of RTBs. That way you can at least fix the upper limit.

Good luck,
Fergus
 
Back
Top