diff --git a/Code/Mantid/Framework/Algorithms/src/ExtractMaskToTable.cpp b/Code/Mantid/Framework/Algorithms/src/ExtractMaskToTable.cpp
index 134d98190e8242b6eef2aa7b45fc86eb6c8f8e18..8555def2b1e7aa967e5ab1fe249fb829557022a5 100644
--- a/Code/Mantid/Framework/Algorithms/src/ExtractMaskToTable.cpp
+++ b/Code/Mantid/Framework/Algorithms/src/ExtractMaskToTable.cpp
@@ -415,7 +415,9 @@ namespace Algorithms
     {
       detid_t tmpid = minuend[i];
       fiter = lower_bound(firstsubiter, subtrahend.end(), tmpid);
-      bool exist = *fiter == tmpid;
+      bool exist(false);
+      if (fiter != subtrahend.end())
+          exist = *fiter == tmpid;
       if (!exist)
       {
         diff.push_back(tmpid);