From a46f3fcefc1178dcdcbd4d948c0350e29c94e839 Mon Sep 17 00:00:00 2001 From: dzhwinter Date: Thu, 15 Jun 2017 02:52:16 +0800 Subject: [PATCH] "fix double release tensor buffer error." --- paddle/math/tests/CMakeLists.txt | 1 + paddle/optimizer/Tensor.h | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/paddle/math/tests/CMakeLists.txt b/paddle/math/tests/CMakeLists.txt index ceb96b2e250..bdecba0869d 100644 --- a/paddle/math/tests/CMakeLists.txt +++ b/paddle/math/tests/CMakeLists.txt @@ -31,3 +31,4 @@ add_simple_unittest(test_FPException) add_simple_unittest(test_GpuProfiler) add_simple_unittest(test_BaseMatrix) add_simple_unittest(test_Matrix) +add_simple_unittest(test_Matrix2) diff --git a/paddle/optimizer/Tensor.h b/paddle/optimizer/Tensor.h index 2eefdec3e5e..49b8dadaaef 100644 --- a/paddle/optimizer/Tensor.h +++ b/paddle/optimizer/Tensor.h @@ -5,6 +5,7 @@ #include #include +#include "paddle/math/MemoryHandle.h" #include "paddle/utils/Common.h" #include "paddle/utils/Logging.h" @@ -14,15 +15,19 @@ namespace optimizer { template class TensorT { public: - TensorT(size_t size) : height_(1), width_(size) { data_ = new T[size]; } + TensorT(size_t size) + : TensorT(std::make_shared(size * sizeof(float)), size) { + } + TensorT(CpuMemHandlePtr handle, size_t size) + : height_(1), + width_(size), + data_(reinterpret_cast(handle->getBuf())) {} TensorT(T* data, size_t size) : height_(1), width_(size), data_(data) {} TensorT(T* data, size_t h, size_t w) : height_(h), width_(w), data_(data) {} - ~TensorT() { - if (data_) delete data_; - } + virtual ~TensorT() {} T* get_buffer() { return this->data_; } -- GitLab