Change Dict::lookupNF return a const &

Saves some copy()
diff --git a/poppler/Annot.cc b/poppler/Annot.cc
index 467836d..80e3a99 100644
--- a/poppler/Annot.cc
+++ b/poppler/Annot.cc
@@ -891,22 +891,22 @@
   // Obtain dictionary or stream associated to appearance type
   switch (type) {
   case appearRollover:
-    apData = appearDict.dictLookupNF("R");
+    apData = appearDict.dictLookupNF("R").copy();
     if (apData.isNull())
-      apData = appearDict.dictLookupNF("N");
+      apData = appearDict.dictLookupNF("N").copy();
     break;
   case appearDown:
-    apData = appearDict.dictLookupNF("D");
+    apData = appearDict.dictLookupNF("D").copy();
     if (apData.isNull())
-      apData = appearDict.dictLookupNF("N");
+      apData = appearDict.dictLookupNF("N").copy();
     break;
   case appearNormal:
-    apData = appearDict.dictLookupNF("N");
+    apData = appearDict.dictLookupNF("N").copy();
     break;
   }
 
   if (apData.isDict() && state)
-    return apData.dictLookupNF(state);
+    return apData.dictLookupNF(state).copy();
   else if (apData.isRef())
     return apData;
 
@@ -914,7 +914,7 @@
 }
 
 std::unique_ptr<GooString> AnnotAppearance::getStateKey(int i) {
-  Object obj1 = appearDict.dictLookupNF("N");
+  const Object &obj1 = appearDict.dictLookupNF("N");
   if (obj1.isDict())
     return std::make_unique<GooString>(obj1.dictGetKey(i));
   return nullptr;
@@ -922,7 +922,7 @@
 
 int AnnotAppearance::getNumStates() {
   int res = 0;
-  Object obj1 = appearDict.dictLookupNF("N");
+  const Object &obj1 = appearDict.dictLookupNF("N");
   if (obj1.isDict())
     res = obj1.dictGetLength();
   return res;
@@ -956,17 +956,17 @@
   bool found;
 
   // Scan each state's ref/subdictionary
-  obj1 = appearDict.dictLookupNF("N");
+  obj1 = appearDict.dictLookupNF("N").copy();
   found = referencesStream(&obj1, refToStream);
   if (found)
     return true;
 
-  obj1 = appearDict.dictLookupNF("R");
+  obj1 = appearDict.dictLookupNF("R").copy();
   found = referencesStream(&obj1, refToStream);
   if (found)
     return true;
 
-  obj1 = appearDict.dictLookupNF("D");
+  obj1 = appearDict.dictLookupNF("D").copy();
   found = referencesStream(&obj1, refToStream);
   return found;
 }
@@ -1011,11 +1011,11 @@
 
 void AnnotAppearance::removeAllStreams() {
   Object obj1;
-  obj1 = appearDict.dictLookupNF("N");
+  obj1 = appearDict.dictLookupNF("N").copy();
   removeStateStreams(&obj1);
-  obj1 = appearDict.dictLookupNF("R");
+  obj1 = appearDict.dictLookupNF("R").copy();
   removeStateStreams(&obj1);
-  obj1 = appearDict.dictLookupNF("D");
+  obj1 = appearDict.dictLookupNF("D").copy();
   removeStateStreams(&obj1);
 }
 
@@ -1226,7 +1226,7 @@
   }
 
   // Note: This value is overwritten by Annots ctor
-  obj1 = dict->lookupNF("P");
+  obj1 = dict->lookupNF("P").copy();
   if (obj1.isRef()) {
     Ref ref = obj1.getRef();
 
@@ -1303,7 +1303,7 @@
     treeKey = 0;
   }
 
-  oc = dict->lookupNF("OC");
+  oc = dict->lookupNF("OC").copy();
 }
 
 void Annot::getRect(double *x1, double *y1, double *x2, double *y2) const {
@@ -1866,7 +1866,7 @@
 }
 
 void AnnotPopup::initialize(PDFDoc *docA, Dict *dict) {
-  parent = dict->lookupNF("Parent");
+  parent = dict->lookupNF("Parent").copy();
   if (!parent.isRef()) {
     parent.setToNull();
   }
@@ -1905,7 +1905,7 @@
 AnnotMarkup::~AnnotMarkup() = default;
 
 void AnnotMarkup::initialize(PDFDoc *docA, Dict *dict) {
-  Object obj1, obj2;
+  Object obj1;
 
   obj1 = dict->lookup("T");
   if (obj1.isString()) {
@@ -1913,7 +1913,7 @@
   }
 
   Object popupObj = dict->lookup("Popup");
-  obj2 = dict->lookupNF("Popup");
+  const Object &obj2 = dict->lookupNF("Popup");
   if (popupObj.isDict() && obj2.isRef()) {
     popup = std::make_unique<AnnotPopup>(docA, std::move(popupObj), &obj2);
   }
@@ -1930,7 +1930,7 @@
     date.reset(obj1.getString()->copy());
   }
 
-  obj1 = dict->lookupNF("IRT");
+  obj1 = dict->lookupNF("IRT").copy();
   if (obj1.isRef()) {
     inReplyTo = obj1.getRef();
   } else {
@@ -2825,7 +2825,7 @@
       error(errSyntaxWarning, -1, "Font subdictionary is not a dictionary");
     } else {
       // Get the font dictionary for the actual requested font
-      Object fontDictionary = fontResources.getDict()->lookupNF(da.getFontName().getName());
+      Object fontDictionary = fontResources.getDict()->lookupNF(da.getFontName().getName()).copy();
 
       // Resolve reference, if necessary
       Ref fontReference = {-1, -1};
@@ -3760,7 +3760,7 @@
     action.reset(LinkAction::parseAction(&obj1, doc->getCatalog()->getBaseURI()));
   }
 
-  additionalActions = dict->lookupNF("AA");
+  additionalActions = dict->lookupNF("AA").copy();
 
   obj1 = dict->lookup("Parent");
   if (obj1.isDict()) {
@@ -5132,7 +5132,7 @@
     }
   }
 
