Use unique pointer for ANeuralNetworksMemory (#2578)
author오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Tue, 4 Sep 2018 09:51:49 +0000 (18:51 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 4 Sep 2018 09:51:49 +0000 (18:51 +0900)
Use unique pointer for ANeuralNetworksMemory to avoid memory leak warning

Signed-off-by: Hyeongseok Oh <hseok82.oh@samsung.com>
runtimes/neurun/src/frontend/memory.cc
runtimes/pure_arm_compute/src/memory.cc

index 56bc846..90b4c84 100644 (file)
@@ -1,7 +1,9 @@
 #include <NeuralNetworks.h>
 #include <sys/mman.h>
 #include <new>
+#include <memory>
 
+#include "nnfw/std/memory.h"
 #include "frontend/wrapper/memory.h"
 
 int ANeuralNetworksMemory_createFromFd(size_t size, int protect, int fd, size_t offset,
@@ -12,11 +14,14 @@ int ANeuralNetworksMemory_createFromFd(size_t size, int protect, int fd, size_t
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
-  *memory = new (std::nothrow) ANeuralNetworksMemory{size, protect, fd, offset};
-  if (*memory == nullptr)
+  // Use unique pointer to avoid memory leak
+  std::unique_ptr<ANeuralNetworksMemory> memory_ptr =
+      nnfw::make_unique<ANeuralNetworksMemory>(size, protect, fd, offset);
+  if (memory_ptr == nullptr)
   {
     return ANEURALNETWORKS_OUT_OF_MEMORY;
   }
+  *memory = memory_ptr.release();
 
   return ANEURALNETWORKS_NO_ERROR;
 }
index ab55f0e..b44c278 100644 (file)
@@ -1,6 +1,8 @@
 #include <NeuralNetworks.h>
 #include <sys/mman.h>
+#include <memory>
 
+#include "nnfw/std/memory.h"
 #include "memory.h"
 
 int ANeuralNetworksMemory_createFromFd(size_t size, int protect, int fd, size_t offset,
@@ -11,12 +13,14 @@ int ANeuralNetworksMemory_createFromFd(size_t size, int protect, int fd, size_t
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
-  ANeuralNetworksMemory *memory_ptr = new ANeuralNetworksMemory{size, protect, fd, offset};
+  // Use unique pointer to avoid memory leak
+  std::unique_ptr<ANeuralNetworksMemory> memory_ptr =
+      nnfw::make_unique<ANeuralNetworksMemory>(size, protect, fd, offset);
   if (memory_ptr == nullptr)
   {
     return ANEURALNETWORKS_OUT_OF_MEMORY;
   }
-  *memory = memory_ptr;
+  *memory = memory_ptr.release();
 
   return ANEURALNETWORKS_NO_ERROR;
 }