Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,6 @@ pi_result _pi_context::finalize() {

// Destroy the command list used for initializations
ZE_CALL(zeCommandListDestroy(ZeCommandListInit));

// Destruction of some members of pi_context uses L0 context
// and therefore it must be valid at that point.
// Technically it should be placed to the destructor of pi_context
// but this makes API error handling more complex.
ZE_CALL(zeContextDestroy(ZeContext));
return PI_SUCCESS;
}

Expand Down Expand Up @@ -1830,9 +1824,22 @@ pi_result piContextRelease(pi_context Context) {

assert(Context);
if (--(Context->RefCount) == 0) {
auto ZeContext = Context->ZeContext;

// Clean up any live memory associated with Context
pi_result Result = Context->finalize();

// We must delete Context first and then destroy zeContext because
// Context deallocation requires ZeContext in some member deallocation of
// pi_context.
delete Context;

// Destruction of some members of pi_context uses L0 context
// and therefore it must be valid at that point.
// Technically it should be placed to the destructor of pi_context
// but this makes API error handling more complex.
ZE_CALL(zeContextDestroy(ZeContext));

return Result;
}
return PI_SUCCESS;
Expand Down