@@ -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}
0 commit comments