From 20684021dad9339941e62618ec241ddc6ecc0c43 Mon Sep 17 00:00:00 2001 From: cao lei Date: Mon, 6 Feb 2023 09:53:48 -0800 Subject: [PATCH] do not use raw pointer for CpuBuffersInfo::buffers (#14574) ### Description Do not use raw pointer for CpuBuffersInfo::buffers object ### Motivation and Context This PR is to fix the bug 11159: https://dev.azure.com/aiinfra/ONNX%20Runtime/_workitems/edit/11159/ --- onnxruntime/core/providers/cuda/cuda_stream_handle.cc | 5 ++--- onnxruntime/core/providers/rocm/rocm_stream_handle.cc | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/onnxruntime/core/providers/cuda/cuda_stream_handle.cc b/onnxruntime/core/providers/cuda/cuda_stream_handle.cc index b818e9b57a7b2..81d0070f1aeaf 100644 --- a/onnxruntime/core/providers/cuda/cuda_stream_handle.cc +++ b/onnxruntime/core/providers/cuda/cuda_stream_handle.cc @@ -102,7 +102,7 @@ struct CpuBuffersInfo { // should contain all values in // deferred_release_buffer_pool_[my_stream] // when release my_stream's buffers. - void** buffers; + std::unique_ptr buffers; // CPU buffer buffers[i]. // Number of buffer points in "buffers". size_t n_buffers; @@ -117,7 +117,6 @@ static void CUDART_CB ReleaseCpuBufferCallback(void* raw_info) { for (size_t i = 0; i < info->n_buffers; ++i) { info->allocator->Free(info->buffers[i]); } - delete[] info->buffers; } Status CudaStream::CleanUpOnRunEnd() { @@ -128,7 +127,7 @@ Status CudaStream::CleanUpOnRunEnd() { if (release_cpu_buffer_on_cuda_stream_ && cpu_allocator_->Info().alloc_type == OrtArenaAllocator) { std::unique_ptr cpu_buffers_info = std::make_unique(); cpu_buffers_info->allocator = cpu_allocator_; - cpu_buffers_info->buffers = new void*[deferred_cpu_buffers_.size()]; + cpu_buffers_info->buffers = std::make_unique(deferred_cpu_buffers_.size()); for (size_t i = 0; i < deferred_cpu_buffers_.size(); ++i) { cpu_buffers_info->buffers[i] = deferred_cpu_buffers_.at(i); } diff --git a/onnxruntime/core/providers/rocm/rocm_stream_handle.cc b/onnxruntime/core/providers/rocm/rocm_stream_handle.cc index c87fa6983425c..fb6eeb6746376 100644 --- a/onnxruntime/core/providers/rocm/rocm_stream_handle.cc +++ b/onnxruntime/core/providers/rocm/rocm_stream_handle.cc @@ -83,7 +83,7 @@ void RocmStream::EnqueDeferredCPUBuffer(void* cpu_buffer) { struct CpuBuffersInfo { // TODO: should be moved to base class AllocatorPtr allocator; - void** buffers; + std::unique_ptr buffers; // CPU buffer buffers[i]. // Number of buffer points in "buffers". size_t n_buffers; @@ -95,7 +95,6 @@ static void ReleaseCpuBufferCallback(hipStream_t /*stream*/, hipError_t /*status for (size_t i = 0; i < info->n_buffers; ++i) { info->allocator->Free(info->buffers[i]); } - delete[] info->buffers; } Status RocmStream::CleanUpOnRunEnd() { @@ -106,7 +105,7 @@ Status RocmStream::CleanUpOnRunEnd() { if (release_cpu_buffer_on_rocm_stream_ && cpu_allocator_->Info().alloc_type == OrtArenaAllocator) { std::unique_ptr cpu_buffers_info = std::make_unique(); cpu_buffers_info->allocator = cpu_allocator_; - cpu_buffers_info->buffers = new void*[deferred_cpu_buffers_.size()]; + cpu_buffers_info->buffers = std::make_unique(deferred_cpu_buffers_.size()); for (size_t i = 0; i < deferred_cpu_buffers_.size(); ++i) { cpu_buffers_info->buffers[i] = deferred_cpu_buffers_.at(i); }