Commit d16a3ecc for tesseract

commit d16a3ecc463528f620234e2702d64963b0f1a691
Author: Stefan Weil <sw@weilnetz.de>
Date:   Sun Nov 22 14:10:38 2026 +0100

    Fix crash when training continues with a new character set

    A checkpoint stores copies of the best and the worst model
    (best_model_data_, worst_model_data_) for the evaluation thread and a
    copy of the best trainer (best_trainer_). lstmtraining --continue_from
    resets all counters with InitIterations, but it kept these copies.

    At the first new minimum of the error rate UpdateErrorGraph passed the
    stale worst model to the tester. With --old_traineddata that model still
    has the old number of outputs, while its ground truth is encoded with the
    new recoder. LSTMTrainer::ComputeCharError then wrote beyond its vector
    of label counts, and the program aborted in the evaluation thread
    (free(): invalid next size), right after the first checkpoint.

    Without a change of the character set the stale model was evaluated and
    reported as a result of the new run.

    Clear the three copies in InitIterations.

    Assisted-by: Claude Fable 5.2 (Anthropic)
    Signed-off-by: Stefan Weil <sw@weilnetz.de>

diff --git a/src/training/unicharset/lstmtrainer.cpp b/src/training/unicharset/lstmtrainer.cpp
index 2fcbec98..51619e4d 100644
--- a/src/training/unicharset/lstmtrainer.cpp
+++ b/src/training/unicharset/lstmtrainer.cpp
@@ -207,6 +207,14 @@ void LSTMTrainer::InitIterations() {
     error_rates_[i] = 100.0;
   }
   error_rate_of_last_saved_best_ = kMinStartedErrorRate;
+  // The models which a checkpoint keeps for the evaluation and for the
+  // sub-trainer belong to the old counters. They must not be used any more:
+  // after a change of the character set (--old_traineddata) they even have
+  // another number of outputs than the new recoder, and evaluating them made
+  // ComputeCharError write beyond its array of label counts.
+  best_model_data_.clear();
+  worst_model_data_.clear();
+  best_trainer_.clear();
 }

 // If the training sample is usable, grid searches for the optimal