diff --git a/source/adapters/cuda/event.cpp b/source/adapters/cuda/event.cpp index 804b35a9b7..b4705a99d5 100644 --- a/source/adapters/cuda/event.cpp +++ b/source/adapters/cuda/event.cpp @@ -283,8 +283,17 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle( std::unique_ptr EventPtr{nullptr}; - *phEvent = ur_event_handle_t_::makeWithNative( - hContext, reinterpret_cast(hNativeEvent)); + try { + EventPtr = + std::unique_ptr(ur_event_handle_t_::makeWithNative( + hContext, reinterpret_cast(hNativeEvent))); + } catch (const std::bad_alloc &) { + return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY; + } catch (...) { + return UR_RESULT_ERROR_UNKNOWN; + } + + *phEvent = EventPtr.release(); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/hip/event.cpp b/source/adapters/hip/event.cpp index 313212724a..5cf5f98c92 100644 --- a/source/adapters/hip/event.cpp +++ b/source/adapters/hip/event.cpp @@ -330,8 +330,19 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle( ur_event_handle_t *phEvent) { std::ignore = pProperties; - *phEvent = ur_event_handle_t_::makeWithNative( - hContext, reinterpret_cast(hNativeEvent)); + std::unique_ptr EventPtr{nullptr}; + + try { + EventPtr = + std::unique_ptr(ur_event_handle_t_::makeWithNative( + hContext, reinterpret_cast(hNativeEvent))); + } catch (const std::bad_alloc &) { + return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY; + } catch (...) { + return UR_RESULT_ERROR_UNKNOWN; + } + + *phEvent = EventPtr.release(); return UR_RESULT_SUCCESS; }