From 80714a0295df71d6dee35182d221648680f5f7d6 Mon Sep 17 00:00:00 2001 From: yum Date: Sat, 9 Sep 2023 20:29:06 -0700 Subject: Constrain UI text buffers to 1000 lines This keeps memory usage from growing without bound. --- GUI/GUI/GUI/Logging.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/GUI/GUI/GUI/Logging.cpp b/GUI/GUI/GUI/Logging.cpp index b5b9e55..6b42bd6 100644 --- a/GUI/GUI/GUI/Logging.cpp +++ b/GUI/GUI/GUI/Logging.cpp @@ -7,10 +7,12 @@ #endif #include +#include #include #include #include +#include #include Logging::ThreadLogger Logging::kThreadLogger = Logging::ThreadLogger(); @@ -43,7 +45,29 @@ void Logging::ThreadLogger::Drain() } log_ofs << message; } + + // Constrain wxTextCtrl's to 1000 lines to keep memory in check. + if (frame) { + wxString allText = frame->GetValue(); + wxArrayString lines = wxStringTokenize(allText, "\n"); + size_t count = lines.GetCount(); + if (count > 2000) { + // Keep only the last 1000 lines. + size_t linesToRemove = count - 1000; + + // Remove lines from the beginning + lines.RemoveAt(0, linesToRemove); + + // Join the lines back into a single string + wxString newText = wxJoin(lines, '\n'); + + // Update the text in the wxTextCtrl + frame->Clear(); + frame->AppendText(newText); + } + } } + log_ofs.close(); messages_.clear(); } -- cgit v1.2.3