[MCJIT] Fix an iterator invalidation bug in MCJIT::finalizeObject.
[oota-llvm.git] / lib / ExecutionEngine / MCJIT / MCJIT.cpp
index 3dd205751d1be9b60124d895f7a448a0e4a80edb..8ff41ffd7d677f51af72dca49822c7ae5043f91a 100644 (file)
@@ -225,12 +225,14 @@ void MCJIT::finalizeLoadedModules() {
 void MCJIT::finalizeObject() {
   MutexGuard locked(lock);
 
-  for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
-                              E = OwnedModules.end_added();
-       I != E; ++I) {
-    Module *M = *I;
+  // Generate code for module is going to move objects out of the 'added' list,
+  // so we need to copy that out before using it:
+  SmallVector<Module*, 16> ModsToAdd;
+  for (auto M : OwnedModules.added())
+    ModsToAdd.push_back(M);
+
+  for (auto M : ModsToAdd)
     generateCodeForModule(M);
-  }
 
   finalizeLoadedModules();
 }