diff --git a/paddle/fluid/memory/allocation/stream_safe_custom_device_allocator.cc b/paddle/fluid/memory/allocation/stream_safe_custom_device_allocator.cc index b88f40f6a273292bee8c60e7e62d5c79552afa59..ce63ab807e01e71dc187299944731f3519303210 100644 --- a/paddle/fluid/memory/allocation/stream_safe_custom_device_allocator.cc +++ b/paddle/fluid/memory/allocation/stream_safe_custom_device_allocator.cc @@ -88,8 +88,7 @@ bool StreamSafeCustomDeviceAllocation::CanBeFreed() { } std::call_once(once_flag_, [this] { phi::DeviceManager::SetDevice(place_); }); for (auto it = outstanding_event_map_.begin(); - it != outstanding_event_map_.end(); - ++it) { + it != outstanding_event_map_.end();) { auto& event = it->second; if (!event->Query()) { VLOG(9) << "Event " << event->raw_event() << " for " << ptr() @@ -98,6 +97,7 @@ bool StreamSafeCustomDeviceAllocation::CanBeFreed() { } VLOG(8) << "Destroy event " << event->raw_event(); event->Destroy(); + it = outstanding_event_map_.erase(it); } outstanding_event_map_.clear(); return true;