summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2012-10-11 13:49:26 +0800
committerPeng Wu <alexepico@gmail.com>2012-10-11 13:49:26 +0800
commit5ed3e7452572dd9580c5c514e71a3494978e73b0 (patch)
treeec9b91edfbdebdc7635a216acea71f808aac6081
parentd23d9fca34dae6aea0cb6e8640ccf3f2cbc81256 (diff)
downloadlibpinyin-5ed3e7452572dd9580c5c514e71a3494978e73b0.tar.gz
libpinyin-5ed3e7452572dd9580c5c514e71a3494978e73b0.tar.xz
libpinyin-5ed3e7452572dd9580c5c514e71a3494978e73b0.zip
fixes memory leak in test pinyin lookup
-rw-r--r--tests/lookup/test_pinyin_lookup.cpp30
1 files changed, 17 insertions, 13 deletions
diff --git a/tests/lookup/test_pinyin_lookup.cpp b/tests/lookup/test_pinyin_lookup.cpp
index e74ed44..f762bea 100644
--- a/tests/lookup/test_pinyin_lookup.cpp
+++ b/tests/lookup/test_pinyin_lookup.cpp
@@ -48,10 +48,18 @@ int main( int argc, char * argv[]){
PinyinLookup2 pinyin_lookup(options, &largetable, &phrase_index,
&system_bigram, &user_bigram);
+
+ /* prepare the prefixes for get_best_match. */
+ TokenVector prefixes = g_array_new
+ (FALSE, FALSE, sizeof(phrase_token_t));
+ g_array_append_val(prefixes, sentence_start);
- char* linebuf = NULL;
- size_t size = 0;
- ssize_t read;
+ CandidateConstraints constraints = g_array_new
+ (FALSE, FALSE, sizeof(lookup_constraint_t));
+
+ MatchResults results = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
+
+ char* linebuf = NULL; size_t size = 0; ssize_t read;
while( (read = getline(&linebuf, &size, stdin)) != -1 ){
if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
linebuf[strlen(linebuf) - 1] = '\0';
@@ -69,22 +77,13 @@ int main( int argc, char * argv[]){
if ( 0 == keys->len ) /* invalid pinyin */
continue;
- /* prepare the prefixes for get_best_match. */
- TokenVector prefixes = g_array_new
- (FALSE, FALSE, sizeof(phrase_token_t));
- g_array_append_val(prefixes, sentence_start);
-
/* initialize constraints. */
- CandidateConstraints constraints = g_array_new(FALSE, FALSE, sizeof(lookup_constraint_t));
-
g_array_set_size(constraints, keys->len);
for ( size_t i = 0; i < constraints->len; ++i){
lookup_constraint_t * constraint = &g_array_index(constraints, lookup_constraint_t, i);
constraint->m_type = NO_CONSTRAINT;
}
- MatchResults results = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
-
guint32 start_time = record_time();
for ( size_t i = 0; i < bench_times; ++i)
pinyin_lookup.get_best_match(prefixes, keys, constraints, results);
@@ -99,11 +98,16 @@ int main( int argc, char * argv[]){
char * sentence = NULL;
pinyin_lookup.convert_to_utf8(results, sentence);
printf("%s\n", sentence);
- g_array_free(results, TRUE);
g_array_free(keys, TRUE);
g_array_free(key_rests, TRUE);
g_free(sentence);
}
+
+ g_array_free(prefixes, TRUE);
+ g_array_free(constraints, TRUE);
+ g_array_free(results, TRUE);
+
free(linebuf);
+ return 0;
}