added ShutdownLibrary function to uninitialize global variables

This commit is contained in:
Taku Kudo 2022-08-20 23:34:37 +09:00
parent fda34113aa
commit de150504f8
9 changed files with 27 additions and 3 deletions

View File

@ -156,6 +156,7 @@ struct BinaryBlob {
} // namespace sentencepiece
int main(int argc, char **argv) {
sentencepiece::ScopedResourceDestructor cleaner;
sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true);
const std::vector<std::pair<

View File

@ -15,6 +15,7 @@
#include <cstring>
#include "common.h"
#include "init.h"
#include "sentencepiece_processor.h"
#ifdef _USE_EXTERNAL_ABSL
@ -35,6 +36,7 @@ void Abort() {
SetTestCounter(2);
} else {
std::cerr << "Program terminated with an unrecoverable error." << std::endl;
ShutdownLibrary();
exit(-1);
}
}
@ -43,6 +45,7 @@ void Exit(int code) {
if (GetTestCounter() == 1) {
SetTestCounter(2);
} else {
ShutdownLibrary();
exit(code);
}
}

View File

@ -18,6 +18,7 @@
#include "common.h"
#include "third_party/absl/flags/flag.h"
#include "third_party/absl/flags/parse.h"
#include "third_party/protobuf-lite/google/protobuf/message_lite.h"
ABSL_DECLARE_FLAG(int32, minloglevel);
@ -35,6 +36,20 @@ inline void ParseCommandLineFlags(const char *usage, int *argc, char ***argv,
logging::SetMinLogLevel(absl::GetFlag(FLAGS_minloglevel));
}
inline void ShutdownLibrary() {
google::protobuf::ShutdownProtobufLibrary();
#ifdef HAS_ABSL_CLEANUP_FLAGS
absl::CleanupFlags();
#endif
}
class ScopedResourceDestructor {
public:
ScopedResourceDestructor() {}
~ScopedResourceDestructor() { ShutdownLibrary(); }
};
} // namespace sentencepiece
#endif // INIT_H_

View File

@ -34,6 +34,7 @@ ABSL_FLAG(std::string, extra_options, "",
"':' separated encoder extra options, e.g., \"reverse:bos:eos\"");
int main(int argc, char *argv[]) {
sentencepiece::ScopedResourceDestructor cleaner;
sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true);
std::vector<std::string> rest_args;

View File

@ -51,6 +51,7 @@ ABSL_FLAG(bool, generate_vocabulary, false,
"Generates vocabulary file instead of segmentation");
int main(int argc, char *argv[]) {
sentencepiece::ScopedResourceDestructor cleaner;
sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true);
std::vector<std::string> rest_args;

View File

@ -1,11 +1,10 @@
// Copyright 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// n// http://www.apache.org/licenses/LICENSE-2.0
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@ -29,6 +28,7 @@ ABSL_FLAG(std::string, output_format, "vocab",
"and scores, syms outputs pieces and indices.");
int main(int argc, char *argv[]) {
sentencepiece::ScopedResourceDestructor cleaner;
sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true);
sentencepiece::SentencePieceProcessor sp;

View File

@ -46,6 +46,7 @@ using sentencepiece::normalizer::Builder;
using sentencepiece::normalizer::Normalizer;
int main(int argc, char *argv[]) {
sentencepiece::ScopedResourceDestructor cleaner;
sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true);
std::vector<std::string> rest_args;

View File

@ -157,6 +157,7 @@ ABSL_FLAG(std::uint64_t, differential_privacy_clipping_threshold, 0,
" clipping the counts for DP");
int main(int argc, char *argv[]) {
sentencepiece::ScopedResourceDestructor cleaner;
sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true);
sentencepiece::TrainerSpec trainer_spec;

View File

@ -24,6 +24,7 @@ ABSL_FLAG(std::string, test_srcdir, "../data", "Data directory.");
ABSL_FLAG(std::string, test_tmpdir, "test_tmp", "Temporary directory.");
int main(int argc, char **argv) {
sentencepiece::ScopedResourceDestructor cleaner;
sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true);
sentencepiece::test::RunAllTests();
return 0;