Skip to content

Commit a6ef7e0

Browse files
committed
move to weak_ptr and avoid double dispose
1 parent 843a961 commit a6ef7e0

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

sycl/source/detail/device_global_map_entry.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ DeviceGlobalUSMMem::~DeviceGlobalUSMMem() {
2121
// removeAssociatedResources is expected to have cleaned up both the pointer
2222
// and the event. When asserts are enabled the values are set, so we check
2323
// these here.
24-
// CP
25-
auto ContextImplPtr = MAllocatingContext.get();
26-
if (MPtr != nullptr && ContextImplPtr != nullptr) {
27-
detail::usm::freeInternal(MPtr, ContextImplPtr);
28-
MPtr = nullptr;
29-
}
30-
if (MInitEvent != nullptr && ContextImplPtr != nullptr) {
31-
ContextImplPtr->getAdapter().call<UrApiKind::urEventRelease>(MInitEvent);
32-
MInitEvent = nullptr;
24+
auto ContextImplPtr = MAllocatingContext.lock();
25+
if (ContextImplPtr) {
26+
if (MPtr != nullptr) {
27+
detail::usm::freeInternal(MPtr, ContextImplPtr.get());
28+
MPtr = nullptr;
29+
}
30+
if (MInitEvent != nullptr) {
31+
ContextImplPtr->getAdapter().call<UrApiKind::urEventRelease>(MInitEvent);
32+
MInitEvent = nullptr;
33+
}
3334
}
34-
MAllocatingContext.reset();
3535

3636
assert(MPtr == nullptr && "MPtr has not been cleaned up.");
3737
assert(MInitEvent == nullptr && "MInitEvent has not been cleaned up.");
@@ -175,12 +175,9 @@ void DeviceGlobalMapEntry::removeAssociatedResources(
175175
if (USMMem.MInitEvent != nullptr)
176176
CtxImpl->getAdapter().call<UrApiKind::urEventRelease>(
177177
USMMem.MInitEvent);
178-
#ifndef NDEBUG
179-
// For debugging we set the event and memory to some recognizable values
180-
// to allow us to check that this cleanup happens before erasure.
178+
// Set to nullptr to avoid double free.
181179
USMMem.MPtr = nullptr;
182180
USMMem.MInitEvent = nullptr;
183-
#endif
184181
MDeviceToUSMPtrMap.erase(USMPtrIt);
185182
}
186183
}
@@ -199,12 +196,9 @@ void DeviceGlobalMapEntry::cleanup() {
199196
detail::usm::freeInternal(USMMem.MPtr, CtxImpl);
200197
if (USMMem.MInitEvent != nullptr)
201198
CtxImpl->getAdapter().call<UrApiKind::urEventRelease>(USMMem.MInitEvent);
202-
#ifndef NDEBUG
203-
// For debugging we set the event and memory to some recognizable values
204-
// to allow us to check that this cleanup happens before erasure.
199+
// Set to nullptr to avoid double free.
205200
USMMem.MPtr = nullptr;
206201
USMMem.MInitEvent = nullptr;
207-
#endif
208202
}
209203
MDeviceToUSMPtrMap.clear();
210204
}

sycl/source/detail/device_global_map_entry.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct DeviceGlobalUSMMem {
4646
std::mutex MInitEventMutex;
4747
ur_event_handle_t MInitEvent = nullptr;
4848

49-
std::shared_ptr<context_impl> MAllocatingContext;
49+
std::weak_ptr<context_impl> MAllocatingContext;
5050
friend struct DeviceGlobalMapEntry;
5151
};
5252

0 commit comments

Comments
 (0)