Merge pull request #3 from metasequoiaime/fix/spelling-trie-reload

fix(decoder): 修复模型重载的内存释放与树节点泄漏
This commit is contained in:
Evan
2026-09-06 10:32:51 +09:00
committed by GitHub
2 changed files with 25 additions and 15 deletions
+11 -3
View File
@@ -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;
+14 -12
View File
@@ -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;
}