diff --git a/sycl/source/detail/device_global_map_entry.cpp b/sycl/source/detail/device_global_map_entry.cpp index 25704caaee6de..b5dec899f2759 100644 --- a/sycl/source/detail/device_global_map_entry.cpp +++ b/sycl/source/detail/device_global_map_entry.cpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace sycl { inline namespace _V1 { @@ -21,16 +22,21 @@ DeviceGlobalUSMMem::~DeviceGlobalUSMMem() { // removeAssociatedResources is expected to have cleaned up both the pointer // and the event. When asserts are enabled the values are set, so we check // these here. - auto ContextImplPtr = MAllocatingContext.lock(); - if (ContextImplPtr) { - if (MPtr != nullptr) { - detail::usm::freeInternal(MPtr, ContextImplPtr.get()); - MPtr = nullptr; - } - if (MInitEvent != nullptr) { - ContextImplPtr->getAdapter().call(MInitEvent); - MInitEvent = nullptr; + try { + auto ContextImplPtr = MAllocatingContext.lock(); + if (ContextImplPtr) { + if (MPtr != nullptr) { + detail::usm::freeInternal(MPtr, ContextImplPtr.get()); + MPtr = nullptr; + } + if (MInitEvent != nullptr) { + ContextImplPtr->getAdapter().call( + MInitEvent); + MInitEvent = nullptr; + } } + } catch (std::exception &e) { + __SYCL_REPORT_EXCEPTION_TO_STREAM("exception in ~DeviceGlobalUSMMem", e); } assert(MPtr == nullptr && "MPtr has not been cleaned up.");