From bf509a719789ca7f26f8a626eb78b941b2848600 Mon Sep 17 00:00:00 2001 From: liuqi Date: Mon, 4 Jun 2018 13:08:22 +0800 Subject: [PATCH] Fix memory leak when allocate memory failed. --- mace/core/allocator.h | 3 +++ mace/core/runtime/opencl/opencl_allocator.cc | 2 ++ 2 files changed, 5 insertions(+) diff --git a/mace/core/allocator.h b/mace/core/allocator.h index a35b2a2e..a241fd4c 100644 --- a/mace/core/allocator.h +++ b/mace/core/allocator.h @@ -88,6 +88,9 @@ class CPUAllocator : public Allocator { LOG(WARNING) << "Allocate CPU Buffer with " << nbytes << " bytes failed because of" << strerror(errno); + if (data != NULL) { + free(data); + } *result = nullptr; return MaceStatus::MACE_OUT_OF_RESOURCES; } diff --git a/mace/core/runtime/opencl/opencl_allocator.cc b/mace/core/runtime/opencl/opencl_allocator.cc index 4d356d6f..4d0c2351 100644 --- a/mace/core/runtime/opencl/opencl_allocator.cc +++ b/mace/core/runtime/opencl/opencl_allocator.cc @@ -58,6 +58,7 @@ MaceStatus OpenCLAllocator::New(size_t nbytes, void **result) const { LOG(WARNING) << "Allocate OpenCL Buffer with " << nbytes << " bytes failed because of" << OpenCLErrorToString(error); + delete buffer; *result = nullptr; return MaceStatus::MACE_OUT_OF_RESOURCES; } else { @@ -89,6 +90,7 @@ MaceStatus OpenCLAllocator::NewImage(const std::vector &image_shape, << image_shape[0] << ", " << image_shape[1] << "] failed because of" << OpenCLErrorToString(error); + delete cl_image; *result = nullptr; return MaceStatus::MACE_OUT_OF_RESOURCES; } else { -- GitLab