Skip to content

Commit 255f304

Browse files
[SYCL] Fix leak of device handle (#2671)
There is a circular dependency between the device_impl and the platform_impl, each holding a shared pointer to the other, which prevents their destruction. The patch replaces the vector of shared pointers in the platform_impl with a vector of weak_ptr. Signed-off-by: Chris Perkins <[email protected]>
1 parent 7ecdd0a commit 255f304

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

sycl/source/detail/platform_impl.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,11 @@ std::shared_ptr<device_impl> platform_impl::getOrMakeDeviceImpl(
300300
const std::lock_guard<std::mutex> Guard(MDeviceMapMutex);
301301

302302
// If we've already seen this device, return the impl
303-
for (const std::shared_ptr<device_impl> &Device : MDeviceCache) {
304-
if (Device->getHandleRef() == PiDevice)
305-
return Device;
303+
for (const std::weak_ptr<device_impl> &DeviceWP : MDeviceCache) {
304+
if (std::shared_ptr<device_impl> Device = DeviceWP.lock()) {
305+
if (Device->getHandleRef() == PiDevice)
306+
return Device;
307+
}
306308
}
307309

308310
// Otherwise make the impl

sycl/source/detail/platform_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class platform_impl {
195195
bool MHostPlatform = false;
196196
RT::PiPlatform MPlatform = 0;
197197
std::shared_ptr<plugin> MPlugin;
198-
std::vector<std::shared_ptr<device_impl>> MDeviceCache;
198+
std::vector<std::weak_ptr<device_impl>> MDeviceCache;
199199
std::mutex MDeviceMapMutex;
200200
};
201201

sycl/test/basic_tests/queue/release.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ int main() {
1919
//CHECK: ---> piContextRelease(
2020
//CHECK: ---> piKernelRelease(
2121
//CHECK: ---> piProgramRelease(
22+
//CHECK: ---> piDeviceRelease(

0 commit comments

Comments
 (0)