From ba362a027b79247133d4acf47331deff10e5202d Mon Sep 17 00:00:00 2001 From: Wuwei Lin Date: Wed, 23 May 2018 14:23:57 +0800 Subject: [PATCH] Fix string preprocessor --- src/shogun/preprocessor/StringPreprocessor.cpp | 9 ++++++++- tests/unit/kernel/CommUlongStringKernel_unittest.cc | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/shogun/preprocessor/StringPreprocessor.cpp b/src/shogun/preprocessor/StringPreprocessor.cpp index b770adb6dbd..fd657399179 100644 --- a/src/shogun/preprocessor/StringPreprocessor.cpp +++ b/src/shogun/preprocessor/StringPreprocessor.cpp @@ -96,6 +96,8 @@ namespace shogun "has to be of C_STRING (%d) class!\n", features->get_feature_class(), C_STRING); + SG_REF(features); + auto string_features = features->as>(); auto string_list = string_features->get_features(); @@ -106,7 +108,12 @@ namespace shogun auto processed = new CStringFeatures( string_list, string_features->get_alphabet()); - SG_UNREF(features); + + // FIXME: we should prevent freeing input features here because SGString + // doesn't have ref counting, which will be freed after unref. + // fix this after #4248 + if (features->ref_count() > 1) + SG_UNREF(features); SG_REF(processed); return processed; diff --git a/tests/unit/kernel/CommUlongStringKernel_unittest.cc b/tests/unit/kernel/CommUlongStringKernel_unittest.cc index 73ba8fb0f06..f58bb688ed9 100644 --- a/tests/unit/kernel/CommUlongStringKernel_unittest.cc +++ b/tests/unit/kernel/CommUlongStringKernel_unittest.cc @@ -38,8 +38,8 @@ TEST(CommUlongStringKernel, kernel_matrix) l_feats->obtain_from_char(s_feats, 5-1, 5, 0, false); CSortUlongString* preproc = new CSortUlongString(); preproc->fit(l_feats); - l_feats->add_preprocessor(preproc); - l_feats->apply_preprocessor(); + l_feats = preproc->apply(l_feats)->as>(); + CCommUlongStringKernel* kernel = new CCommUlongStringKernel(l_feats, l_feats); CIdentityKernelNormalizer* normalizer = new CIdentityKernelNormalizer(); kernel->set_normalizer(normalizer);