-  additionalActions = dict->lookupNF("AA");
+  additionalActions = dict->lookupNF("AA").copy();
 
   obj1 = dict->lookup("MK");
   if (obj1.isDict()) {
diff --git a/poppler/Catalog.cc b/poppler/Catalog.cc
index ed11145..e958ad1 100644
--- a/poppler/Catalog.cc
+++ b/poppler/Catalog.cc
@@ -120,7 +120,7 @@
   }
 
   // actions
-  additionalActions = catDict.dictLookupNF("AA");
+  additionalActions = catDict.dictLookupNF("AA").copy();
 
   // get the ViewerPreferences dictionary
   viewerPreferences = catDict.dictLookup("ViewerPreferences");
@@ -213,7 +213,7 @@
     Object catDict = xref->getCatalog();
 
     if (catDict.isDict()) {
-      Object pagesDictRef = catDict.dictLookupNF("Pages");
+      const Object &pagesDictRef = catDict.dictLookupNF("Pages");
       if (pagesDictRef.isRef() &&
           pagesDictRef.getRefNum() >= 0 &&
           pagesDictRef.getRefNum() < xref->getNumObjects()) {
@@ -732,7 +732,7 @@
     // some PDF files actually use real numbers here ("/Count 9.0")
     if (!obj.isNum()) {
       if (pagesDict.dictIs("Page")) {
-	Object pageRootRef = catDict.dictLookupNF("Pages");
+	const Object &pageRootRef = catDict.dictLookupNF("Pages");
 
 	error(errSyntaxError, -1, "Pages top-level is a single Page. The document is malformed, trying to recover...");
 
diff --git a/poppler/Dict.cc b/poppler/Dict.cc
index 768d70b..383f7fd 100644
--- a/poppler/Dict.cc
+++ b/poppler/Dict.cc
@@ -16,7 +16,7 @@
 // Copyright (C) 2005 Kristian Høgsberg <krh@redhat.com>
 // Copyright (C) 2006 Krzysztof Kowalczyk <kkowalczyk@gmail.com>
 // Copyright (C) 2007-2008 Julien Rebetez <julienr@svn.gnome.org>
-// Copyright (C) 2008, 2010, 2013, 2014, 2017 Albert Astals Cid <aacid@kde.org>
+// Copyright (C) 2008, 2010, 2013, 2014, 2017, 2019 Albert Astals Cid <aacid@kde.org>
 // Copyright (C) 2010 Paweł Wiejacha <pawel.wiejacha@gmail.com>
 // Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
 // Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
@@ -168,11 +168,12 @@
   return Object(objNull);
 }
 
-Object Dict::lookupNF(const char *key) const {
+const Object &Dict::lookupNF(const char *key) const {
   if (const auto *entry = find(key)) {
-    return entry->second.copy();
+    return entry->second;
   }
-  return Object(objNull);
+  static Object nullObj(objNull);
+  return nullObj;
 }
 
 bool Dict::lookupInt(const char *key, const char *alt_key, int *value) const
diff --git a/poppler/Dict.h b/poppler/Dict.h
index 0747d40..db38131 100644
--- a/poppler/Dict.h
+++ b/poppler/Dict.h
@@ -76,7 +76,7 @@
   // Look up an entry and return the value.  Returns a null object
   // if <key> is not in the dictionary.
   Object lookup(const char *key, int recursion = 0) const;
-  Object lookupNF(const char *key) const;
+  const Object &lookupNF(const char *key) const;
   bool lookupInt(const char *key, const char *alt_key, int *value) const;
 
   // Iterative accessors.
diff --git a/poppler/FileSpec.cc b/poppler/FileSpec.cc
index 2087fe2..463e551 100644
--- a/poppler/FileSpec.cc
+++ b/poppler/FileSpec.cc
@@ -7,7 +7,7 @@
 //
 // Copyright (C) 2008-2009 Carlos Garcia Campos <carlosgc@gnome.org>
 // Copyright (C) 2009 Kovid Goyal <kovid@kovidgoyal.net>
-// Copyright (C) 2012, 2017, 2018 Albert Astals Cid <aacid@kde.org>
+// Copyright (C) 2012, 2017-2019 Albert Astals Cid <aacid@kde.org>
 // Copyright (C) 2012 Hib Eris <hib@hiberis.nl>
 // Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich
 // Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
@@ -127,7 +127,7 @@
   if (fileSpec.isDict()) {
     obj1 = fileSpec.dictLookup("EF");
     if (obj1.isDict()) {
-      fileStream = obj1.dictLookupNF("F");
+      fileStream = obj1.dictLookupNF("F").copy();
       if (!fileStream.isRef()) {
         ok = false;
         fileStream.setToNull();
diff --git a/poppler/FontInfo.cc b/poppler/FontInfo.cc
index 8467d65..161e597 100644
--- a/poppler/FontInfo.cc
+++ b/poppler/FontInfo.cc
@@ -99,7 +99,7 @@
 
   // scan the fonts in this resource dictionary
   gfxFontDict = nullptr;
-  Object fontObj = resDict->lookupNF("Font");
+  const Object &fontObj = resDict->lookupNF("Font");
   if (fontObj.isRef()) {
     Object obj2 = fontObj.fetch(xrefA);
     if (obj2.isDict()) {
diff --git a/poppler/Form.cc b/poppler/Form.cc
index 204bf4d..905e5c3 100644
--- a/poppler/Form.cc
+++ b/poppler/Form.cc
@@ -648,7 +648,7 @@
       const Ref ref = childRef.getRef();
       if (usedParents->find(ref.num) == usedParents->end()) {
         // Field child: it could be a form field or a widget or composed dict
-        Object obj2 = childObj.dictLookupNF("Parent");
+        Object obj2 = childObj.dictLookupNF("Parent").copy();
 	Object obj3 = childObj.dictLookup("Parent");
         if (obj2.isRef() || obj3.isDict()) {
           // Child is a form field or composed dict
@@ -1875,7 +1875,7 @@
   if (!obj.isNull()) {
     return obj;
   }
-  Object parent = dict->lookupNF("Parent");
+  const Object &parent = dict->lookupNF("Parent");
   if (parent.isRef()) {
     const Ref ref = parent.getRef();
     if (usedParents->find(ref.num) == usedParents->end()) {
diff --git a/poppler/Gfx.cc b/poppler/Gfx.cc
index c80786e..4da19c1 100644
--- a/poppler/Gfx.cc
+++ b/poppler/Gfx.cc
@@ -317,7 +317,6 @@
 
 GfxResources::GfxResources(XRef *xrefA, Dict *resDictA, GfxResources *nextA) :
     gStateCache(2), xref(xrefA) {
-  Object obj1, obj2;
   Ref r;
 
   if (resDictA) {
@@ -325,9 +324,9 @@
     // build font dictionary
     Dict *resDict = resDictA->copy(xref);
     fonts = nullptr;
-    obj1 = resDict->lookupNF("Font");
+    const Object &obj1 = resDict->lookupNF("Font");
     if (obj1.isRef()) {
-      obj2 = obj1.fetch(xref);
+      Object obj2 = obj1.fetch(xref);
       if (obj2.isDict()) {
 	r = obj1.getRef();
 	fonts = new GfxFontDict(xref, &r, obj2.getDict());
@@ -414,7 +413,7 @@
 
   for (resPtr = this; resPtr; resPtr = resPtr->next) {
     if (resPtr->xObjDict.isDict()) {
-      Object obj = resPtr->xObjDict.dictLookupNF(name);
+      Object obj = resPtr->xObjDict.dictLookupNF(name).copy();
       if (!obj.isNull())
 	return obj;
     }
@@ -428,7 +427,7 @@
 
   for (resPtr = this; resPtr; resPtr = resPtr->next) {
     if (resPtr->propertiesDict.isDict()) {
-      Object obj = resPtr->propertiesDict.dictLookupNF(name);
+      Object obj = resPtr->propertiesDict.dictLookupNF(name).copy();
       if (!obj.isNull())
 	return obj;
     }
@@ -457,7 +456,7 @@
 
   for (resPtr = this; resPtr; resPtr = resPtr->next) {
     if (resPtr->patternDict.isDict()) {
-      Object obj = resPtr->patternDict.dictLookupNF(name);
+      Object obj = resPtr->patternDict.dictLookupNF(name).copy();
       if (!obj.isNull()) {
 	Ref patternRef = { -1, -1 };
 	if (obj.isRef()) {
@@ -515,7 +514,7 @@
 
   for (resPtr = this; resPtr; resPtr = resPtr->next) {
     if (resPtr->gStateDict.isDict()) {
-      Object obj = resPtr->gStateDict.dictLookupNF(name);
+      Object obj = resPtr->gStateDict.dictLookupNF(name).copy();
       if (!obj.isNull()) {
 	return obj;
       }
@@ -4228,7 +4227,7 @@
 
   // check for optional content key
   if (ref) {
-    obj1 = dict->lookupNF("OC");
+    obj1 = dict->lookupNF("OC").copy();
     if (catalog->getOptContentConfig() && !catalog->getOptContentConfig()->optContentIsVisible(&obj1)) {
       return;
     }
@@ -4692,7 +4691,7 @@
 
   // check for optional content key
   ocSaved = ocState;
-  obj1 = dict->lookupNF("OC");
+  obj1 = dict->lookupNF("OC").copy();
   if (catalog->getOptContentConfig() && !catalog->getOptContentConfig()->optContentIsVisible(&obj1)) {
     if (out->needCharCount()) {
       ocState = false;
diff --git a/poppler/GfxFont.cc b/poppler/GfxFont.cc
index 172d837..44282d9 100644
--- a/poppler/GfxFont.cc
+++ b/poppler/GfxFont.cc
@@ -341,14 +341,14 @@
 
   Object fontDesc = fontDict2->lookup("FontDescriptor");
   if (fontDesc.isDict()) {
-    Object obj3 = fontDesc.dictLookupNF("FontFile");
+    Object obj3 = fontDesc.dictLookupNF("FontFile").copy();
     if (obj3.isRef()) {
       *embID = obj3.getRef();
       if (expectedType != fontType1) {
 	err = true;
       }
     }
-    if (embID->num == -1 && (obj3 = fontDesc.dictLookupNF("FontFile2"), obj3.isRef())) {
+    if (embID->num == -1 && (obj3 = fontDesc.dictLookupNF("FontFile2").copy(), obj3.isRef())) {
       *embID = obj3.getRef();
       if (isType0) {
 	expectedType = fontCIDType2;
@@ -356,7 +356,7 @@
 	err = true;
       }
     }
-    if (embID->num == -1 && (obj3 = fontDesc.dictLookupNF("FontFile3"), obj3.isRef())) {
+    if (embID->num == -1 && (obj3 = fontDesc.dictLookupNF("FontFile3").copy(), obj3.isRef())) {
       *embID = obj3.getRef();
       Object obj4 = obj3.fetch(xref);
       if (obj4.isStream()) {
@@ -1684,7 +1684,7 @@
 
 Object Gfx8BitFont::getCharProcNF(int code) {
   if (enc[code] && charProcs.isDict()) {
-    return charProcs.dictLookupNF(enc[code]);
+    return charProcs.dictLookupNF(enc[code]).copy();
   } else {
     return Object(objNull);
   }
diff --git a/poppler/Link.cc b/poppler/Link.cc
index 32fc41c..428e8b6 100644
--- a/poppler/Link.cc
+++ b/poppler/Link.cc
@@ -163,7 +163,7 @@
 
     // Prevent circles in the tree by checking the ref against used refs in
     // our current tree branch.
-    const Object nextRefObj = obj->dictLookupNF("Next");
+    const Object &nextRefObj = obj->dictLookupNF("Next");
     if (nextRefObj.isRef()) {
         const Ref ref = nextRefObj.getRef();
         if (!seenNextActions->insert(ref.num).second) {
@@ -637,7 +637,7 @@
   annotRef.num = -1;
   annotTitle = nullptr;
 
-  Object tmp = obj->dictLookupNF("Annotation");
+  Object tmp = obj->dictLookupNF("Annotation").copy();
   if (tmp.isRef()) {
     annotRef = tmp.getRef();
   }
@@ -758,7 +758,7 @@
 	  renditionObj.setToNull();
 	}
 
-	screenRef = obj->dictLookupNF("AN");
+	screenRef = obj->dictLookupNF("AN").copy();
 	if (!screenRef.isRef() && operation >= 0 && operation <= 4) {
 	  error(errSyntaxWarning, -1, "Invalid Rendition Action: no AN field with op = {0:d}", operationCode);
 	  screenRef.setToNull();
diff --git a/poppler/Movie.cc b/poppler/Movie.cc
index f4dfdf9..1a54023 100644
--- a/poppler/Movie.cc
+++ b/poppler/Movie.cc
@@ -6,7 +6,7 @@
 // Hugo Mercier <hmercier31[at]gmail.com> (c) 2008
 // Pino Toscano <pino@kde.org> (c) 2008
 // Carlos Garcia Campos <carlosgc@gnome.org> (c) 2010
-// Albert Astals Cid <aacid@kde.org> (c) 2010, 2017, 2018
+// Albert Astals Cid <aacid@kde.org> (c) 2010, 2017-2019
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -213,7 +213,7 @@
   //
   // movie poster
   //
-  poster = movieDict->dictLookupNF("Poster");
+  poster = movieDict->dictLookupNF("Poster").copy();
   if (!poster.isNull()) {
     if (poster.isRef() || poster.isStream()) {
       showPoster = true;
diff --git a/poppler/Object.h b/poppler/Object.h
index 4f07efa..77a40cd 100644
--- a/poppler/Object.h
+++ b/poppler/Object.h
@@ -280,7 +280,7 @@
   void dictRemove(const char *key);
   bool dictIs(const char *dictType) const;
   Object dictLookup(const char *key, int recursion = 0) const;
-  Object dictLookupNF(const char *key) const;
+  const Object &dictLookupNF(const char *key) const;
   const char *dictGetKey(int i) const;
   Object dictGetVal(int i) const;
   const Object &dictGetValNF(int i) const;
@@ -368,7 +368,7 @@
 inline Object Object::dictLookup(const char *key, int recursion) const
   { OBJECT_TYPE_CHECK(objDict); return dict->lookup(key, recursion); }
 
-inline Object Object::dictLookupNF(const char *key) const
+inline const Object &Object::dictLookupNF(const char *key) const
   { OBJECT_TYPE_CHECK(objDict); return dict->lookupNF(key); }
 
 inline const char *Object::dictGetKey(int i) const
diff --git a/poppler/OptionalContent.cc b/poppler/OptionalContent.cc
index f4906bf..23acd01 100644
--- a/poppler/OptionalContent.cc
+++ b/poppler/OptionalContent.cc
@@ -163,7 +163,7 @@
     if (ve.isArray()) {
       result = evalOCVisibilityExpr(&ve, 0);
     } else {
-      Object ocg = dict->lookupNF("OCGs");
+      const Object &ocg = dict->lookupNF("OCGs");
       if (ocg.isArray()) {
         Object policy = dict->lookup("P");
         if (policy.isName("AllOn")) {
diff --git a/poppler/Outline.cc b/poppler/Outline.cc
index b17ffdf..1b7fb8d 100644
--- a/poppler/Outline.cc
+++ b/poppler/Outline.cc
@@ -14,7 +14,7 @@
 // under GPL version 2 or later
 //
 // Copyright (C) 2005 Marco Pesenti Gritti <mpg@redhat.com>
-// Copyright (C) 2008, 2016-2018 Albert Astals Cid <aacid@kde.org>
+// Copyright (C) 2008, 2016-2019 Albert Astals Cid <aacid@kde.org>
 // Copyright (C) 2009 Nick Jones <nick.jones@network-box.com>
 // Copyright (C) 2016 Jason Crain <jason@aquaticape.us>
 // Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
@@ -44,7 +44,7 @@
   if (!outlineObj->isDict()) {
     return;
   }
-  Object first = outlineObj->dictLookupNF("First");
+  const Object &first = outlineObj->dictLookupNF("First");
   items = OutlineItem::readItemList(nullptr, &first, xref);
 }
 
@@ -85,9 +85,9 @@
     }
   }
 
-  firstRef = dict->lookupNF("First");
-  lastRef = dict->lookupNF("Last");
-  nextRef = dict->lookupNF("Next");
+  firstRef = dict->lookupNF("First").copy();
+  lastRef = dict->lookupNF("Last").copy();
+  nextRef = dict->lookupNF("Next").copy();
 
   startsOpen = false;
   obj1 = dict->lookup("Count");
diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc
index 6526711..75f169e 100644
--- a/poppler/PDFDoc.cc
+++ b/poppler/PDFDoc.cc
@@ -925,7 +925,7 @@
     markPageObjects(infoDict, yRef, countRef, 0, refPage->num, rootNum + 2);
     if (trailerObj->isDict()) {
       Dict *trailerDict = trailerObj->getDict();
-      Object ref = trailerDict->lookupNF("Info");
+      const Object &ref = trailerDict->lookupNF("Info");
       if (ref.isRef()) {
         yRef->add(ref.getRef().num, ref.getRef().gen, 0, true);
         if (getXRef()->getEntry(ref.getRef().num)->type == xrefEntryCompressed) {
@@ -939,7 +939,7 @@
   Object catObj = getXRef()->getCatalog();
   Dict *catDict = catObj.getDict();
   Object pagesObj = catDict->lookup("Pages");
-  Object afObj = catDict->lookupNF("AcroForm");
+  Object afObj = catDict->lookupNF("AcroForm").copy();
   if (!afObj.isNull()) {
     markAcroForm(&afObj, yRef, countRef, 0, refPage->num, rootNum + 2);
   }
@@ -958,7 +958,7 @@
     }
   }
   markPageObjects(pageDict, yRef, countRef, 0, refPage->num, rootNum + 2);
-  Object annotsObj = pageDict->lookupNF("Annots");
+  Object annotsObj = pageDict->lookupNF("Annots").copy();
   if (!annotsObj.isNull()) {
     markAnnotations(&annotsObj, yRef, countRef, 0, refPage->num, rootNum + 2);
   }
@@ -1550,7 +1550,7 @@
 
   bool hasEncrypt = false;
   if (!xRef->getTrailerDict()->isNone()) {
-    Object obj2 = xRef->getTrailerDict()->dictLookupNF("Encrypt");
+    Object obj2 = xRef->getTrailerDict()->dictLookupNF("Encrypt").copy();
     if (!obj2.isNull()) {
       trailerDict->set("Encrypt", std::move(obj2));
       hasEncrypt = true;
@@ -1807,7 +1807,7 @@
           Dict *dict = obj1.getDict();
           Object type = dict->lookup("Type");
           if (type.isName() && strcmp(type.getName(), "Annot") == 0) {
-            Object obj2 = dict->lookupNF("P");
+            const Object &obj2 = dict->lookupNF("P");
             if (obj2.isRef()) {
               if (obj2.getRef().num == oldPageNum) {
                 const Object &obj3 = array->getNF(i);
diff --git a/poppler/PSOutputDev.cc b/poppler/PSOutputDev.cc
index 32f11fc..ee07a6f 100644
--- a/poppler/PSOutputDev.cc
+++ b/poppler/PSOutputDev.cc
@@ -1854,7 +1854,7 @@
   int i;
 
   gfxFontDict = nullptr;
-  Object obj1 = resDict->lookupNF("Font");
+  const Object &obj1 = resDict->lookupNF("Font");
   if (obj1.isRef()) {
     Object obj2 = obj1.fetch(xref);
     if (obj2.isDict()) {
diff --git a/poppler/Page.cc b/poppler/Page.cc
index 33c624f..2cb7514 100644
--- a/poppler/Page.cc
+++ b/poppler/Page.cc
@@ -258,7 +258,7 @@
   attrs->clipBoxes();
 
   // transtion
-  trans = pageObj.dictLookupNF("Trans");
+  trans = pageObj.dictLookupNF("Trans").copy();
   if (!(trans.isRef() || trans.isDict() || trans.isNull())) {
     error(errSyntaxError, -1, "Page transition object (page {0:d}) is wrong type ({1:s})",
 	  num, trans.getTypeName());
@@ -266,7 +266,7 @@
   }
 
   // duration
-  Object tmp = pageObj.dictLookupNF("Dur");
+  const Object &tmp = pageObj.dictLookupNF("Dur");
   if (!(tmp.isNum() || tmp.isNull())) {
     error(errSyntaxError, -1, "Page duration object (page {0:d}) is wrong type ({1:s})",
 	  num, tmp.getTypeName());
@@ -275,7 +275,7 @@
   }
 
   // annotations
-  annotsObj = pageObj.dictLookupNF("Annots");
+  annotsObj = pageObj.dictLookupNF("Annots").copy();
   if (!(annotsObj.isRef() || annotsObj.isArray() || annotsObj.isNull())) {
     error(errSyntaxError, -1, "Page annotations object (page {0:d}) is wrong type ({1:s})",
 	  num, annotsObj.getTypeName());
@@ -283,7 +283,7 @@
   }
 
   // contents
-  contents = pageObj.dictLookupNF("Contents");
+  contents = pageObj.dictLookupNF("Contents").copy();
   if (!(contents.isRef() || contents.isArray() ||
 	contents.isNull())) {
     error(errSyntaxError, -1, "Page contents object (page {0:d}) is wrong type ({1:s})",
@@ -292,7 +292,7 @@
   }
 
   // thumb
-  thumb = pageObj.dictLookupNF("Thumb");
+  thumb = pageObj.dictLookupNF("Thumb").copy();
   if (!(thumb.isStream() || thumb.isNull() || thumb.isRef())) {
       error(errSyntaxError, -1, "Page thumb object (page {0:d}) is wrong type ({1:s})",
             num, thumb.getTypeName());
@@ -300,7 +300,7 @@
   }
 
   // actions
-  actions = pageObj.dictLookupNF("AA");
+  actions = pageObj.dictLookupNF("AA").copy();
   if (!(actions.isDict() || actions.isNull())) {
       error(errSyntaxError, -1, "Page additional action object (page {0:d}) is wrong type ({1:s})",
             num, actions.getTypeName());
@@ -340,15 +340,15 @@
   Object obj1;
   Dict *pageDict = pageObj.getDict()->copy(xrefA);
   xref = xrefA;
-  trans = pageDict->lookupNF("Trans");
-  annotsObj = pageDict->lookupNF("Annots");
-  contents = pageDict->lookupNF("Contents");
+  trans = pageDict->lookupNF("Trans").copy();
+  annotsObj = pageDict->lookupNF("Annots").copy();
+  contents = pageDict->lookupNF("Contents").copy();
   if (contents.isArray()) {
-    obj1 = pageDict->lookupNF("Contents");
+    obj1 = pageDict->lookupNF("Contents").copy();
     contents = obj1.getArray()->copy(xrefA);
   }
-  thumb = pageDict->lookupNF("Thumb");
-  actions = pageDict->lookupNF("AA");
+  thumb = pageDict->lookupNF("Thumb").copy();
+  actions = pageDict->lookupNF("AA").copy();
   obj1 = pageDict->lookup("Resources");
   if (obj1.isDict()) {
     attrs->replaceResource(std::move(obj1));
diff --git a/poppler/Stream.cc b/poppler/Stream.cc
index 6a6b46a..33537b0 100644
--- a/poppler/Stream.cc
+++ b/poppler/Stream.cc
@@ -14,7 +14,7 @@
 // under GPL version 2 or later
 //
 // Copyright (C) 2005 Jeff Muizelaar <jeff@infidigm.net>
-// Copyright (C) 2006-2010, 2012-2014, 2016, 2017, 2018 Albert Astals Cid <aacid@kde.org>
+// Copyright (C) 2006-2010, 2012-2014, 2016-2019 Albert Astals Cid <aacid@kde.org>
 // Copyright (C) 2007 Krzysztof Kowalczyk <kkowalczyk@gmail.com>
 // Copyright (C) 2008 Julien Rebetez <julien@fhtagn.net>
 // Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
@@ -322,7 +322,7 @@
     Object globals;
     if (params->isDict()) {
       XRef *xref = params->getDict()->getXRef();
-      obj = params->dictLookupNF("JBIG2Globals");
+      obj = params->dictLookupNF("JBIG2Globals").copy();
       globals = obj.fetch(xref, recursion);
     }
     str = new JBIG2Stream(str, std::move(globals), &obj);
diff --git a/poppler/StructElement.cc b/poppler/StructElement.cc
index 3cde8c9..4149116 100644
--- a/poppler/StructElement.cc
+++ b/poppler/StructElement.cc
@@ -1045,7 +1045,7 @@
   }
 
   // Parent object reference (required).
-  s->parentRef = element->lookupNF("P");
+  s->parentRef = element->lookupNF("P").copy();
   if (!s->parentRef.isRef()) {
     error(errSyntaxError, -1, "P object is wrong type ({0:s})", obj.getTypeName());
     return;
@@ -1085,7 +1085,7 @@
   // is to be rendered in. Note: each element stores only the /Pg value
   // contained by it, and StructElement::getPageRef() may look in parent
   // elements to find the page where an element belongs.
-  pageRef = element->lookupNF("Pg");
+  pageRef = element->lookupNF("Pg").copy();
 
   // Revision number (optional).
   obj = element->lookup("R");
@@ -1201,17 +1201,17 @@
 
     child = new StructElement(mcidObj.getInt(), treeRoot, this);
 
-    Object pageRefObj = childObj->dictLookupNF("Pg");
+    Object pageRefObj = childObj->dictLookupNF("Pg").copy();
     if (pageRefObj.isRef()) {
       child->pageRef = std::move(pageRefObj);
     }
   } else if (childObj->isDict("OBJR")) {
-    Object refObj = childObj->dictLookupNF("Obj");
+    const Object &refObj = childObj->dictLookupNF("Obj");
     if (refObj.isRef()) {
 
       child = new StructElement(refObj.getRef(), treeRoot, this);
 
-      Object pageRefObj = childObj->dictLookupNF("Pg");
+      Object pageRefObj = childObj->dictLookupNF("Pg").copy();
       if (pageRefObj.isRef()) {
         child->pageRef = std::move(pageRefObj);
       }
@@ -1259,7 +1259,7 @@
       parseChild(&ref, &obj, seen);
     }
   } else if (kids.isDict() || kids.isInt()) {
-    Object ref = element->lookupNF("K");
+    const Object &ref = element->lookupNF("K");
     parseChild(&ref, &kids, seen);
   }
 }
diff --git a/poppler/StructTreeRoot.cc b/poppler/StructTreeRoot.cc
index 99359b7..dc9eb56 100644
--- a/poppler/StructTreeRoot.cc
+++ b/poppler/StructTreeRoot.cc
@@ -95,7 +95,7 @@
     StructElement *child = new StructElement(kids.getDict(), this, nullptr, seenElements);
     if (child->isOk()) {
       appendChild(child);
-      Object ref = root->lookupNF("K");
+      const Object &ref = root->lookupNF("K");
       if (ref.isRef())
         parentTreeAdd(ref.getRef(), child);
     } else {
diff --git a/poppler/XRef.cc b/poppler/XRef.cc
index 4679da9..d9b7a33 100644
--- a/poppler/XRef.cc
+++ b/poppler/XRef.cc
@@ -291,7 +291,7 @@
     }
 
     // set size to (at least) the size specified in trailer dict
-    obj = trailerDict.dictLookupNF("Size");
+    obj = trailerDict.dictLookupNF("Size").copy();
     if (!obj.isInt()) {
         error(errSyntaxWarning, -1, "No valid XRef size in trailer");
     } else {
@@ -306,7 +306,7 @@
     }
 
     // get the root dictionary (catalog) object
-    obj = trailerDict.dictLookupNF("Root");
+    obj = trailerDict.dictLookupNF("Root").copy();
     if (obj.isRef()) {
       rootNum = obj.getRefNum();
       rootGen = obj.getRefGen();
@@ -588,7 +588,7 @@
   }
 
   // get the 'Prev' pointer
-  obj2 = obj.getDict()->lookupNF("Prev");
+  obj2 = obj.getDict()->lookupNF("Prev").copy();
   if (obj2.isInt() || obj2.isInt64()) {
     if (obj2.isInt())
       pos2 = obj2.getInt();
@@ -657,7 +657,7 @@
   ok = false;
 
   Dict *dict = xrefStr->getDict();
-  obj = dict->lookupNF("Size");
+  obj = dict->lookupNF("Size").copy();
   if (!obj.isInt()) {
     return false;
   }
@@ -672,7 +672,7 @@
     }
   }
 
-  obj = dict->lookupNF("W");
+  obj = dict->lookupNF("W").copy();
   if (!obj.isArray() || obj.arrayGetLength() < 3) {
     return false;
   }
@@ -691,7 +691,7 @@
   }
 
   xrefStr->reset();
-  Object idx = dict->lookupNF("Index");
+  const Object &idx = dict->lookupNF("Index");
   if (idx.isArray()) {
     for (int i = 0; i+1 < idx.arrayGetLength(); i += 2) {
       obj = idx.arrayGet(i);
@@ -715,7 +715,7 @@
     }
   }
 
-  obj = dict->lookupNF("Prev");
+  obj = dict->lookupNF("Prev").copy();
   if (obj.isInt() && obj.getInt() >= 0) {
     *pos = obj.getInt();
     more = true;
@@ -816,7 +816,6 @@
 //          Existing data in XRef::entries may get corrupted if applied anyway.
 bool XRef::constructXRef(bool *wasReconstructed, bool needCatalogDict) {
   Parser *parser;
-  Object obj;
   char buf[256];
   Goffset pos;
   int num, gen;
@@ -872,7 +871,7 @@
 		 false);
         Object newTrailerDict = parser->getObj();
         if (newTrailerDict.isDict()) {
-	  obj = newTrailerDict.dictLookupNF("Root");
+	  const Object &obj = newTrailerDict.dictLookupNF("Root");
 	  if (obj.isRef() && (!gotRoot || !needCatalogDict) && rootNum != obj.getRefNum()) {
 	    rootNum = obj.getRefNum();
 	    rootGen = obj.getRefGen();
@@ -1196,7 +1195,7 @@
 
 // Added for the pdftex project.
 Object XRef::getDocInfoNF() {
-  return trailerDict.dictLookupNF("Info");
+  return trailerDict.dictLookupNF("Info").copy();
 }
 
 Object XRef::createDocInfoIfNoneExists() {
@@ -1695,7 +1694,7 @@
 
 void XRef::markUnencrypted() {
   // Mark objects referred from the Encrypt dict as Unencrypted
-  Object obj = trailerDict.dictLookupNF("Encrypt");
+  const Object &obj = trailerDict.dictLookupNF("Encrypt");
   if (obj.isRef()) {
     XRefEntry *e = getEntry(obj.getRefNum());
     e->setFlag(XRefEntry::Unencrypted, true);
diff --git a/qt5/src/poppler-annotation-helper.h b/qt5/src/poppler-annotation-helper.h
index f7d2dec..d8193c6 100644
--- a/qt5/src/poppler-annotation-helper.h
+++ b/qt5/src/poppler-annotation-helper.h
@@ -1,5 +1,5 @@
 /* poppler-annotation-helper.h: qt interface to poppler
- * Copyright (C) 2006, 2008, 2017, 2018, Albert Astals Cid <aacid@kde.org>
+ * Copyright (C) 2006, 2008, 2017-2019, Albert Astals Cid <aacid@kde.org>
  * Copyright (C) 2008, Pino Toscano <pino@kde.org>
  * Copyright (C) 2012, Fabio D'Urso <fabiodurso@hotmail.it>
  * Copyright (C) 2018, Dileep Sankhla <sankhla.dileep96@gmail.com>
@@ -139,7 +139,7 @@
 
 void XPDFReader::lookupIntRef( Dict * dict, char * type, int & dest )
 {
-    Object refObj = dict->lookupNF( type );
+    const Object &refObj = dict->lookupNF( type );
     if ( refObj.isNull() )
         return;
     if ( refObj.isRef() )
diff --git a/utils/pdfunite.cc b/utils/pdfunite.cc
index 4505f87..b014211 100644
--- a/utils/pdfunite.cc
+++ b/utils/pdfunite.cc
@@ -204,12 +204,12 @@
     Object catObj = docs[0]->getXRef()->getCatalog();
     Dict *catDict = catObj.getDict();
     intents = catDict->lookup("OutputIntents");
-    afObj = catDict->lookupNF("AcroForm");
+    afObj = catDict->lookupNF("AcroForm").copy();
     Ref *refPage = docs[0]->getCatalog()->getPageRef(1);
     if (!afObj.isNull() && refPage) {
       docs[0]->markAcroForm(&afObj, yRef, countRef, 0, refPage->num, refPage->num);
     }
-    ocObj = catDict->lookupNF("OCProperties");
+    ocObj = catDict->lookupNF("OCProperties").copy();
     if (!ocObj.isNull() && ocObj.isDict() && refPage) {
       docs[0]->markPageObjects(ocObj.getDict(), yRef, countRef, 0, refPage->num, refPage->num);
     }
@@ -296,7 +296,7 @@
       pages.push_back(std::move(page));
       offsets.push_back(numOffset);
       docs[i]->markPageObjects(pageDict, yRef, countRef, numOffset, refPage->num, refPage->num);
-      Object annotsObj = pageDict->lookupNF("Annots");
+      Object annotsObj = pageDict->lookupNF("Annots").copy();
       if (!annotsObj.isNull()) {
         docs[i]->markAnnotations(&annotsObj, yRef, countRef, numOffset, refPage->num, refPage->num);
       }
@@ -313,7 +313,7 @@
     Object pageForm = pageCatDict->lookup("AcroForm");
     if (i > 0 && !pageForm.isNull() && pageForm.isDict()) {
       if (afObj.isNull()) {
-        afObj = pageCatDict->lookupNF("AcroForm");
+        afObj = pageCatDict->lookupNF("AcroForm").copy();
       } else if (afObj.isDict()) {
         doMergeFormDict(afObj.getDict(), pageForm.getDict(), numOffset);
       }