Skip to content

Commit

Permalink
do not use raw pointer for CpuBuffersInfo::buffers (#14574)
Browse files Browse the repository at this point in the history
### 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/
  • Loading branch information
jslhcl authored Feb 6, 2023
1 parent 4bb95d7 commit 2068402
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
5 changes: 2 additions & 3 deletions onnxruntime/core/providers/cuda/cuda_stream_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<void*[]> buffers;
// CPU buffer buffers[i].
// Number of buffer points in "buffers".
size_t n_buffers;
Expand All @@ -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() {
Expand All @@ -128,7 +127,7 @@ Status CudaStream::CleanUpOnRunEnd() {
if (release_cpu_buffer_on_cuda_stream_ && cpu_allocator_->Info().alloc_type == OrtArenaAllocator) {
std::unique_ptr<CpuBuffersInfo> cpu_buffers_info = std::make_unique<CpuBuffersInfo>();
cpu_buffers_info->allocator = cpu_allocator_;
cpu_buffers_info->buffers = new void*[deferred_cpu_buffers_.size()];
cpu_buffers_info->buffers = std::make_unique<void*[]>(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);
}
Expand Down
5 changes: 2 additions & 3 deletions onnxruntime/core/providers/rocm/rocm_stream_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<void*[]> buffers;
// CPU buffer buffers[i].
// Number of buffer points in "buffers".
size_t n_buffers;
Expand All @@ -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() {
Expand All @@ -106,7 +105,7 @@ Status RocmStream::CleanUpOnRunEnd() {
if (release_cpu_buffer_on_rocm_stream_ && cpu_allocator_->Info().alloc_type == OrtArenaAllocator) {
std::unique_ptr<CpuBuffersInfo> cpu_buffers_info = std::make_unique<CpuBuffersInfo>();
cpu_buffers_info->allocator = cpu_allocator_;
cpu_buffers_info->buffers = new void*[deferred_cpu_buffers_.size()];
cpu_buffers_info->buffers = std::make_unique<void*[]>(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);
}
Expand Down

0 comments on commit 2068402

Please sign in to comment.