Use unique_ptr instead of raw pointer (#77)

This commit is contained in:
Jack Zhou
2022-08-08 19:41:01 +08:00
committed by GitHub
parent 0e45a7e4aa
commit 60ac898ece
3 changed files with 11 additions and 15 deletions

View File

@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "fastdeploy/fastdeploy_model.h" #include "fastdeploy/fastdeploy_model.h"
#include "fastdeploy/utils/unique_ptr.h"
#include "fastdeploy/utils/utils.h" #include "fastdeploy/utils/utils.h"
namespace fastdeploy { namespace fastdeploy {
@@ -53,7 +54,7 @@ bool FastDeployModel::InitRuntime() {
<< std::endl; << std::endl;
return false; return false;
} }
runtime_ = std::unique_ptr<Runtime>(new Runtime()); runtime_ = utils::make_unique<Runtime>();
if (!runtime_->Init(runtime_option)) { if (!runtime_->Init(runtime_option)) {
return false; return false;
} }

View File

@@ -13,7 +13,9 @@
// limitations under the License. // limitations under the License.
#include "fastdeploy/fastdeploy_runtime.h" #include "fastdeploy/fastdeploy_runtime.h"
#include "fastdeploy/utils/unique_ptr.h"
#include "fastdeploy/utils/utils.h" #include "fastdeploy/utils/utils.h"
#ifdef ENABLE_ORT_BACKEND #ifdef ENABLE_ORT_BACKEND
#include "fastdeploy/backends/ort/ort_backend.h" #include "fastdeploy/backends/ort/ort_backend.h"
#endif #endif
@@ -276,8 +278,8 @@ void Runtime::CreatePaddleBackend() {
pd_option.cpu_thread_num = option.cpu_thread_num; pd_option.cpu_thread_num = option.cpu_thread_num;
FDASSERT(option.model_format == Frontend::PADDLE, FDASSERT(option.model_format == Frontend::PADDLE,
"PaddleBackend only support model format of Frontend::PADDLE."); "PaddleBackend only support model format of Frontend::PADDLE.");
backend_ = new PaddleBackend(); backend_ = utils::make_unique<PaddleBackend>();
auto casted_backend = dynamic_cast<PaddleBackend*>(backend_); auto casted_backend = dynamic_cast<PaddleBackend*>(backend_.get());
FDASSERT(casted_backend->InitFromPaddle(option.model_file, option.params_file, FDASSERT(casted_backend->InitFromPaddle(option.model_file, option.params_file,
pd_option), pd_option),
"Load model from Paddle failed while initliazing PaddleBackend."); "Load model from Paddle failed while initliazing PaddleBackend.");
@@ -306,8 +308,8 @@ void Runtime::CreateOrtBackend() {
option.model_format == Frontend::ONNX, option.model_format == Frontend::ONNX,
"OrtBackend only support model format of Frontend::PADDLE / " "OrtBackend only support model format of Frontend::PADDLE / "
"Frontend::ONNX."); "Frontend::ONNX.");
backend_ = new OrtBackend(); backend_ = utils::make_unique<OrtBackend>();
auto casted_backend = dynamic_cast<OrtBackend*>(backend_); auto casted_backend = dynamic_cast<OrtBackend*>(backend_.get());
if (option.model_format == Frontend::ONNX) { if (option.model_format == Frontend::ONNX) {
FDASSERT(casted_backend->InitFromOnnx(option.model_file, ort_option), FDASSERT(casted_backend->InitFromOnnx(option.model_file, ort_option),
"Load model from ONNX failed while initliazing OrtBackend."); "Load model from ONNX failed while initliazing OrtBackend.");
@@ -344,8 +346,8 @@ void Runtime::CreateTrtBackend() {
option.model_format == Frontend::ONNX, option.model_format == Frontend::ONNX,
"TrtBackend only support model format of Frontend::PADDLE / " "TrtBackend only support model format of Frontend::PADDLE / "
"Frontend::ONNX."); "Frontend::ONNX.");
backend_ = new TrtBackend(); backend_ = utils::make_unique<TrtBackend>();
auto casted_backend = dynamic_cast<TrtBackend*>(backend_); auto casted_backend = dynamic_cast<TrtBackend*>(backend_.get());
if (option.model_format == Frontend::ONNX) { if (option.model_format == Frontend::ONNX) {
FDASSERT(casted_backend->InitFromOnnx(option.model_file, trt_option), FDASSERT(casted_backend->InitFromOnnx(option.model_file, trt_option),
"Load model from ONNX failed while initliazing TrtBackend."); "Load model from ONNX failed while initliazing TrtBackend.");

View File

@@ -153,14 +153,7 @@ struct FASTDEPLOY_DECL Runtime {
RuntimeOption option; RuntimeOption option;
~Runtime() {
if (backend_ != nullptr) {
delete backend_;
backend_ = nullptr;
}
}
private: private:
BaseBackend* backend_ = nullptr; std::unique_ptr<BaseBackend> backend_;
}; };
} // namespace fastdeploy } // namespace fastdeploy