diff --git a/mindspore/ccsrc/minddata/dataset/util/allocator.h b/mindspore/ccsrc/minddata/dataset/util/allocator.h index 744844aba8..76b98a45b0 100644 --- a/mindspore/ccsrc/minddata/dataset/util/allocator.h +++ b/mindspore/ccsrc/minddata/dataset/util/allocator.h @@ -92,21 +92,27 @@ template Status MakeUnique(std::unique_ptr> *out, Allocator alloc, size_t n, Args &&... args) { RETURN_UNEXPECTED_IF_NULL(out); CHECK_FAIL_RETURN_UNEXPECTED(n > 0, "size must be positive"); - T *data = alloc.allocate(n); - if (!std::is_arithmetic::value) { - for (auto i = 0; i < n; i++) { - std::allocator_traits>::construct(alloc, &(data[i]), std::forward(args)...); - } - } - auto deleter = [](T *p, Allocator f_alloc, size_t f_n) { - if (!std::is_arithmetic::value && std::is_destructible::value) { - for (auto i = 0; i < f_n; ++i) { - std::allocator_traits>::destroy(f_alloc, &p[i]); + try { + T *data = alloc.allocate(n); + if (!std::is_arithmetic::value) { + for (auto i = 0; i < n; i++) { + std::allocator_traits>::construct(alloc, &(data[i]), std::forward(args)...); } } - f_alloc.deallocate(p, f_n); - }; - *out = std::unique_ptr>(data, std::bind(deleter, std::placeholders::_1, alloc, n)); + auto deleter = [](T *p, Allocator f_alloc, size_t f_n) { + if (!std::is_arithmetic::value && std::is_destructible::value) { + for (auto i = 0; i < f_n; ++i) { + std::allocator_traits>::destroy(f_alloc, &p[i]); + } + } + f_alloc.deallocate(p, f_n); + }; + *out = std::unique_ptr>(data, std::bind(deleter, std::placeholders::_1, alloc, n)); + } catch (const std::bad_alloc &e) { + return Status(StatusCode::kOutOfMemory); + } catch (const std::exception &e) { + RETURN_STATUS_UNEXPECTED(e.what()); + } return Status::OK(); }