@@ -1990,19 +1990,30 @@ ur_result_t _ur_buffer::getZeHandle(char *&ZeHandle, access_mode_t AccessMode,
19901990
19911991 auto &Allocation = Allocations[Device];
19921992
1993+ if (this ->isFreed ) {
1994+ die (" getZeHandle() buffer already released, no valid handles." );
1995+ }
1996+
19931997 // Sub-buffers don't maintain own allocations but rely on parent buffer.
19941998 if (SubBuffer) {
1995- UR_CALL (SubBuffer->Parent ->getZeHandle (ZeHandle, AccessMode, Device,
1996- phWaitEvents, numWaitEvents));
1997- ZeHandle += SubBuffer->Origin ;
1998- // Still store the allocation info in the PI sub-buffer for
1999- // getZeHandlePtr to work. At least zeKernelSetArgumentValue needs to
2000- // be given a pointer to the allocation handle rather than its value.
2001- //
2002- Allocation.ZeHandle = ZeHandle;
2003- Allocation.ReleaseAction = allocation_t ::keep;
2004- LastDeviceWithValidAllocation = Device;
2005- return UR_RESULT_SUCCESS;
1999+ // Verify that the Parent Buffer is still valid or if it has been freed.
2000+ if (SubBuffer->Parent && !SubBuffer->Parent ->isFreed ) {
2001+ UR_CALL (SubBuffer->Parent ->getZeHandle (ZeHandle, AccessMode, Device,
2002+ phWaitEvents, numWaitEvents));
2003+ ZeHandle += SubBuffer->Origin ;
2004+ // Still store the allocation info in the PI sub-buffer for
2005+ // getZeHandlePtr to work. At least zeKernelSetArgumentValue needs to
2006+ // be given a pointer to the allocation handle rather than its value.
2007+ //
2008+ Allocation.ZeHandle = ZeHandle;
2009+ Allocation.ReleaseAction = allocation_t ::keep;
2010+ LastDeviceWithValidAllocation = Device;
2011+ return UR_RESULT_SUCCESS;
2012+ } else {
2013+ // Return an error if the parent buffer is already gone.
2014+ die (" getZeHandle() SubBuffer's parent already released, no valid "
2015+ " handles." );
2016+ }
20062017 }
20072018
20082019 // First handle case where the buffer is represented by only
@@ -2263,6 +2274,7 @@ ur_result_t _ur_buffer::free() {
22632274 die (" _ur_buffer::free(): Unhandled release action" );
22642275 }
22652276 ZeHandle = nullptr ; // don't leave hanging pointers
2277+ this ->isFreed = true ;
22662278 }
22672279 return UR_RESULT_SUCCESS;
22682280}
0 commit comments