@@ -2045,19 +2045,30 @@ ur_result_t _ur_buffer::getZeHandle(char *&ZeHandle, access_mode_t AccessMode,
2045
2045
2046
2046
auto &Allocation = Allocations[Device];
2047
2047
2048
+ if (this ->isFreed ) {
2049
+ die (" getZeHandle() buffer already released, no valid handles." );
2050
+ }
2051
+
2048
2052
// Sub-buffers don't maintain own allocations but rely on parent buffer.
2049
2053
if (SubBuffer) {
2050
- UR_CALL (SubBuffer->Parent ->getZeHandle (ZeHandle, AccessMode, Device,
2051
- phWaitEvents, numWaitEvents));
2052
- ZeHandle += SubBuffer->Origin ;
2053
- // Still store the allocation info in the PI sub-buffer for
2054
- // getZeHandlePtr to work. At least zeKernelSetArgumentValue needs to
2055
- // be given a pointer to the allocation handle rather than its value.
2056
- //
2057
- Allocation.ZeHandle = ZeHandle;
2058
- Allocation.ReleaseAction = allocation_t ::keep;
2059
- LastDeviceWithValidAllocation = Device;
2060
- return UR_RESULT_SUCCESS;
2054
+ // Verify that the Parent Buffer is still valid or if it has been freed.
2055
+ if (SubBuffer->Parent && !SubBuffer->Parent ->isFreed ) {
2056
+ UR_CALL (SubBuffer->Parent ->getZeHandle (ZeHandle, AccessMode, Device,
2057
+ phWaitEvents, numWaitEvents));
2058
+ ZeHandle += SubBuffer->Origin ;
2059
+ // Still store the allocation info in the PI sub-buffer for
2060
+ // getZeHandlePtr to work. At least zeKernelSetArgumentValue needs to
2061
+ // be given a pointer to the allocation handle rather than its value.
2062
+ //
2063
+ Allocation.ZeHandle = ZeHandle;
2064
+ Allocation.ReleaseAction = allocation_t ::keep;
2065
+ LastDeviceWithValidAllocation = Device;
2066
+ return UR_RESULT_SUCCESS;
2067
+ } else {
2068
+ // Return an error if the parent buffer is already gone.
2069
+ die (" getZeHandle() SubBuffer's parent already released, no valid "
2070
+ " handles." );
2071
+ }
2061
2072
}
2062
2073
2063
2074
// First handle case where the buffer is represented by only
@@ -2320,6 +2331,7 @@ ur_result_t _ur_buffer::free() {
2320
2331
die (" _ur_buffer::free(): Unhandled release action" );
2321
2332
}
2322
2333
ZeHandle = nullptr ; // don't leave hanging pointers
2334
+ this ->isFreed = true ;
2323
2335
}
2324
2336
return UR_RESULT_SUCCESS;
2325
2337
}
0 commit comments