From 47550624a760d6f76d2ca2871ff89637c46b7ae4 Mon Sep 17 00:00:00 2001 From: maxwell_ding Date: Sun, 26 Apr 2020 15:27:01 +0800 Subject: [PATCH] fix(core): fix memory leak in Global singleton, test=develop --- lite/core/context.h | 2 +- lite/core/device_info.h | 5 +++-- lite/core/op_registry.cc | 3 ++- lite/utils/factory.h | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lite/core/context.h b/lite/core/context.h index f8013ac500..f3a6857b84 100644 --- a/lite/core/context.h +++ b/lite/core/context.h @@ -395,7 +395,7 @@ class KernelContext { class ContextScheduler { public: static ContextScheduler& Global() { - static auto* x = new ContextScheduler; + static auto x = std::unique_ptr(new ContextScheduler); return *x; } diff --git a/lite/core/device_info.h b/lite/core/device_info.h index f5b75039ea..01f2c09912 100644 --- a/lite/core/device_info.h +++ b/lite/core/device_info.h @@ -15,6 +15,7 @@ #pragma once #include +#include #include #include #include "lite/core/tensor.h" @@ -43,7 +44,7 @@ typedef enum { class DeviceInfo { public: static DeviceInfo& Global() { - static auto* x = new DeviceInfo; + static auto x = std::unique_ptr(new DeviceInfo); return *x; } @@ -156,7 +157,7 @@ class Env { typedef TargetWrapper API; typedef std::vector> Devs; static Devs& Global() { - static Devs* devs = new Devs(); + static auto devs = std::unique_ptr(new Devs); return *devs; } static void Init(int max_stream = 6) { diff --git a/lite/core/op_registry.cc b/lite/core/op_registry.cc index 29c853c70c..4fc1b8623a 100644 --- a/lite/core/op_registry.cc +++ b/lite/core/op_registry.cc @@ -14,6 +14,7 @@ #include "lite/core/op_registry.h" #include +#include #include namespace paddle { @@ -248,7 +249,7 @@ KernelRegistry::KernelRegistry() } KernelRegistry &KernelRegistry::Global() { - static auto *x = new KernelRegistry; + static auto x = std::unique_ptr(new KernelRegistry); return *x; } diff --git a/lite/utils/factory.h b/lite/utils/factory.h index fea8561bcf..72e1c04459 100644 --- a/lite/utils/factory.h +++ b/lite/utils/factory.h @@ -48,7 +48,7 @@ class Factory { using creator_t = std::function; static Factory& Global() { - static Factory* x = new self_t; + static auto x = std::unique_ptr(new self_t); return *x; } -- GitLab