From ae193288e15aaecd17f83de68a30561a8f2d3e08 Mon Sep 17 00:00:00 2001 From: Evan Hu Date: Sun, 6 Sep 2026 10:31:08 +0900 Subject: [PATCH] =?UTF-8?q?fix(decoder):=20=E4=BF=AE=E5=A4=8D=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E9=87=8D=E8=BD=BD=E7=9A=84=E5=86=85=E5=AD=98=E9=87=8A?= =?UTF-8?q?=E6=94=BE=E4=B8=8E=E6=A0=91=E8=8A=82=E7=82=B9=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/share/spellingtrie.cpp | 14 +++++++++++--- tests/decoder_smoke.cpp | 26 ++++++++++++++------------ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/share/spellingtrie.cpp b/src/share/spellingtrie.cpp index a4deba3..61f59cf 100644 --- a/src/share/spellingtrie.cpp +++ b/src/share/spellingtrie.cpp @@ -84,11 +84,11 @@ SpellingTrie::~SpellingTrie() { } if (NULL != dumb_node_) { - delete[] dumb_node_; + delete dumb_node_; } if (NULL != splitter_node_) { - delete[] splitter_node_; + delete splitter_node_; } if (NULL != instance_) { @@ -266,6 +266,14 @@ bool SpellingTrie::construct(const char* spelling_arr, size_t item_size, size_t node_num_ = 1; #endif + // Model reloads reuse this singleton; release the previous tree first. + if (root_) { + free_son_trie(root_); + delete root_; + } + delete dumb_node_; + delete splitter_node_; + root_ = new SpellingNode(); memset(root_, 0, sizeof(SpellingNode)); @@ -346,7 +354,7 @@ bool SpellingTrie::build_ym_info() { delete spl_table; // Generate the maping from the spelling ids to the Yunmu ids. - if (spl_ym_ids_) delete spl_ym_ids_; + if (spl_ym_ids_) delete[] spl_ym_ids_; spl_ym_ids_ = new uint8[spelling_num_ + kFullSplIdStart]; if (NULL == spl_ym_ids_) return false; diff --git a/tests/decoder_smoke.cpp b/tests/decoder_smoke.cpp index 11affa9..ef1b9d4 100644 --- a/tests/decoder_smoke.cpp +++ b/tests/decoder_smoke.cpp @@ -6,21 +6,23 @@ int main(int argc, char** argv) { if (argc != 3) return 2; std::remove(argv[2]); - if (!ime_pinyin::im_open_decoder(argv[1], argv[2])) { - std::cerr << "Cannot open decoder dictionary\n"; - return 1; - } int status = 0; - for (const char* input : {"nihao", "zhongguo", "shurufa"}) { - ime_pinyin::im_reset_search(); - const auto count = ime_pinyin::im_search(input, std::strlen(input)); - ime_pinyin::char16 candidate[256] = {}; - if (!count || !ime_pinyin::im_get_candidate(0, candidate, 256) || !candidate[0]) { - std::cerr << "No candidate for " << input << '\n'; - status = 1; + for (int reload = 0; reload < 3; ++reload) { + if (!ime_pinyin::im_open_decoder(argv[1], argv[2])) { + std::cerr << "Cannot open decoder dictionary\n"; + return 1; } + for (const char* input : {"nihao", "zhongguo", "shurufa"}) { + ime_pinyin::im_reset_search(); + const auto count = ime_pinyin::im_search(input, std::strlen(input)); + ime_pinyin::char16 candidate[256] = {}; + if (!count || !ime_pinyin::im_get_candidate(0, candidate, 256) || !candidate[0]) { + std::cerr << "No candidate for " << input << '\n'; + status = 1; + } + } + ime_pinyin::im_close_decoder(); } - ime_pinyin::im_close_decoder(); std::remove(argv[2]); return status; }