RichTextBox memory problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Broderick

    RichTextBox memory problem

    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

  • Chris Lyon [MSFT]

    #2
    RE: RichTextBox memory problem

    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

    --------------------[color=blue]
    >Content-Class: urn:content-classes:message
    >From: "John Broderick" <john.broderick @snapon.ie>
    >Sender: "John Broderick" <john.broderick @snapon.ie>
    >Subject: RichTextBox memory problem
    >Date: Fri, 29 Aug 2003 09:11:48 -0700
    >Lines: 34
    >Message-ID: <0dad01c36e48$3 ff04f00$a501280 a@phx.gbl>
    >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+WOFRTBsT9yLoM l/CQKKoQ==
    >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
    >Newsgroups: microsoft.publi c.dotnet.genera l
    >Path: cpmsftngxa06.ph x.gbl
    >Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.genera l:106606
    >NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
    >X-Tomcat-NG: microsoft.publi c.dotnet.genera l
    >
    >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
    >
    >[/color]


    --

    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
    Use these online forms to report copyright and trademark infringement to Microsoft Legal. Infringement notices must comply with the Digital Millennium Copyright Act.


    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.

    Comment

    • John Broderick

      #3
      RE: RichTextBox memory problem

      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.

      [color=blue]
      >-----Original Message-----
      >Hi John
      >
      >When you remove myTB from the panel, are you calling[/color]
      Dispose? Since myTB inherits from RichTextBox, it should
      also implement the IDisposable interface, and call its[color=blue]
      >parent class' Dispose. This will free up resources[/color]
      without waiting for the GC to call myTB's finalizer.[color=blue]
      >
      >Hope that helps
      >-Chris
      >
      >--------------------[color=green]
      >>Content-Class: urn:content-classes:message
      >>From: "John Broderick" <john.broderick @snapon.ie>
      >>Sender: "John Broderick" <john.broderick @snapon.ie>
      >>Subject: RichTextBox memory problem
      >>Date: Fri, 29 Aug 2003 09:11:48 -0700
      >>Lines: 34
      >>Message-ID: <0dad01c36e48$3 ff04f00$a501280 a@phx.gbl>
      >>MIME-Version: 1.0
      >>Content-Type: text/plain;
      >>[/color][/color]
      charset="iso-8859-1"[color=blue][color=green]
      >>Content-Transfer-Encoding: 7bit
      >>X-Newsreader: Microsoft CDO for Windows 2000
      >>Thread-Index: AcNuSD/w+WOFRTBsT9yLoM l/CQKKoQ==
      >>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
      >>Newsgroups: microsoft.publi c.dotnet.genera l
      >>Path: cpmsftngxa06.ph x.gbl
      >>Xref: cpmsftngxa06.ph x.gbl[/color][/color]
      microsoft.publi c.dotnet.genera l:106606[color=blue][color=green]
      >>NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
      >>X-Tomcat-NG: microsoft.publi c.dotnet.genera l
      >>
      >>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
      >>
      >>[/color]
      >
      >
      >--
      >
      >This posting is provided "AS IS" with no warranties, and[/color]
      confers no rights. Use of included script samples are
      subject to the terms specified at[color=blue]
      >http://www.microsoft.com/info/cpyright.htm
      >
      >Note: For the benefit of the community-at-large, all[/color]
      responses to this message are best directed to the
      newsgroup/thread from which they originated.[color=blue]
      >
      >.
      >[/color]

      Comment

      • Fergus Cooney

        #4
        Re: RichTextBox memory problem

        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


        Comment

        • John Broderick

          #5
          Re: RichTextBox memory problem

          sounds like the best idea until I can get an answer for
          this. thanks fergus[color=blue]
          >-----Original Message-----
          >Hi John,
          >
          > I don't know what causes the problem, but the[/color]
          workaround is for you to[color=blue]
          >maintain a cache of RTBs. That way you can at least fix[/color]
          the upper limit.[color=blue]
          >
          >Good luck,
          >Fergus
          >
          >
          >.
          >[/color]

          Comment

          Working...