Make Annot::getNF and Dict::getNF return const & instead of copy

Lots of users can deal with a const & directly, so it saves us some
copying. For the ones that can't move the copy to the caller side.

Some of copy() on the caller side can be easily removed, that will come
on next commits
diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc
index ed37da4..38699e2 100644
--- a/glib/poppler-document.cc
+++ b/glib/poppler-document.cc
@@ -2845,7 +2845,7 @@
       for (j = 0; j < rb_array->getLength (); ++j) {
 	OptionalContentGroup *oc;
 
-        Object ref = rb_array->getNF (j);
+        const Object &ref = rb_array->getNF (j);
 	if (!ref.isRef ()) {
 	  continue;
 	}
@@ -2888,7 +2888,7 @@
     Object orderItem = order->get (i);
 
     if (orderItem.isDict ()) {
-      Object ref = order->getNF (i);
+      const Object &ref = order->getNF (i);
       if (ref.isRef ()) {
         OptionalContentGroup *oc = ocg->findOcgByRef (ref.getRef ());
 	Layer *layer = layer_new (oc);
diff --git a/poppler/Annot.cc b/poppler/Annot.cc
index 294aa7b..467836d 100644
--- a/poppler/Annot.cc
+++ b/poppler/Annot.cc
@@ -938,7 +938,7 @@
   } else if (stateObj->isDict()) { // Test each value
     const int size = stateObj->dictGetLength();
     for (int i = 0; i < size; ++i) {
-      Object obj1 = stateObj->dictGetValNF(i);
+      const Object &obj1 = stateObj->dictGetValNF(i);
       if (obj1.isRef()) {
         Ref r = obj1.getRef();
         if (r.num == refToStream.num && r.gen == refToStream.gen) {
@@ -1001,7 +1001,7 @@
   } else if (obj1->isDict()) {
     const int size = obj1->dictGetLength();
     for (int i = 0; i < size; ++i) {
-      Object obj2 = obj1->dictGetValNF(i);
+      const Object &obj2 = obj1->dictGetValNF(i);
       if (obj2.isRef()) {
         removeStream(obj2.getRef());
       }
@@ -6646,7 +6646,7 @@
       //form widget
       Object obj1 = annotsObj->arrayGet(i);
       if (obj1.isDict()) {
-	Object obj2 = annotsObj->arrayGetNF(i);
+	const Object &obj2 = annotsObj->arrayGetNF(i);
         annot = createAnnot (std::move(obj1), &obj2);
         if (annot) {
           if (annot->isOk()) {
diff --git a/poppler/Array.cc b/poppler/Array.cc
index ca4a03d..83098a2 100644
--- a/poppler/Array.cc
+++ b/poppler/Array.cc
@@ -16,7 +16,7 @@
 // Copyright (C) 2005 Kristian Høgsberg <krh@redhat.com>
 // Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
 // Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
-// Copyright (C) 2013, 2017 Albert Astals Cid <aacid@kde.org>
+// Copyright (C) 2013, 2017, 2019 Albert Astals Cid <aacid@kde.org>
 // Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
 // Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
 //
@@ -77,16 +77,17 @@
   return elems[i].fetch(xref, recursion);
 }
 
-Object Array::getNF(int i) const {
+const Object &Array::getNF(int i) const {
   if (i < 0 || std::size_t(i) >= elems.size()) {
-    return Object(objNull);
+    static Object nullObj(objNull);
+    return nullObj;
   }
-  return elems[i].copy();
+  return elems[i];
 }
 
 bool Array::getString(int i, GooString *string) const
 {
-  Object obj = getNF(i);
+  const Object &obj = getNF(i);
   if (obj.isString()) {
     string->clear();
     string->append(obj.getString());
diff --git a/poppler/Array.h b/poppler/Array.h
index dfe7c75..b64503a 100644
--- a/poppler/Array.h
+++ b/poppler/Array.h
@@ -16,7 +16,7 @@
 // Copyright (C) 2005 Kristian Høgsberg <krh@redhat.com>
 // Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
 // Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
-// Copyright (C) 2017, 2018 Albert Astals Cid <aacid@kde.org>
+// Copyright (C) 2017-2019 Albert Astals Cid <aacid@kde.org>
 // Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
 // Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
 //
@@ -68,7 +68,7 @@
 
   // Accessors.
   Object get(int i, int recursion = 0) const;
-  Object getNF(int i) const;
+  const Object &getNF(int i) const;
   bool getString(int i, GooString *string) const;
 
 private:
diff --git a/poppler/Catalog.cc b/poppler/Catalog.cc
index 894a8955..ed11145 100644
--- a/poppler/Catalog.cc
+++ b/poppler/Catalog.cc
@@ -14,7 +14,7 @@
 // under GPL version 2 or later
 //
 // Copyright (C) 2005 Kristian Høgsberg <krh@redhat.com>
-// Copyright (C) 2005-2013, 2015, 2017, 2018 Albert Astals Cid <aacid@kde.org>
+// Copyright (C) 2005-2013, 2015, 2017-2019 Albert Astals Cid <aacid@kde.org>
 // Copyright (C) 2005 Jeff Muizelaar <jrmuizel@nit.ca>
 // Copyright (C) 2005 Jonathan Blandford <jrb@redhat.com>
 // Copyright (C) 2005 Marco Pesenti Gritti <mpg@redhat.com>
@@ -270,7 +270,7 @@
        continue;
     }
 
-    Object kidRef = kids.arrayGetNF(kidsIdx);
+    const Object &kidRef = kids.arrayGetNF(kidsIdx);
     if (!kidRef.isRef()) {
       error(errSyntaxError, -1, "Kid object (page {0:uld}) is not an indirect reference ({1:s})",
 	    pages.size()+1, kidRef.getTypeName());
@@ -558,7 +558,7 @@
     else
       error(errSyntaxError, -1, "Invalid page tree");
   }
-  value = array->getNF(index + 1);
+  value = array->getNF(index + 1).copy();
 }
 
 NameTree::Entry::~Entry() {
@@ -615,7 +615,7 @@
   Object kids = tree->dictLookup("Kids");
   if (kids.isArray()) {
     for (int i = 0; i < kids.arrayGetLength(); ++i) {
-      Object kidRef = kids.arrayGetNF(i);
+      const Object &kidRef = kids.arrayGetNF(i);
       if (kidRef.isRef()) {
 	const int numObj = kidRef.getRef().num;
 	if (seen.find(numObj) != seen.end()) {
diff --git a/poppler/Dict.h b/poppler/Dict.h
index bdb08cd..0747d40 100644
--- a/poppler/Dict.h
+++ b/poppler/Dict.h
@@ -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) 2010, 2017, 2018 Albert Astals Cid <aacid@kde.org>
+// Copyright (C) 2010, 2017-2019 Albert Astals Cid <aacid@kde.org>
 // Copyright (C) 2010 Paweł Wiejacha <pawel.wiejacha@gmail.com>
 // Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
 // Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
@@ -82,7 +82,7 @@
   // Iterative accessors.
   const char *getKey(int i) const { return entries[i].first.c_str(); }
   Object getVal(int i) const { return entries[i].second.fetch(xref); }
-  Object getValNF(int i) const { return entries[i].second.copy(); }
+  const Object &getValNF(int i) const { return entries[i].second; }
 
   // Set the xref pointer.  This is only used in one special case: the
   // trailer dictionary, which is read before the xref table is
diff --git a/poppler/FontInfo.cc b/poppler/FontInfo.cc
index cefacad..9ce4ce3 100644
--- a/poppler/FontInfo.cc
+++ b/poppler/FontInfo.cc
@@ -131,7 +131,7 @@
     Object objDict = resDict->lookup(resTypes[resType]);
     if (objDict.isDict()) {
       for (int i = 0; i < objDict.dictGetLength(); ++i) {
-        obj1 = objDict.dictGetValNF(i);
+        obj1 = objDict.dictGetValNF(i).copy();
         if (obj1.isRef()) {
           // check for an already-seen object
           const Ref r = obj1.getRef();
diff --git a/poppler/Form.cc b/poppler/Form.cc
index e37212e..204bf4d 100644
--- a/poppler/Form.cc
+++ b/poppler/Form.cc
@@ -634,7 +634,7 @@
   if (obj1.isArray()) {
     // Load children
     for (int i = 0 ; i < obj1.arrayGetLength(); i++) {
-      Object childRef = obj1.arrayGetNF(i);
+      const Object &childRef = obj1.arrayGetNF(i);
       if (!childRef.isRef()) {
         error (errSyntaxError, -1, "Invalid form field renference");
         continue;
@@ -1817,7 +1817,7 @@
     Array *array = obj1.getArray();
     for(int i=0; i<array->getLength(); i++) {
       Object obj2 = array->get(i);
-      Object oref = array->getNF(i);
+      const Object &oref = array->getNF(i);
       if (!oref.isRef()) {
         error(errSyntaxWarning, -1, "Direct object in rootFields");
         continue;
@@ -1846,7 +1846,7 @@
     Array *array = obj1.getArray();
     calculateOrder.reserve(array->getLength());
     for(int i=0; i<array->getLength(); i++) {
-      Object oref = array->getNF(i);
+      const Object &oref = array->getNF(i);
       if (!oref.isRef()) {
         error(errSyntaxWarning, -1, "Direct object in CO");
         continue;
diff --git a/poppler/Function.cc b/poppler/Function.cc
index 2b88288..e0af1d1 100644
--- a/poppler/Function.cc
+++ b/poppler/Function.cc
@@ -690,7 +690,7 @@
   }
   for (i = 0; i < k; ++i) {
     std::set<int> usedParentsAux = *usedParents;
-    Object obj2 = obj1.arrayGetNF(i);
+    Object obj2 = obj1.arrayGetNF(i).copy();
     if (obj2.isRef()) {
       const Ref ref = obj2.getRef();
       if (usedParentsAux.find(ref.num) == usedParentsAux.end()) {
diff --git a/poppler/Gfx.cc b/poppler/Gfx.cc
index d9460f2..c80786e 100644
--- a/poppler/Gfx.cc
+++ b/poppler/Gfx.cc
@@ -1241,7 +1241,7 @@
   if (obj2.isArray()) {
     GfxFont *font;
     if (obj2.arrayGetLength() == 2) {
-      Object fargs0 = obj2.arrayGetNF(0);
+      const Object &fargs0 = obj2.arrayGetNF(0);
       Object fargs1 = obj2.arrayGet(1);
       if (fargs0.isRef() && fargs1.isNum()) {
 	Object fobj = fargs0.fetch(xref);
@@ -1277,7 +1277,7 @@
     if (obj2.arrayGetLength() == 2) {
       Object dargs[2];
 
-      dargs[0] = obj2.arrayGetNF(0);
+      dargs[0] = obj2.arrayGetNF(0).copy();
       dargs[1] = obj2.arrayGet(1);
       if (dargs[0].isArray() && dargs[1].isInt()) {
 	opSetDash(dargs,2);
diff --git a/poppler/GfxFont.cc b/poppler/GfxFont.cc
index 1560499..014e5ca 100644
--- a/poppler/GfxFont.cc
+++ b/poppler/GfxFont.cc
@@ -2388,7 +2388,7 @@
   numFonts = fontDict->getLength();
   fonts = (GfxFont **)gmallocn(numFonts, sizeof(GfxFont *));
   for (i = 0; i < numFonts; ++i) {
-    Object obj1 = fontDict->getValNF(i);
+    const Object &obj1 = fontDict->getValNF(i);
     Object obj2 = obj1.fetch(xref);
     if (obj2.isDict()) {
       if (obj1.isRef()) {
@@ -2519,7 +2519,7 @@
     n = obj->arrayGetLength();
     h->hash((char *)&n, sizeof(int));
     for (i = 0; i < n; ++i) {
-      obj2 = obj->arrayGetNF(i);
+      obj2 = obj->arrayGetNF(i).copy();
       hashFontObject1(&obj2, h);
     }
     break;
@@ -2530,7 +2530,7 @@
     for (i = 0; i < n; ++i) {
       p = obj->dictGetKey(i);
       h->hash(p, (int)strlen(p));
-      obj2 = obj->dictGetValNF(i);
+      obj2 = obj->dictGetValNF(i).copy();
       hashFontObject1(&obj2, h);
     }
     break;
diff --git a/poppler/GfxState.cc b/poppler/GfxState.cc
index 84f28c4..f0ea025 100644
--- a/poppler/GfxState.cc
+++ b/poppler/GfxState.cc
@@ -16,7 +16,7 @@
 // Copyright (C) 2005 Kristian Høgsberg <krh@redhat.com>
 // Copyright (C) 2006, 2007 Jeff Muizelaar <jeff@infidigm.net>
 // Copyright (C) 2006, 2010 Carlos Garcia Campos <carlosgc@gnome.org>
-// Copyright (C) 2006-2018 Albert Astals Cid <aacid@kde.org>
+// Copyright (C) 2006-2019 Albert Astals Cid <aacid@kde.org>
 // Copyright (C) 2009, 2012 Koji Otani <sho@bbr.jp>
 // Copyright (C) 2009, 2011-2016 Thomas Freitag <Thomas.Freitag@alfa.de>
 // Copyright (C) 2009, 2019 Christian Persch <chpe@gnome.org>
@@ -1828,7 +1828,7 @@
     error(errSyntaxError, -1, "Bad ICCBased color space");
     return nullptr;
   }
-  obj1 = arr->getNF(1);
+  obj1 = arr->getNF(1).copy();
   if (obj1.isRef()) {
     iccProfileStreamA = obj1.getRef();
   } else {
diff --git a/poppler/Link.cc b/poppler/Link.cc
index 33bcb82..525c442 100644
--- a/poppler/Link.cc
+++ b/poppler/Link.cc
@@ -16,7 +16,7 @@
 // Copyright (C) 2006, 2008 Pino Toscano <pino@kde.org>
 // Copyright (C) 2007, 2010, 2011 Carlos Garcia Campos <carlosgc@gnome.org>
 // Copyright (C) 2008 Hugo Mercier <hmercier31@gmail.com>
-// Copyright (C) 2008-2010, 2012-2014, 2016-2018 Albert Astals Cid <aacid@kde.org>
+// Copyright (C) 2008-2010, 2012-2014, 2016-2019 Albert Astals Cid <aacid@kde.org>
 // Copyright (C) 2009 Kovid Goyal <kovid@kovidgoyal.net>
 // Copyright (C) 2009 Ilya Gorenbein <igorenbein@finjan.com>
 // Copyright (C) 2012 Tobias Koening <tobias.koenig@kdab.com>
@@ -188,7 +188,7 @@
       }
 
       // Similar circle check as above.
-      const Object obj3Ref = a->getNF(i);
+      const Object &obj3Ref = a->getNF(i);
       if (obj3Ref.isRef()) {
           const Ref ref = obj3Ref.getRef();
           if (!seenNextActions->insert(ref.num).second) {
@@ -230,7 +230,7 @@
     error(errSyntaxWarning, -1, "Annotation destination array is too short");
     return;
   }
-  Object obj1 = a->getNF(0);
+  Object obj1 = a->getNF(0).copy();
   if (obj1.isInt()) {
     pageNum = obj1.getInt() + 1;
     pageIsRef = false;
@@ -829,7 +829,7 @@
     StateList *stList = nullptr;
 
     for (int i = 0; i < obj1.arrayGetLength(); ++i) {
-      Object obj2 = obj1.arrayGetNF(i);
+      const Object &obj2 = obj1.arrayGetNF(i);
       if (obj2.isName()) {
         if (stList)
 	  stateList->push_back(stList);
diff --git a/poppler/Object.cc b/poppler/Object.cc
index fca0e42..be53947 100644
--- a/poppler/Object.cc
+++ b/poppler/Object.cc
@@ -156,7 +156,7 @@
     for (i = 0; i < arrayGetLength(); ++i) {
       if (i > 0)
 	fprintf(f, " ");
-      obj = arrayGetNF(i);
+      obj = arrayGetNF(i).copy();
       obj.print(f);
     }
     fprintf(f, "]");
@@ -165,7 +165,7 @@
     fprintf(f, "<<");
     for (i = 0; i < dictGetLength(); ++i) {
       fprintf(f, " /%s ", dictGetKey(i));
-      obj = dictGetValNF(i);
+      obj = dictGetValNF(i).copy();
       obj.print(f);
     }
     fprintf(f, " >>");
diff --git a/poppler/Object.h b/poppler/Object.h
index edc96f5..4f07efa 100644
--- a/poppler/Object.h
+++ b/poppler/Object.h
@@ -270,7 +270,7 @@
   void arrayAdd(Object &&elem);
   void arrayRemove(int i);
   Object arrayGet(int i, int recursion) const;
-  Object arrayGetNF(int i) const;
+  const Object &arrayGetNF(int i) const;
 
   // Dict accessors.
   int dictGetLength() const;
@@ -283,7 +283,7 @@
   Object dictLookupNF(const char *key) const;
   const char *dictGetKey(int i) const;
   Object dictGetVal(int i) const;
-  Object dictGetValNF(int i) const;
+  const Object &dictGetValNF(int i) const;
 
   // Stream accessors.
   bool streamIs(const char *dictType) const;
@@ -338,7 +338,7 @@
 inline Object Object::arrayGet(int i, int recursion = 0) const
   { OBJECT_TYPE_CHECK(objArray); return array->get(i, recursion); }
 
-inline Object Object::arrayGetNF(int i) const
+inline const Object &Object::arrayGetNF(int i) const
   { OBJECT_TYPE_CHECK(objArray); return array->getNF(i); }
 
 //------------------------------------------------------------------------
@@ -377,7 +377,7 @@
 inline Object Object::dictGetVal(int i) const
   { OBJECT_TYPE_CHECK(objDict); return dict->getVal(i); }
 
-inline Object Object::dictGetValNF(int i) const
+inline const Object &Object::dictGetValNF(int i) const
   { OBJECT_TYPE_CHECK(objDict); return dict->getValNF(i); }
 
 //------------------------------------------------------------------------
diff --git a/poppler/OptionalContent.cc b/poppler/OptionalContent.cc
index 9f5bef8..75b93f4 100644
--- a/poppler/OptionalContent.cc
+++ b/poppler/OptionalContent.cc
@@ -5,7 +5,7 @@
 // Copyright 2007 Brad Hards <bradh@kde.org>
 // Copyright 2008 Pino Toscano <pino@kde.org>
 // Copyright 2008, 2010 Carlos Garcia Campos <carlosgc@gnome.org>
-// Copyright 2008, 2010, 2011, 2017, 2018 Albert Astals Cid <aacid@kde.org>
+// Copyright 2008, 2010, 2011, 2017-2019 Albert Astals Cid <aacid@kde.org>
 // Copyright 2008 Mark Kaplan <mkaplan@finjan.com>
 // Copyright 2018 Adam Reichold <adam.reichold@t-online.de>
 //
@@ -51,7 +51,7 @@
       break;
     }
     auto thisOptionalContentGroup = std::make_unique<OptionalContentGroup>(ocg.getDict());
-    ocg = ocgList.arrayGetNF(i);
+    ocg = ocgList.arrayGetNF(i).copy();
     if (!ocg.isRef()) {
       break;
     }
@@ -79,7 +79,7 @@
   if (on.isArray()) {
     // ON is an optional element
     for (int i = 0; i < on.arrayGetLength(); ++i) {
-      Object reference = on.arrayGetNF(i);
+      const Object &reference = on.arrayGetNF(i);
       if (!reference.isRef()) {
 	// there can be null entries
 	break;
@@ -97,7 +97,7 @@
   if (off.isArray()) {
     // OFF is an optional element
     for (int i = 0; i < off.arrayGetLength(); ++i) {
-      Object reference = off.arrayGetNF(i);
+      const Object &reference = off.arrayGetNF(i);
       if (!reference.isRef()) {
 	// there can be null entries
 	break;
@@ -217,7 +217,7 @@
   Object op = expr2.arrayGet(0);
   if (op.isName("Not")) {
     if (expr2.arrayGetLength() == 2) {
-      Object obj = expr2.arrayGetNF(1);
+      Object obj = expr2.arrayGetNF(1).copy();
       ret = !evalOCVisibilityExpr(&obj, recursion + 1);
     } else {
       error(errSyntaxError, -1,
@@ -227,13 +227,13 @@
   } else if (op.isName("And")) {
     ret = true;
     for (int i = 1; i < expr2.arrayGetLength() && ret; ++i) {
-      Object obj = expr2.arrayGetNF(i);
+      Object obj = expr2.arrayGetNF(i).copy();
       ret = evalOCVisibilityExpr(&obj, recursion + 1);
     }
   } else if (op.isName("Or")) {
     ret = false;
     for (int i = 1; i < expr2.arrayGetLength() && !ret; ++i) {
-      Object obj = expr2.arrayGetNF(i);
+      Object obj = expr2.arrayGetNF(i).copy();
       ret = evalOCVisibilityExpr(&obj, recursion + 1);
     }
   } else {
@@ -247,7 +247,7 @@
 bool OCGs::allOn( Array *ocgArray )
 {
   for (int i = 0; i < ocgArray->getLength(); ++i) {
-    Object ocgItem = ocgArray->getNF(i);
+    const Object &ocgItem = ocgArray->getNF(i);
     if (ocgItem.isRef()) {
       OptionalContentGroup* oc = findOcgByRef( ocgItem.getRef() );      
       if ( oc && oc->getState() == OptionalContentGroup::Off ) {
@@ -261,7 +261,7 @@
 bool OCGs::allOff( Array *ocgArray )
 {
   for (int i = 0; i < ocgArray->getLength(); ++i) {
-    Object ocgItem = ocgArray->getNF(i);
+    const Object &ocgItem = ocgArray->getNF(i);
     if (ocgItem.isRef()) {
       OptionalContentGroup* oc = findOcgByRef( ocgItem.getRef() );      
       if ( oc && oc->getState() == OptionalContentGroup::On ) {
@@ -275,7 +275,7 @@
 bool OCGs::anyOn( Array *ocgArray )
 {
   for (int i = 0; i < ocgArray->getLength(); ++i) {
-    Object ocgItem = ocgArray->getNF(i);
+    const Object &ocgItem = ocgArray->getNF(i);
     if (ocgItem.isRef()) {
       OptionalContentGroup* oc = findOcgByRef( ocgItem.getRef() );      
       if ( oc && oc->getState() == OptionalContentGroup::On ) {
@@ -289,7 +289,7 @@
 bool OCGs::anyOff( Array *ocgArray )
 {
   for (int i = 0; i < ocgArray->getLength(); ++i) {
-    Object ocgItem = ocgArray->getNF(i);
+    const Object &ocgItem = ocgArray->getNF(i);
     if (ocgItem.isRef()) {
       OptionalContentGroup* oc = findOcgByRef( ocgItem.getRef() );      
       if ( oc && oc->getState() == OptionalContentGroup::Off ) {
@@ -399,7 +399,7 @@
     node = new OCDisplayNode();
   }
   for (; i < obj2.arrayGetLength(); ++i) {
-    Object obj3 = obj2.arrayGetNF(i);
+    Object obj3 = obj2.arrayGetNF(i).copy();
     if ((child = OCDisplayNode::parse(&obj3, oc, xref, recursion + 1))) {
       if (!child->ocg && !child->name && node->getNumChildren() > 0) {
 	node->getChild(node->getNumChildren() - 1)->addChildren(child->takeChildren());
diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc
index 5cbeeb5..6526711 100644
--- a/poppler/PDFDoc.cc
+++ b/poppler/PDFDoc.cc
@@ -975,7 +975,7 @@
       strcmp(key, "Pages") != 0) 
     {
       if (j > 0) outStr->printf(" ");
-      Object value = catDict->getValNF(j);
+      Object value = catDict->getValNF(j).copy();
       outStr->printf("/%s ", key);
       writeObject(&value, outStr, getXRef(), 0, nullptr, cryptRC4, 0, 0, 0);
     }
@@ -998,7 +998,7 @@
   for (int n = 0; n < pageDict->getLength(); n++) {
     if (n > 0) outStr->printf(" ");
     const char *key = pageDict->getKey(n);
-    Object value = pageDict->getValNF(n);
+    Object value = pageDict->getValNF(n).copy();
     if (strcmp(key, "Parent") == 0) {
       outStr->printf("/Parent %d 0 R", rootNum + 1);
     } else {
@@ -1253,7 +1253,7 @@
     GooString *keyNameToPrint = keyName.sanitizedName(false /* non ps mode */);
     outStr->printf("/%s ", keyNameToPrint->c_str());
     delete keyNameToPrint;
-    Object obj1 = dict->getValNF(i);
+    Object obj1 = dict->getValNF(i).copy();
     writeObject(&obj1, outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, objNum, objGen, alreadyWrittenDicts);
   }
   outStr->printf(">> ");
@@ -1403,7 +1403,7 @@
       array = obj->getArray();
       outStr->printf("[");
       for (int i=0; i<array->getLength(); i++) {
-	Object obj1 = array->getNF(i);
+	Object obj1 = array->getNF(i).copy();
         writeObject(&obj1, outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, objNum, objGen);
       }
       outStr->printf("] ");
@@ -1673,10 +1673,10 @@
   for (int i=0; i<dict->getLength(); i++) {
     const char *key = dict->getKey(i);
     if (strcmp(key, "Annots") != 0) {
-      Object obj1 = dict->getValNF(i);
+      Object obj1 = dict->getValNF(i).copy();
       markObject(&obj1, xRef, countRef, numOffset, oldRefNum, newRefNum, alreadyMarkedDicts);
     } else {
-      Object annotsObj = dict->getValNF(i);
+      Object annotsObj = dict->getValNF(i).copy();
       if (!annotsObj.isNull()) {
         markAnnotations(&annotsObj, xRef, countRef, 0, oldRefNum, newRefNum, alreadyMarkedDicts);
       }
@@ -1696,7 +1696,7 @@
     case objArray:
       array = obj->getArray();
       for (int i=0; i<array->getLength(); i++) {
-        Object obj1 = array->getNF(i);
+        Object obj1 = array->getNF(i).copy();
         markObject(&obj1, xRef, countRef, numOffset, oldRefNum, newRefNum);
       }
       break;
@@ -1784,7 +1784,7 @@
 
   for (int n = 0; n < pageDict->getLength(); n++) {
     const char *key = pageDict->getKey(n);
-    Object value  = pageDict->getValNF(n);
+    Object value  = pageDict->getValNF(n).copy();
     if (strcmp(key, "Parent") != 0 &&
 	      strcmp(key, "Pages") != 0 &&
 	      strcmp(key, "AcroForm") != 0 &&
@@ -1810,7 +1810,7 @@
             Object obj2 = dict->lookupNF("P");
             if (obj2.isRef()) {
               if (obj2.getRef().num == oldPageNum) {
-                Object obj3 = array->getNF(i);
+                const Object &obj3 = array->getNF(i);
                 if (obj3.isRef()) {
                   Ref r;
                   r.num = newPageNum;
@@ -1837,7 +1837,7 @@
           }
           markPageObjects(dict, xRef, countRef, numOffset, oldPageNum, newPageNum, alreadyMarkedDicts);
         }
-        obj1 = array->getNF(i);
+        obj1 = array->getNF(i).copy();
         if (obj1.isRef()) {
           if (obj1.getRef().num + (int) numOffset >= xRef->getNumObjects() || xRef->getEntry(obj1.getRef().num + numOffset)->type == xrefEntryFree) {
             if (getXRef()->getEntry(obj1.getRef().num)->type == xrefEntryFree) {
@@ -1889,10 +1889,10 @@
       Dict *dict = acroform.getDict();
       for (int i=0; i < dict->getLength(); i++) {
         if (strcmp(dict->getKey(i), "Fields") == 0) {
-          Object fields = dict->getValNF(i);
+          Object fields = dict->getValNF(i).copy();
           modified = markAnnotations(&fields, xRef, countRef, numOffset, oldRefNum, newRefNum);
         } else {
-          Object obj = dict->getValNF(i);
+          Object obj = dict->getValNF(i).copy();
           markObject(&obj, xRef, countRef, numOffset, oldRefNum, newRefNum);
         }
       }
diff --git a/poppler/PSOutputDev.cc b/poppler/PSOutputDev.cc
index 5f1db1f..32f11fc 100644
--- a/poppler/PSOutputDev.cc
+++ b/poppler/PSOutputDev.cc
@@ -1791,7 +1791,7 @@
 
       // avoid infinite recursion on XObjects
       skip = false;
-      Object xObjRef = xObjDict.dictGetValNF(i);
+      const Object &xObjRef = xObjDict.dictGetValNF(i);
       if (xObjRef.isRef()) {
 	Ref ref0 = xObjRef.getRef();
 	if (resourceIDs.find(ref0.num) != resourceIDs.end()) {
@@ -1822,7 +1822,7 @@
 
       // avoid infinite recursion on Patterns
       skip = false;
-      Object patRef = patDict.dictGetValNF(i);
+      const Object &patRef = patDict.dictGetValNF(i);
       if (patRef.isRef()) {
 	Ref ref0 = patRef.getRef();
 	if (resourceIDs.find(ref0.num) != resourceIDs.end()) {
@@ -2893,7 +2893,7 @@
   Object xObjDict = resDict->lookup("XObject");
   if (xObjDict.isDict()) {
     for (int i = 0; i < xObjDict.dictGetLength(); ++i) {
-      Object xObjRef = xObjDict.dictGetValNF(i);
+      const Object &xObjRef = xObjDict.dictGetValNF(i);
       Object xObj = xObjDict.dictGetVal(i);
       if (xObj.isStream()) {
 	Object subtypeObj = xObj.streamGetDict()->lookup("Subtype");
@@ -3110,7 +3110,7 @@
   Object xObjDict = resDict->lookup("XObject");
   if (xObjDict.isDict()) {
     for (int i = 0; i < xObjDict.dictGetLength(); ++i) {
-      Object xObjRef = xObjDict.dictGetValNF(i);
+      const Object &xObjRef = xObjDict.dictGetValNF(i);
       Object xObj = xObjDict.dictGetVal(i);
       if (xObj.isStream()) {
 	Object subtypeObj = xObj.streamGetDict()->lookup("Subtype");
diff --git a/poppler/Page.cc b/poppler/Page.cc
index 775c1a5..33c624f 100644
--- a/poppler/Page.cc
+++ b/poppler/Page.cc
@@ -423,7 +423,7 @@
     int idx = -1;
     // Get annotation position
     for (int i = 0; idx == -1 && i < annArray.arrayGetLength(); ++i) {
-      Object tmp = annArray.arrayGetNF(i);
+      const Object &tmp = annArray.arrayGetNF(i);
       if (tmp.isRef()) {
         Ref currAnnot = tmp.getRef();
         if (currAnnot.num == annotRef.num && currAnnot.gen == annotRef.gen) {
diff --git a/poppler/StructElement.cc b/poppler/StructElement.cc
index d65abe7..250a1d3 100644
--- a/poppler/StructElement.cc
+++ b/poppler/StructElement.cc
@@ -6,7 +6,7 @@
 //
 // Copyright 2013, 2014 Igalia S.L.
 // Copyright 2014 Luigi Scarso <luigi.scarso@gmail.com>
-// Copyright 2014, 2017, 2018 Albert Astals Cid <aacid@kde.org>
+// Copyright 2014, 2017-2019 Albert Astals Cid <aacid@kde.org>
 // Copyright 2015 Dmytro Morgun <lztoad@gmail.com>
 // Copyright 2018 Adrian Johnson <ajohnson@redneon.com>
 // Copyright 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich
@@ -1255,7 +1255,7 @@
   if (kids.isArray()) {
     for (int i = 0; i < kids.arrayGetLength(); i++) {
       Object obj = kids.arrayGet(i);
-      Object ref = kids.arrayGetNF(i);
+      Object ref = kids.arrayGetNF(i).copy();
       parseChild(&ref, &obj, seen);
     }
   } else if (kids.isDict() || kids.isInt()) {
diff --git a/poppler/StructTreeRoot.cc b/poppler/StructTreeRoot.cc
index d23ad4d..099bf62 100644
--- a/poppler/StructTreeRoot.cc
+++ b/poppler/StructTreeRoot.cc
@@ -7,7 +7,7 @@
 // Copyright 2013, 2014 Igalia S.L.
 // Copyright 2014 Fabio D'Urso <fabiodurso@hotmail.it>
 // Copyright 2017 Jan-Erik S <janerik234678@gmail.com>
-// Copyright 2017, 2018 Albert Astals Cid <aacid@kde.org>
+// Copyright 2017-2019 Albert Astals Cid <aacid@kde.org>
 // Copyright 2017, 2018 Adrian Johnson <ajohnson@redneon.com>
 // Copyright 2018, Adam Reichold <adam.reichold@t-online.de>
 //
@@ -65,7 +65,7 @@
       error(errSyntaxWarning, -1, "K in StructTreeRoot has more than one children in a tagged PDF");
     }
     for (int i = 0; i < kids.arrayGetLength(); i++) {
-      Object ref = kids.arrayGetNF(i);
+      const Object &ref = kids.arrayGetNF(i);
       if (ref.isRef()) {
         seenElements.insert(ref.getRefNum());
       }
@@ -145,7 +145,7 @@
 	if (value.isArray()) {
 	  vec.resize(value.arrayGetLength());
 	  for (int j = 0; j < value.arrayGetLength(); j++) {
-	    Object itemvalue = value.arrayGetNF(j);
+	    const Object &itemvalue = value.arrayGetNF(j);
 	    if (itemvalue.isRef()) {
 	      Ref ref = itemvalue.getRef();
 	      vec[j].ref = ref;
@@ -155,7 +155,7 @@
 	    }
 	  }
 	} else {
-	  value = nums.arrayGetNF(i + 1);
+	  value = nums.arrayGetNF(i + 1).copy();
 	  if (value.isRef()) {
 	    Ref ref = value.getRef();
 	    vec.resize(1);
diff --git a/poppler/XRef.cc b/poppler/XRef.cc
index 44e7cca..4679da9 100644
--- a/poppler/XRef.cc
+++ b/poppler/XRef.cc
@@ -1617,7 +1617,7 @@
     {
       Array *array = obj->getArray();
       for (int i = 0; i < array->getLength(); i++) {
-	obj1 = array->getNF(i);
+	obj1 = array->getNF(i).copy();
         markUnencrypted(&obj1);
       }
       break;
@@ -1633,7 +1633,7 @@
         dict = obj->getDict();
       }
       for (int i = 0; i < dict->getLength(); i++) {
-	obj1 = dict->getValNF(i);
+	obj1 = dict->getValNF(i).copy();
         markUnencrypted(&obj1);
       }
       break;
diff --git a/qt5/src/poppler-optcontent.cc b/qt5/src/poppler-optcontent.cc
index 9d8bf7c..ac1c61f 100644
--- a/qt5/src/poppler-optcontent.cc
+++ b/qt5/src/poppler-optcontent.cc
@@ -3,7 +3,7 @@
  * Copyright (C) 2007, Brad Hards <bradh@kde.org>
  * Copyright (C) 2008, 2014, Pino Toscano <pino@kde.org>
  * Copyright (C) 2008, Carlos Garcia Campos <carlosgc@gnome.org>
- * Copyright (C) 2015-2018, Albert Astals Cid <aacid@kde.org>
+ * Copyright (C) 2015-2019, Albert Astals Cid <aacid@kde.org>
  * Copyright (C) 2017, Hubert Figuière <hub@figuiere.net>
  * 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>
@@ -43,7 +43,7 @@
   {
     itemsInGroup.reserve(rbarray->getLength());
     for (int i = 0; i < rbarray->getLength(); ++i) {
-      Object ref = rbarray->getNF( i );
+      const Object &ref = rbarray->getNF( i );
       if ( ! ref.isRef() ) {
 	qDebug() << "expected ref, but got:" << ref.getType();
       }
@@ -201,7 +201,7 @@
     for (int i = 0; i < orderArray->getLength(); ++i) {
       Object orderItem = orderArray->get(i);
       if ( orderItem.isDict() ) {
-	Object item = orderArray->getNF(i);
+	const Object &item = orderArray->getNF(i);
 	if (item.isRef() ) {
           OptContentItem *ocItem = m_optContentItems.value(QString::number(item.getRefNum()));
 	  if (ocItem) {
diff --git a/test/pdf-fullrewrite.cc b/test/pdf-fullrewrite.cc
index 94f39b3..d9b8ac3 100644
--- a/test/pdf-fullrewrite.cc
+++ b/test/pdf-fullrewrite.cc
@@ -125,8 +125,8 @@
    * in dictB is also contained in dictA */
   for (int i = 0; i < length; ++i) {
     const char *key = dictA->getKey(i);
-    Object valA = dictA->getValNF(i);
-    Object valB = dictB->lookupNF(key);
+    Object valA = dictA->getValNF(i).copy();
+    Object valB = dictB->lookupNF(key).copy();
     if (!compareObjects(&valA, &valB))
       return false;
   }
@@ -197,8 +197,8 @@
           return false;
         } else {
           for (int i = 0; i < length; ++i) {
-            Object elemA = arrayA->getNF(i);
-            Object elemB = arrayB->getNF(i);
+            Object elemA = arrayA->getNF(i).copy();
+            Object elemB = arrayB->getNF(i).copy();
             if (!compareObjects(&elemA, &elemB)) {
               return false;
             }
diff --git a/utils/pdfunite.cc b/utils/pdfunite.cc
index 73f416e..4505f87 100644
--- a/utils/pdfunite.cc
+++ b/utils/pdfunite.cc
@@ -47,12 +47,12 @@
     Array *newNameArray = new Array(srcXRef);
     int j = 0;
     for (int i = 0; i < srcNameArray.arrayGetLength() - 1; i += 2) {
-      Object key = srcNameArray.arrayGetNF(i);
-      Object value = srcNameArray.arrayGetNF(i + 1);
+      const Object &key = srcNameArray.arrayGetNF(i);
+      const Object &value = srcNameArray.arrayGetNF(i + 1);
       if (key.isString() && value.isRef()) {
         while (j < mergeNameArray.arrayGetLength() - 1) {
-          Object mkey = mergeNameArray.arrayGetNF(j);
-          Object mvalue = mergeNameArray.arrayGetNF(j + 1);
+          const Object &mkey = mergeNameArray.arrayGetNF(j);
+          const Object &mvalue = mergeNameArray.arrayGetNF(j + 1);
           if (mkey.isString() && mvalue.isRef()) {
             if (mkey.getString()->cmp(key.getString()) < 0) {
               newNameArray->add(Object(new GooString(mkey.getString()->c_str())));
@@ -72,8 +72,8 @@
       }
     }
     while (j < mergeNameArray.arrayGetLength() - 1) {
-      Object mkey = mergeNameArray.arrayGetNF(j);
-      Object mvalue = mergeNameArray.arrayGetNF(j + 1);
+      const Object &mkey = mergeNameArray.arrayGetNF(j);
+      const Object &mvalue = mergeNameArray.arrayGetNF(j + 1);
       if (mkey.isString() && mvalue.isRef()) {
         newNameArray->add(Object(new GooString(mkey.getString()->c_str())));
         newNameArray->add(Object( { mvalue.getRef().num + numOffset, mvalue.getRef().gen } ));
@@ -85,8 +85,8 @@
   } else if (srcNameArray.isNull() && mergeNameArray.isArray()) {
     Array *newNameArray = new Array(srcXRef);
     for (int i = 0; i < mergeNameArray.arrayGetLength() - 1; i += 2) {
-      Object key = mergeNameArray.arrayGetNF(i);
-      Object value = mergeNameArray.arrayGetNF(i + 1);
+      const Object &key = mergeNameArray.arrayGetNF(i);
+      const Object &value = mergeNameArray.arrayGetNF(i + 1);
       if (key.isString() && value.isRef()) {
         newNameArray->add(Object(new GooString(key.getString()->c_str())));
         newNameArray->add(Object( { value.getRef().num + numOffset, value.getRef().gen } ));
@@ -117,7 +117,7 @@
   Object mergeFields = mergeFormDict->lookup("Fields");
   if (srcFields.isArray() && mergeFields.isArray()) {
     for (int i = 0; i < mergeFields.arrayGetLength(); i++) {
-      Object value = mergeFields.arrayGetNF(i);
+      const Object &value = mergeFields.arrayGetNF(i);
       srcFields.arrayAdd(Object( { value.getRef().num + numOffset, value.getRef().gen } ));
     }
   }
@@ -372,7 +372,7 @@
       if (j > 0)
 	outStr->printf(" ");
       const char *key = pageDict->getKey(j);
-      Object value = pageDict->getValNF(j);
+      Object value = pageDict->getValNF(j).copy();
       if (strcmp(key, "Parent") == 0) {
         outStr->printf("/Parent %d 0 R", rootNum + 1);
       } else {