table: Ensure that Cs::erase removes the item from the cleanup index

Change-Id: Iefe658904829fa662c4854c38a5d57dad5e80222
Refs: #2348
diff --git a/daemon/table/cs.cpp b/daemon/table/cs.cpp
index e575694..1654bb2 100644
--- a/daemon/table/cs.cpp
+++ b/daemon/table/cs.cpp
@@ -757,7 +757,11 @@
               // it can happen when begin() contains the element we want to remove
               if (!isIterated && ((*head)->getFullName() == exactName))
                 {
-                  eraseFromSkipList(*head);
+                  cs::Entry* entryToDelete = *head;
+                  NFD_LOG_TRACE("Found target " << entryToDelete->getFullName());
+                  eraseFromSkipList(entryToDelete);
+                  // head can become invalid after eraseFromSkipList
+                  m_cleanupIndex.remove(entryToDelete);
                   return;
                 }
               else
@@ -802,8 +806,11 @@
 
   if (isNameIdentical)
     {
-      NFD_LOG_TRACE("Found target " << (*head)->getFullName());
-      eraseFromSkipList(*head);
+      cs::Entry* entryToDelete = *head;
+      NFD_LOG_TRACE("Found target " << entryToDelete->getFullName());
+      eraseFromSkipList(entryToDelete);
+      // head can become invalid after eraseFromSkipList
+      m_cleanupIndex.remove(entryToDelete);
     }
 }