added ShutdownLibrary function to uninitialize global variables
authorTaku Kudo <taku@google.com>
Sat, 20 Aug 2022 14:34:37 +0000 (23:34 +0900)
committerKentaro Hayashi <kenhys@xdump.org>
Mon, 21 Nov 2022 13:43:46 +0000 (13:43 +0000)
Signed-off-by: Kentaro Hayashi <kenhys@gmail.com>
Gbp-Pq: Name 0023-added-ShutdownLibrary-function-to-uninitialize-globa.patch

src/compile_charsmap_main.cc
src/error.cc
src/init.h
src/spm_decode_main.cc
src/spm_encode_main.cc
src/spm_export_vocab_main.cc
src/spm_normalize_main.cc
src/spm_train_main.cc
src/test_main.cc

index 13bf82217ec9a13285a7278f4c59ac429664c5c8..da15328ddbf17024ffbf599586359aee8b769b7c 100644 (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<
index 10faa2d7401980bb06e8447b6c693f024fcc2434..d3792dc3d6a37d22b4f1c94a5679f53f543e8ba1 100644 (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);
   }
 }
index 090a2d988c9c8846e41689d4754074b572de08eb..7c75db2725502af4bce8477621a5cbcb284b3452 100644 (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_
index 3382ddc3cdbdb578d3ba4ae7ae7b8ef877554a0f..bc49bd3829da39019488f7527570ed45590268b5 100644 (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;
 
index b0e508d9a14eae2db417503add181007895ed008..2fbb850fa145d314f419fcf8824e7217e6fd10ff 100644 (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;
 
index b5d93cb048b50e71c70f48103b536c9d0acac36d..e5b97dfe4e63121b3c9446b7a5d9c2f153b11b3f 100644 (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;
index 96da360699f9c854d0a7640bf2dca028e82fea25..39f3ef904d72c4c283e57ab3a75980985d7eb64c 100644 (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;
 
index c34ee02d43fca3b000515cb4a9b4d67a4cdc8778..6ab634d346589c57d9b9279d98494ff6bd707299 100644 (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;
index b3170e27c54e845f4e2704cf12cf5b2aac466188..38c978d047b213a57ac5c335bd4da06be9ed4d12 100644 (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;