diff --git a/src/libs/cplusplus/TypeOfExpression.cpp b/src/libs/cplusplus/TypeOfExpression.cpp
index 7dd669365fc3ec8bf3be89b6cf5e7c8ca8210759..487c9f8a46969d1c28b2b6c1fe84cb2020f7ce7c 100644
--- a/src/libs/cplusplus/TypeOfExpression.cpp
+++ b/src/libs/cplusplus/TypeOfExpression.cpp
@@ -48,6 +48,7 @@ TypeOfExpression::TypeOfExpression():
 void TypeOfExpression::setDocuments(const QMap<QString, Document::Ptr> &documents)
 {
     m_documents = documents;
+    m_lookupContext = LookupContext();
 }
 
 QList<TypeOfExpression::Result> TypeOfExpression::operator()(const QString &expression,
diff --git a/src/libs/cplusplus/TypeOfExpression.h b/src/libs/cplusplus/TypeOfExpression.h
index ecdfac7b2907c1ea7563ed1ee3d54d36b95856de..cd7a23441fde133111e5f987951d0bcbb2bc51e2 100644
--- a/src/libs/cplusplus/TypeOfExpression.h
+++ b/src/libs/cplusplus/TypeOfExpression.h
@@ -54,6 +54,9 @@ public:
     /**
      * Sets the documents used to evaluate expressions. Should be set before
      * calling this functor.
+     *
+     * Also clears the lookup context, so can be used to make sure references
+     * to the documents previously used are removed.
      */
     void setDocuments(const QMap<QString, Document::Ptr> &documents);
 
diff --git a/src/plugins/cpptools/cppcodecompletion.cpp b/src/plugins/cpptools/cppcodecompletion.cpp
index 47ac5c57678d6c3bcd84a37ac30d53ff025cd9cd..d24f5a5f81d167f82836eff5e1f0af9f5bd72ae4 100644
--- a/src/plugins/cpptools/cppcodecompletion.cpp
+++ b/src/plugins/cpptools/cppcodecompletion.cpp
@@ -1027,6 +1027,10 @@ bool CppCodeCompletion::partiallyComplete(const QList<TextEditor::CompletionItem
 void CppCodeCompletion::cleanup()
 {
     m_completions.clear();
+
+    // Set empty map in order to avoid referencing old versions of the documents
+    // until the next completion
+    typeOfExpression.setDocuments(QMap<QString, Document::Ptr>());
 }
 
 int CppCodeCompletion::findStartOfName(const TextEditor::ITextEditor *editor)