瀏覽代碼

Return a nullptr on failure in CreateRecognizer (#188)

* Return a nullptr on failure in CreateRecognizer

* fix style issues
Fangjun Kuang 2 年之前
父節點
當前提交
b06b401311
共有 3 個文件被更改,包括 22 次插入10 次删除
  1. 13 10
      sherpa-ncnn/c-api/c-api.cc
  2. 4 0
      sherpa-ncnn/csrc/recognizer.cc
  3. 5 0
      sherpa-ncnn/csrc/recognizer.h

+ 13 - 10
sherpa-ncnn/c-api/c-api.cc

@@ -21,6 +21,7 @@
 #include <algorithm>
 #include <memory>
 #include <string>
+#include <utility>
 
 #include "sherpa-ncnn/csrc/display.h"
 #include "sherpa-ncnn/csrc/model.h"
@@ -87,8 +88,13 @@ SherpaNcnnRecognizer *CreateRecognizer(
   config.feat_config.sampling_rate = in_config->feat_config.sampling_rate;
   config.feat_config.feature_dim = in_config->feat_config.feature_dim;
 
+  auto recognizer = std::make_unique<sherpa_ncnn::Recognizer>(config);
+  if (!recognizer->GetModel()) {
+    return nullptr;
+  }
+
   auto ans = new SherpaNcnnRecognizer;
-  ans->recognizer = std::make_unique<sherpa_ncnn::Recognizer>(config);
+  ans->recognizer = std::move(recognizer);
   return ans;
 }
 
@@ -127,14 +133,13 @@ SherpaNcnnResult *GetResult(SherpaNcnnRecognizer *p, SherpaNcnnStream *s) {
   if (r->count > 0) {
     // Each word ends with nullptr
     r->tokens = new char[text.size() + r->count];
-    memset(reinterpret_cast<void*>(const_cast<char*>(r->tokens)), 0,
-             text.size() + r->count);
+    memset(reinterpret_cast<void *>(const_cast<char *>(r->tokens)), 0,
+           text.size() + r->count);
     r->timestamps = new float[r->count];
     int pos = 0;
     for (int32_t i = 0; i < r->count; ++i) {
-      memcpy(reinterpret_cast<void*>(const_cast<char*>(r->tokens + pos)),
-             res.stokens[i].c_str(),
-             res.stokens[i].size());
+      memcpy(reinterpret_cast<void *>(const_cast<char *>(r->tokens + pos)),
+             res.stokens[i].c_str(), res.stokens[i].size());
       pos += res.stokens[i].size() + 1;
       r->timestamps[i] = res.timestamps[i];
     }
@@ -148,10 +153,8 @@ SherpaNcnnResult *GetResult(SherpaNcnnRecognizer *p, SherpaNcnnStream *s) {
 
 void DestroyResult(const SherpaNcnnResult *r) {
   delete[] r->text;
-  if (r->timestamps != nullptr)
-      delete[] r->timestamps;
-  if (r->tokens != nullptr)
-      delete[] r->tokens;
+  delete[] r->timestamps;  // it is ok to delete a nullptr
+  delete[] r->tokens;
   delete r;
 }
 

+ 4 - 0
sherpa-ncnn/csrc/recognizer.cc

@@ -182,6 +182,8 @@ class Recognizer::Impl {
     return Convert(decoder_result, sym_, frame_shift_ms, subsampling_factor);
   }
 
+  const Model *GetModel() const { return model_.get(); }
+
  private:
   RecognizerConfig config_;
   std::unique_ptr<Model> model_;
@@ -216,4 +218,6 @@ RecognitionResult Recognizer::GetResult(Stream *s) const {
   return impl_->GetResult(s);
 }
 
+const Model *Recognizer::GetModel() const { return impl_->GetModel(); }
+
 }  // namespace sherpa_ncnn

+ 5 - 0
sherpa-ncnn/csrc/recognizer.h

@@ -99,6 +99,11 @@ class Recognizer {
 
   RecognitionResult GetResult(Stream *s) const;
 
+  // Return the contained model
+  //
+  // The user should not free it.
+  const Model *GetModel() const;
+
  private:
   class Impl;
   std::unique_ptr<Impl> impl_;