[ndnSIM] util: Prevent potential segfault in Scheduler when canceling all events

Change-Id: I3c26f66cd3417c9f00b3fc9ac9ae465a5067952e
diff --git a/src/util/scheduler.cpp b/src/util/scheduler.cpp
index 09a3f26..523d005 100644
--- a/src/util/scheduler.cpp
+++ b/src/util/scheduler.cpp
@@ -115,11 +115,14 @@
 void
 Scheduler::cancelAllEvents()
 {
-  for (auto i = m_events.begin(); i != m_events.end(); i++) {
+  for (auto i = m_events.begin(); i != m_events.end(); ) {
+    auto next = i;
+    ++next; // ns3::Simulator::Remove can call cancelEvent
     if ((*i) != nullptr) {
       ns3::Simulator::Remove((**i));
       const_cast<EventId&>(*i).reset();
     }
+    i = next;
   }
   m_events.clear();
 }