Skip to content

Commit 60ac898

Browse files
authored
Use unique_ptr instead of raw pointer (#77)
1 parent 0e45a7e commit 60ac898

3 files changed

Lines changed: 11 additions & 15 deletions

File tree

csrcs/fastdeploy/fastdeploy_model.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414
#include "fastdeploy/fastdeploy_model.h"
15+
#include "fastdeploy/utils/unique_ptr.h"
1516
#include "fastdeploy/utils/utils.h"
1617

1718
namespace fastdeploy {
@@ -53,7 +54,7 @@ bool FastDeployModel::InitRuntime() {
5354
<< std::endl;
5455
return false;
5556
}
56-
runtime_ = std::unique_ptr<Runtime>(new Runtime());
57+
runtime_ = utils::make_unique<Runtime>();
5758
if (!runtime_->Init(runtime_option)) {
5859
return false;
5960
}

csrcs/fastdeploy/fastdeploy_runtime.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
// limitations under the License.
1414

1515
#include "fastdeploy/fastdeploy_runtime.h"
16+
#include "fastdeploy/utils/unique_ptr.h"
1617
#include "fastdeploy/utils/utils.h"
18+
1719
#ifdef ENABLE_ORT_BACKEND
1820
#include "fastdeploy/backends/ort/ort_backend.h"
1921
#endif
@@ -276,8 +278,8 @@ void Runtime::CreatePaddleBackend() {
276278
pd_option.cpu_thread_num = option.cpu_thread_num;
277279
FDASSERT(option.model_format == Frontend::PADDLE,
278280
"PaddleBackend only support model format of Frontend::PADDLE.");
279-
backend_ = new PaddleBackend();
280-
auto casted_backend = dynamic_cast<PaddleBackend*>(backend_);
281+
backend_ = utils::make_unique<PaddleBackend>();
282+
auto casted_backend = dynamic_cast<PaddleBackend*>(backend_.get());
281283
FDASSERT(casted_backend->InitFromPaddle(option.model_file, option.params_file,
282284
pd_option),
283285
"Load model from Paddle failed while initliazing PaddleBackend.");
@@ -306,8 +308,8 @@ void Runtime::CreateOrtBackend() {
306308
option.model_format == Frontend::ONNX,
307309
"OrtBackend only support model format of Frontend::PADDLE / "
308310
"Frontend::ONNX.");
309-
backend_ = new OrtBackend();
310-
auto casted_backend = dynamic_cast<OrtBackend*>(backend_);
311+
backend_ = utils::make_unique<OrtBackend>();
312+
auto casted_backend = dynamic_cast<OrtBackend*>(backend_.get());
311313
if (option.model_format == Frontend::ONNX) {
312314
FDASSERT(casted_backend->InitFromOnnx(option.model_file, ort_option),
313315
"Load model from ONNX failed while initliazing OrtBackend.");
@@ -344,8 +346,8 @@ void Runtime::CreateTrtBackend() {
344346
option.model_format == Frontend::ONNX,
345347
"TrtBackend only support model format of Frontend::PADDLE / "
346348
"Frontend::ONNX.");
347-
backend_ = new TrtBackend();
348-
auto casted_backend = dynamic_cast<TrtBackend*>(backend_);
349+
backend_ = utils::make_unique<TrtBackend>();
350+
auto casted_backend = dynamic_cast<TrtBackend*>(backend_.get());
349351
if (option.model_format == Frontend::ONNX) {
350352
FDASSERT(casted_backend->InitFromOnnx(option.model_file, trt_option),
351353
"Load model from ONNX failed while initliazing TrtBackend.");

csrcs/fastdeploy/fastdeploy_runtime.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,7 @@ struct FASTDEPLOY_DECL Runtime {
153153

154154
RuntimeOption option;
155155

156-
~Runtime() {
157-
if (backend_ != nullptr) {
158-
delete backend_;
159-
backend_ = nullptr;
160-
}
161-
}
162-
163156
private:
164-
BaseBackend* backend_ = nullptr;
157+
std::unique_ptr<BaseBackend> backend_;
165158
};
166159
} // namespace fastdeploy

0 commit comments

Comments
 (0)