Search code examples
c++formsmemorytextboxmultiline

C++ multiline textbox overflow in windows form


I have a log textbox in my program, whose contents grow bigger and bigger as long as the program runs. I didn't have time to test it myself but I was wondering how does the textbox handle tons of text. Will it run out of memory or somehow automatically free some of it? If it is not done automatically, what is the best way to handle this myself rather than just clearing the log from time to time?

Edit: I'm using Visual Studio 2013 CLR Project with Windows forms


Solution

  • A texbox will simply accept more and more text until it runs out of available memory. To mitigate that, you can set a cap in your code for how many characters/lines you choose to display. Once you reach the cap, remove older data before adding new data.

    Otherwise, if you need to display all possible text while minimizing memory usage, consider re-thinking your UI strategy.

    For example, I have an app that displays log files that may be up to multiple GB in size. I use a virtual ListView to display the text, where I keep the visible text buffered in memory, and (un)load non-visual text dynamically as the user scrolls around the ListView. That keeps memory usage down to just a few MB at most, while still providing access to the entire log.

    You might consider doing something similar. Use a file or database to store log data that the user does not immediately see, and load that data on an as-needed basis when the user needs to see it.