@@ -2045,19 +2045,30 @@ ur_result_t _ur_buffer::getZeHandle(char *&ZeHandle, access_mode_t AccessMode,
20452045
20462046 auto &Allocation = Allocations[Device];
20472047
2048+ if (this ->isFreed ) {
2049+ die (" getZeHandle() buffer already released, no valid handles." );
2050+ }
2051+
20482052 // Sub-buffers don't maintain own allocations but rely on parent buffer.
20492053 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+ }
20612072 }
20622073
20632074 // First handle case where the buffer is represented by only
@@ -2320,6 +2331,7 @@ ur_result_t _ur_buffer::free() {
23202331 die (" _ur_buffer::free(): Unhandled release action" );
23212332 }
23222333 ZeHandle = nullptr ; // don't leave hanging pointers
2334+ this ->isFreed = true ;
23232335 }
23242336 return UR_RESULT_SUCCESS;
23252337}
0 commit comments