Make CPDF_Dictionary use unique pointers.

Some changes were required to match underlying ctors
as invoked by the templated methods.

Many release() calls go away, a few WrapUniques() are
introduced to avoid going deeper into other code.

Review-Url: https://codereview.chromium.org/2510223002
diff --git a/fpdfsdk/cpdfsdk_annothandlermgr.cpp b/fpdfsdk/cpdfsdk_annothandlermgr.cpp
index 51ce88e..898b9cc 100644
--- a/fpdfsdk/cpdfsdk_annothandlermgr.cpp
+++ b/fpdfsdk/cpdfsdk_annothandlermgr.cpp
@@ -6,6 +6,8 @@
 
 #include "fpdfsdk/cpdfsdk_annothandlermgr.h"
 
+#include "core/fpdfapi/parser/cpdf_number.h"
+#include "core/fpdfapi/parser/cpdf_string.h"
 #include "core/fpdfdoc/cpdf_annot.h"
 #include "fpdfsdk/cba_annotiterator.h"
 #include "fpdfsdk/cpdfsdk_annot.h"
@@ -62,8 +64,9 @@
   CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
 
   CPDFSDK_DateTime curTime;
-  pPDFAnnot->GetAnnotDict()->SetStringFor("M", curTime.ToPDFDateTimeString());
-  pPDFAnnot->GetAnnotDict()->SetNumberFor("F", 0);
+  pPDFAnnot->GetAnnotDict()->SetNewFor<CPDF_String>(
+      "M", curTime.ToPDFDateTimeString(), false);
+  pPDFAnnot->GetAnnotDict()->SetNewFor<CPDF_Number>("F", 0);
 }
 
 void CPDFSDK_AnnotHandlerMgr::Annot_OnLoad(CPDFSDK_Annot* pAnnot) {
diff --git a/fpdfsdk/cpdfsdk_baannot.cpp b/fpdfsdk/cpdfsdk_baannot.cpp
index ee4b55d..9fb1e88 100644
--- a/fpdfsdk/cpdfsdk_baannot.cpp
+++ b/fpdfsdk/cpdfsdk_baannot.cpp
@@ -6,10 +6,15 @@
 
 #include "fpdfsdk/cpdfsdk_baannot.h"
 
+#include <algorithm>
+
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
+#include "core/fpdfapi/parser/cpdf_name.h"
 #include "core/fpdfapi/parser/cpdf_number.h"
+#include "core/fpdfapi/parser/cpdf_reference.h"
 #include "core/fpdfapi/parser/cpdf_stream.h"
+#include "core/fpdfapi/parser/cpdf_string.h"
 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
 #include "fpdfsdk/cpdfsdk_datetime.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
@@ -89,11 +94,12 @@
 }
 
 void CPDFSDK_BAAnnot::SetContents(const CFX_WideString& sContents) {
-  if (sContents.IsEmpty())
+  if (sContents.IsEmpty()) {
     m_pAnnot->GetAnnotDict()->RemoveFor("Contents");
-  else
-    m_pAnnot->GetAnnotDict()->SetStringFor("Contents",
-                                           PDF_EncodeText(sContents));
+  } else {
+    m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_String>(
+        "Contents", PDF_EncodeText(sContents), false);
+  }
 }
 
 CFX_WideString CPDFSDK_BAAnnot::GetContents() const {
@@ -101,10 +107,12 @@
 }
 
 void CPDFSDK_BAAnnot::SetAnnotName(const CFX_WideString& sName) {
-  if (sName.IsEmpty())
+  if (sName.IsEmpty()) {
     m_pAnnot->GetAnnotDict()->RemoveFor("NM");
-  else
-    m_pAnnot->GetAnnotDict()->SetStringFor("NM", PDF_EncodeText(sName));
+  } else {
+    m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_String>(
+        "NM", PDF_EncodeText(sName), false);
+  }
 }
 
 CFX_WideString CPDFSDK_BAAnnot::GetAnnotName() const {
@@ -114,25 +122,23 @@
 void CPDFSDK_BAAnnot::SetModifiedDate(const FX_SYSTEMTIME& st) {
   CPDFSDK_DateTime dt(st);
   CFX_ByteString str = dt.ToPDFDateTimeString();
-
   if (str.IsEmpty())
     m_pAnnot->GetAnnotDict()->RemoveFor("M");
   else
-    m_pAnnot->GetAnnotDict()->SetStringFor("M", str);
+    m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_String>("M", str, false);
 }
 
 FX_SYSTEMTIME CPDFSDK_BAAnnot::GetModifiedDate() const {
   FX_SYSTEMTIME systime;
   CFX_ByteString str = m_pAnnot->GetAnnotDict()->GetStringFor("M");
-
   CPDFSDK_DateTime dt(str);
   dt.ToSystemTime(systime);
-
   return systime;
 }
 
 void CPDFSDK_BAAnnot::SetFlags(uint32_t nFlags) {
-  m_pAnnot->GetAnnotDict()->SetIntegerFor("F", nFlags);
+  m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Number>("F",
+                                                   static_cast<int>(nFlags));
 }
 
 uint32_t CPDFSDK_BAAnnot::GetFlags() const {
@@ -143,7 +149,7 @@
   if (str.IsEmpty())
     m_pAnnot->GetAnnotDict()->RemoveFor("AS");
   else
-    m_pAnnot->GetAnnotDict()->SetStringFor("AS", str);
+    m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_String>("AS", str, false);
 }
 
 CFX_ByteString CPDFSDK_BAAnnot::GetAppState() const {
@@ -151,7 +157,7 @@
 }
 
 void CPDFSDK_BAAnnot::SetStructParent(int key) {
-  m_pAnnot->GetAnnotDict()->SetIntegerFor("StructParent", key);
+  m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Number>("StructParent", key);
 }
 
 int CPDFSDK_BAAnnot::GetStructParent() const {
@@ -165,12 +171,10 @@
     pBorder->SetNewAt<CPDF_Number>(2, nWidth);
   } else {
     CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictFor("BS");
-    if (!pBSDict) {
-      pBSDict =
-          new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool());
-      m_pAnnot->GetAnnotDict()->SetFor("BS", pBSDict);
-    }
-    pBSDict->SetIntegerFor("W", nWidth);
+    if (!pBSDict)
+      pBSDict = m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Dictionary>("BS");
+
+    pBSDict->SetNewFor<CPDF_Number>("W", nWidth);
   }
 }
 
@@ -186,26 +190,24 @@
 
 void CPDFSDK_BAAnnot::SetBorderStyle(BorderStyle nStyle) {
   CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictFor("BS");
-  if (!pBSDict) {
-    pBSDict = new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool());
-    m_pAnnot->GetAnnotDict()->SetFor("BS", pBSDict);
-  }
+  if (!pBSDict)
+    pBSDict = m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Dictionary>("BS");
 
   switch (nStyle) {
     case BorderStyle::SOLID:
-      pBSDict->SetNameFor("S", "S");
+      pBSDict->SetNewFor<CPDF_Name>("S", "S");
       break;
     case BorderStyle::DASH:
-      pBSDict->SetNameFor("S", "D");
+      pBSDict->SetNewFor<CPDF_Name>("S", "D");
       break;
     case BorderStyle::BEVELED:
-      pBSDict->SetNameFor("S", "B");
+      pBSDict->SetNewFor<CPDF_Name>("S", "B");
       break;
     case BorderStyle::INSET:
-      pBSDict->SetNameFor("S", "I");
+      pBSDict->SetNewFor<CPDF_Name>("S", "I");
       break;
     case BorderStyle::UNDERLINE:
-      pBSDict->SetNameFor("S", "U");
+      pBSDict->SetNewFor<CPDF_Name>("S", "U");
       break;
     default:
       break;
@@ -241,14 +243,13 @@
 }
 
 void CPDFSDK_BAAnnot::SetColor(FX_COLORREF color) {
-  CPDF_Array* pArray = new CPDF_Array;
+  CPDF_Array* pArray = m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Array>("C");
   pArray->AddNew<CPDF_Number>(static_cast<FX_FLOAT>(FXSYS_GetRValue(color)) /
                               255.0f);
   pArray->AddNew<CPDF_Number>(static_cast<FX_FLOAT>(FXSYS_GetGValue(color)) /
                               255.0f);
   pArray->AddNew<CPDF_Number>(static_cast<FX_FLOAT>(FXSYS_GetBValue(color)) /
                               255.0f);
-  m_pAnnot->GetAnnotDict()->SetFor("C", pArray);
 }
 
 void CPDFSDK_BAAnnot::RemoveColor() {
@@ -297,10 +298,8 @@
                                       const CFX_ByteString& sContents,
                                       const CFX_ByteString& sAPState) {
   CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictFor("AP");
-  if (!pAPDict) {
-    pAPDict = new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool());
-    m_pAnnot->GetAnnotDict()->SetFor("AP", pAPDict);
-  }
+  if (!pAPDict)
+    pAPDict = m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Dictionary>("AP");
 
   CPDF_Stream* pStream = nullptr;
   CPDF_Dictionary* pParentDict = nullptr;
@@ -309,11 +308,9 @@
     pStream = pAPDict->GetStreamFor(sAPType);
   } else {
     CPDF_Dictionary* pAPTypeDict = pAPDict->GetDictFor(sAPType);
-    if (!pAPTypeDict) {
-      pAPTypeDict =
-          new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool());
-      pAPDict->SetFor(sAPType, pAPTypeDict);
-    }
+    if (!pAPTypeDict)
+      pAPTypeDict = pAPDict->SetNewFor<CPDF_Dictionary>(sAPType);
+
     pParentDict = pAPTypeDict;
     pStream = pAPTypeDict->GetStreamFor(sAPState);
   }
@@ -321,16 +318,16 @@
   if (!pStream) {
     CPDF_Document* pDoc = m_pPageView->GetPDFDocument();
     pStream = pDoc->NewIndirect<CPDF_Stream>();
-    pParentDict->SetReferenceFor(sAPType, pDoc, pStream);
+    pParentDict->SetNewFor<CPDF_Reference>(sAPType, pDoc, pStream->GetObjNum());
   }
 
   CPDF_Dictionary* pStreamDict = pStream->GetDict();
   if (!pStreamDict) {
     pStreamDict =
         new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool());
-    pStreamDict->SetNameFor("Type", "XObject");
-    pStreamDict->SetNameFor("Subtype", "Form");
-    pStreamDict->SetIntegerFor("FormType", 1);
+    pStreamDict->SetNewFor<CPDF_Name>("Type", "XObject");
+    pStreamDict->SetNewFor<CPDF_Name>("Subtype", "Form");
+    pStreamDict->SetNewFor<CPDF_Number>("FormType", 1);
     pStream->InitStream(nullptr, 0, pStreamDict);
   }
 
@@ -358,7 +355,8 @@
     CPDF_Document* pDoc = m_pPageView->GetPDFDocument();
     if (pDict->IsInline())
       pDict = pDoc->AddIndirectObject(pDict->Clone())->AsDictionary();
-    m_pAnnot->GetAnnotDict()->SetReferenceFor("A", pDoc, pDict);
+    m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Reference>("A", pDoc,
+                                                        pDict->GetObjNum());
   }
 }
 
@@ -372,7 +370,7 @@
 
 void CPDFSDK_BAAnnot::SetAAction(const CPDF_AAction& aa) {
   if (aa.GetDict() != m_pAnnot->GetAnnotDict()->GetDictFor("AA"))
-    m_pAnnot->GetAnnotDict()->SetFor("AA", aa.GetDict());
+    m_pAnnot->GetAnnotDict()->SetFor("AA", pdfium::WrapUnique(aa.GetDict()));
 }
 
 void CPDFSDK_BAAnnot::RemoveAAction() {
@@ -381,7 +379,6 @@
 
 CPDF_Action CPDFSDK_BAAnnot::GetAAction(CPDF_AAction::AActionType eAAT) {
   CPDF_AAction AAction = GetAAction();
-
   if (AAction.ActionExist(eAAT))
     return AAction.GetAction(eAAT);
 
diff --git a/fpdfsdk/cpdfsdk_widget.cpp b/fpdfsdk/cpdfsdk_widget.cpp
index b1c3379..1086906 100644
--- a/fpdfsdk/cpdfsdk_widget.cpp
+++ b/fpdfsdk/cpdfsdk_widget.cpp
@@ -8,9 +8,12 @@
 
 #include <memory>
 
+#include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
+#include "core/fpdfapi/parser/cpdf_reference.h"
 #include "core/fpdfapi/parser/cpdf_stream.h"
+#include "core/fpdfapi/parser/cpdf_string.h"
 #include "core/fpdfdoc/cpdf_defaultappearance.h"
 #include "core/fpdfdoc/cpdf_formcontrol.h"
 #include "core/fpdfdoc/cpdf_formfield.h"
@@ -966,21 +969,21 @@
   if (pNormalIcon) {
     if (CPDF_Dictionary* pImageDict = pNormalIcon->GetDict()) {
       if (pImageDict->GetStringFor("Name").IsEmpty())
-        pImageDict->SetStringFor("Name", "ImgA");
+        pImageDict->SetNewFor<CPDF_String>("Name", "ImgA", false);
     }
   }
 
   if (pRolloverIcon) {
     if (CPDF_Dictionary* pImageDict = pRolloverIcon->GetDict()) {
       if (pImageDict->GetStringFor("Name").IsEmpty())
-        pImageDict->SetStringFor("Name", "ImgB");
+        pImageDict->SetNewFor<CPDF_String>("Name", "ImgB", false);
     }
   }
 
   if (pDownIcon) {
     if (CPDF_Dictionary* pImageDict = pDownIcon->GetDict()) {
       if (pImageDict->GetStringFor("Name").IsEmpty())
-        pImageDict->SetStringFor("Name", "ImgC");
+        pImageDict->SetNewFor<CPDF_String>("Name", "ImgC", false);
     }
   }
 
@@ -1804,14 +1807,12 @@
 
   CPDF_Document* pDoc = m_pPageView->GetPDFDocument();
   CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources");
-  if (!pStreamResList) {
-    pStreamResList = new CPDF_Dictionary(pDoc->GetByteStringPool());
-    pStreamDict->SetFor("Resources", pStreamResList);
-  }
+  if (!pStreamResList)
+    pStreamResList = pStreamDict->SetNewFor<CPDF_Dictionary>("Resources");
 
-  CPDF_Dictionary* pXObject = new CPDF_Dictionary(pDoc->GetByteStringPool());
-  pXObject->SetReferenceFor(sImageAlias, pDoc, pImage->GetObjNum());
-  pStreamResList->SetFor("XObject", pXObject);
+  CPDF_Dictionary* pXObject =
+      pStreamResList->SetNewFor<CPDF_Dictionary>("XObject");
+  pXObject->SetNewFor<CPDF_Reference>(sImageAlias, pDoc, pImage->GetObjNum());
 }
 
 void CPDFSDK_Widget::RemoveAppearance(const CFX_ByteString& sAPType) {
diff --git a/fpdfsdk/formfiller/cba_fontmap.cpp b/fpdfsdk/formfiller/cba_fontmap.cpp
index fd9304b..83e4579 100644
--- a/fpdfsdk/formfiller/cba_fontmap.cpp
+++ b/fpdfsdk/formfiller/cba_fontmap.cpp
@@ -9,6 +9,7 @@
 #include "core/fpdfapi/font/cpdf_font.h"
 #include "core/fpdfapi/page/cpdf_page.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
+#include "core/fpdfapi/parser/cpdf_reference.h"
 #include "core/fpdfapi/parser/cpdf_simple_parser.h"
 #include "core/fpdfapi/parser/cpdf_stream.h"
 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
@@ -119,11 +120,10 @@
   CPDF_Font* pFind = nullptr;
   for (const auto& it : *pFonts) {
     const CFX_ByteString& csKey = it.first;
-    CPDF_Object* pObj = it.second;
-    if (!pObj)
+    if (!it.second)
       continue;
 
-    CPDF_Dictionary* pElement = ToDictionary(pObj->GetDirect());
+    CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
     if (!pElement)
       continue;
     if (pElement->GetStringFor("Type") != "Font")
@@ -154,10 +154,8 @@
     return;
 
   CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDictFor("AP");
-  if (!pAPDict) {
-    pAPDict = new CPDF_Dictionary(m_pDocument->GetByteStringPool());
-    m_pAnnotDict->SetFor("AP", pAPDict);
-  }
+  if (!pAPDict)
+    pAPDict = m_pAnnotDict->SetNewFor<CPDF_Dictionary>("AP");
 
   // to avoid checkbox and radiobutton
   CPDF_Object* pObject = pAPDict->GetObjectFor(m_sAPType);
@@ -167,7 +165,8 @@
   CPDF_Stream* pStream = pAPDict->GetStreamFor(m_sAPType);
   if (!pStream) {
     pStream = m_pDocument->NewIndirect<CPDF_Stream>();
-    pAPDict->SetReferenceFor(m_sAPType, m_pDocument, pStream);
+    pAPDict->SetNewFor<CPDF_Reference>(m_sAPType, m_pDocument,
+                                       pStream->GetObjNum());
   }
 
   CPDF_Dictionary* pStreamDict = pStream->GetDict();
@@ -178,18 +177,17 @@
 
   if (pStreamDict) {
     CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources");
-    if (!pStreamResList) {
-      pStreamResList = new CPDF_Dictionary(m_pDocument->GetByteStringPool());
-      pStreamDict->SetFor("Resources", pStreamResList);
-    }
+    if (!pStreamResList)
+      pStreamResList = pStreamDict->SetNewFor<CPDF_Dictionary>("Resources");
     CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font");
     if (!pStreamResFontList) {
       pStreamResFontList = m_pDocument->NewIndirect<CPDF_Dictionary>();
-      pStreamResList->SetReferenceFor("Font", m_pDocument, pStreamResFontList);
+      pStreamResList->SetNewFor<CPDF_Reference>(
+          "Font", m_pDocument, pStreamResFontList->GetObjNum());
     }
     if (!pStreamResFontList->KeyExist(sAlias)) {
-      pStreamResFontList->SetReferenceFor(sAlias, m_pDocument,
-                                          pFont->GetFontDict()->GetObjNum());
+      pStreamResFontList->SetNewFor<CPDF_Reference>(
+          sAlias, m_pDocument, pFont->GetFontDict()->GetObjNum());
     }
   }
 }
diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp
index cb0a625..ccbb7b8 100644
--- a/fpdfsdk/fpdf_flatten.cpp
+++ b/fpdfsdk/fpdf_flatten.cpp
@@ -7,11 +7,15 @@
 #include "public/fpdf_flatten.h"
 
 #include <algorithm>
+#include <memory>
+#include <utility>
+#include <vector>
 
 #include "core/fpdfapi/page/cpdf_page.h"
 #include "core/fpdfapi/page/cpdf_pageobject.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
+#include "core/fpdfapi/parser/cpdf_name.h"
 #include "core/fpdfapi/parser/cpdf_number.h"
 #include "core/fpdfapi/parser/cpdf_reference.h"
 #include "core/fpdfapi/parser/cpdf_stream.h"
@@ -190,8 +194,8 @@
     pContentsArray = pPage->GetArrayFor("Contents");
     if (!pContentsArray) {
       if (!key.IsEmpty()) {
-        pPage->SetReferenceFor("Contents", pDocument,
-                               NewIndirectContentsStream(key, pDocument));
+        pPage->SetNewFor<CPDF_Reference>(
+            "Contents", pDocument, NewIndirectContentsStream(key, pDocument));
       }
       return;
     }
@@ -208,7 +212,8 @@
     pContentsStream->SetData(sStream.raw_str(), sStream.GetLength());
     pContentsArray->AddNew<CPDF_Reference>(pDocument,
                                            pContentsStream->GetObjNum());
-    pPage->SetReferenceFor("Contents", pDocument, pContentsArray);
+    pPage->SetNewFor<CPDF_Reference>("Contents", pDocument,
+                                     pContentsArray->GetObjNum());
   }
   if (!key.IsEmpty()) {
     pContentsArray->AddNew<CPDF_Reference>(
@@ -271,38 +276,32 @@
     rcOriginalCB = rcOriginalMB;
 
   if (!rcOriginalMB.IsEmpty()) {
-    CPDF_Array* pMediaBox = new CPDF_Array();
+    CPDF_Array* pMediaBox = pPageDict->SetNewFor<CPDF_Array>("MediaBox");
     pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.left);
     pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.bottom);
     pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.right);
     pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.top);
-    pPageDict->SetFor("MediaBox", pMediaBox);
   }
 
   if (!rcOriginalCB.IsEmpty()) {
-    CPDF_Array* pCropBox = new CPDF_Array();
+    CPDF_Array* pCropBox = pPageDict->SetNewFor<CPDF_Array>("ArtBox");
     pCropBox->AddNew<CPDF_Number>(rcOriginalCB.left);
     pCropBox->AddNew<CPDF_Number>(rcOriginalCB.bottom);
     pCropBox->AddNew<CPDF_Number>(rcOriginalCB.right);
     pCropBox->AddNew<CPDF_Number>(rcOriginalCB.top);
-    pPageDict->SetFor("ArtBox", pCropBox);
   }
 
   CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources");
-  if (!pRes) {
-    pRes = new CPDF_Dictionary(pDocument->GetByteStringPool());
-    pPageDict->SetFor("Resources", pRes);
-  }
+  if (!pRes)
+    pRes = pPageDict->SetNewFor<CPDF_Dictionary>("Resources");
 
   CPDF_Stream* pNewXObject = pDocument->NewIndirect<CPDF_Stream>(
       nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool()));
 
   uint32_t dwObjNum = pNewXObject->GetObjNum();
   CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject");
-  if (!pPageXObject) {
-    pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool());
-    pRes->SetFor("XObject", pPageXObject);
-  }
+  if (!pPageXObject)
+    pPageXObject = pRes->SetNewFor<CPDF_Dictionary>("XObject");
 
   CFX_ByteString key = "";
   int nStreams = pdfium::CollectionSize<int>(ObjectArray);
@@ -320,14 +319,13 @@
 
   CPDF_Dictionary* pNewXORes = nullptr;
   if (!key.IsEmpty()) {
-    pPageXObject->SetReferenceFor(key, pDocument, dwObjNum);
+    pPageXObject->SetNewFor<CPDF_Reference>(key, pDocument, dwObjNum);
     CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
-    pNewXORes = new CPDF_Dictionary(pDocument->GetByteStringPool());
-    pNewOXbjectDic->SetFor("Resources", pNewXORes);
-    pNewOXbjectDic->SetNameFor("Type", "XObject");
-    pNewOXbjectDic->SetNameFor("Subtype", "Form");
-    pNewOXbjectDic->SetIntegerFor("FormType", 1);
-    pNewOXbjectDic->SetNameFor("Name", "FRM");
+    pNewXORes = pNewOXbjectDic->SetNewFor<CPDF_Dictionary>("Resources");
+    pNewOXbjectDic->SetNewFor<CPDF_Name>("Type", "XObject");
+    pNewOXbjectDic->SetNewFor<CPDF_Name>("Subtype", "Form");
+    pNewOXbjectDic->SetNewFor<CPDF_Number>("FormType", 1);
+    pNewOXbjectDic->SetNewFor<CPDF_Name>("Name", "FRM");
     CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox");
     pNewOXbjectDic->SetRectFor("BBox", rcBBox);
   }
@@ -356,7 +354,7 @@
       } else {
         auto it = pAPDic->begin();
         if (it != pAPDic->end()) {
-          CPDF_Object* pFirstObj = it->second;
+          CPDF_Object* pFirstObj = it->second.get();
           if (pFirstObj) {
             if (pFirstObj->IsReference())
               pFirstObj = pFirstObj->GetDirect();
@@ -389,19 +387,18 @@
 
     CPDF_Dictionary* pObjDic = pObj->GetDict();
     if (pObjDic) {
-      pObjDic->SetNameFor("Type", "XObject");
-      pObjDic->SetNameFor("Subtype", "Form");
+      pObjDic->SetNewFor<CPDF_Name>("Type", "XObject");
+      pObjDic->SetNewFor<CPDF_Name>("Subtype", "Form");
     }
 
     CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject");
-    if (!pXObject) {
-      pXObject = new CPDF_Dictionary(pDocument->GetByteStringPool());
-      pNewXORes->SetFor("XObject", pXObject);
-    }
+    if (!pXObject)
+      pXObject = pNewXORes->SetNewFor<CPDF_Dictionary>("XObject");
 
     CFX_ByteString sFormName;
     sFormName.Format("F%d", i);
-    pXObject->SetReferenceFor(sFormName, pDocument, pObj->GetObjNum());
+    pXObject->SetNewFor<CPDF_Reference>(sFormName, pDocument,
+                                        pObj->GetObjNum());
 
     CPDF_StreamAcc acc;
     acc.LoadAllData(pNewXObject);
diff --git a/fpdfsdk/fpdf_transformpage.cpp b/fpdfsdk/fpdf_transformpage.cpp
index beaa6c4..b1e8da0 100644
--- a/fpdfsdk/fpdf_transformpage.cpp
+++ b/fpdfsdk/fpdf_transformpage.cpp
@@ -26,12 +26,11 @@
                     float bottom,
                     float right,
                     float top) {
-  CPDF_Array* pBoundingBoxArray = new CPDF_Array;
+  CPDF_Array* pBoundingBoxArray = page->m_pFormDict->SetNewFor<CPDF_Array>(key);
   pBoundingBoxArray->AddNew<CPDF_Number>(left);
   pBoundingBoxArray->AddNew<CPDF_Number>(bottom);
   pBoundingBoxArray->AddNew<CPDF_Number>(right);
   pBoundingBoxArray->AddNew<CPDF_Number>(top);
-  page->m_pFormDict->SetFor(key, pBoundingBoxArray);
 }
 
 bool GetBoundingBox(CPDF_Page* page,
@@ -158,7 +157,8 @@
         pContentArray->AddNew<CPDF_Reference>(pDoc, pStream->GetObjNum());
         pContentArray->AddNew<CPDF_Reference>(pDoc, pDirectObj->GetObjNum());
         pContentArray->AddNew<CPDF_Reference>(pDoc, pEndStream->GetObjNum());
-        pPageDic->SetReferenceFor("Contents", pDoc, pContentArray);
+        pPageDic->SetNewFor<CPDF_Reference>("Contents", pDoc,
+                                            pContentArray->GetObjNum());
       }
     }
   }
@@ -169,7 +169,7 @@
     CPDF_Dictionary* pPattenDict = pRes->GetDictFor("Pattern");
     if (pPattenDict) {
       for (const auto& it : *pPattenDict) {
-        CPDF_Object* pObj = it.second;
+        CPDF_Object* pObj = it.second.get();
         if (pObj->IsReference())
           pObj = pObj->GetDirect();
 
@@ -328,6 +328,7 @@
     CPDF_Array* pContentArray = pDoc->NewIndirect<CPDF_Array>();
     pContentArray->AddNew<CPDF_Reference>(pDoc, pStream->GetObjNum());
     pContentArray->AddNew<CPDF_Reference>(pDoc, pDirectObj->GetObjNum());
-    pPageDic->SetReferenceFor("Contents", pDoc, pContentArray);
+    pPageDic->SetNewFor<CPDF_Reference>("Contents", pDoc,
+                                        pContentArray->GetObjNum());
   }
 }
diff --git a/fpdfsdk/fpdfdoc_unittest.cpp b/fpdfsdk/fpdfdoc_unittest.cpp
index d049b4e..664ce39 100644
--- a/fpdfsdk/fpdfdoc_unittest.cpp
+++ b/fpdfsdk/fpdfdoc_unittest.cpp
@@ -105,7 +105,7 @@
   }
   {
     // Empty bookmark tree.
-    m_pRootObj->SetFor("Outlines", new CPDF_Dictionary());
+    m_pRootObj->SetNewFor<CPDF_Dictionary>("Outlines");
     std::unique_ptr<unsigned short, pdfium::FreeDeleter> title =
         GetFPDFWideString(L"");
     EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get()));
@@ -117,27 +117,27 @@
     // Check on a regular bookmark tree.
     auto bookmarks = CreateDictObjs(3);
 
-    bookmarks[1].obj->SetFor("Title", new CPDF_String(L"Chapter 1"));
-    bookmarks[1].obj->SetFor(
-        "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num));
-    bookmarks[1].obj->SetFor(
-        "Next", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num));
+    bookmarks[1].obj->SetNewFor<CPDF_String>("Title", L"Chapter 1");
+    bookmarks[1].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs,
+                                                bookmarks[0].num);
+    bookmarks[1].obj->SetNewFor<CPDF_Reference>("Next", m_pIndirectObjs,
+                                                bookmarks[2].num);
 
-    bookmarks[2].obj->SetFor("Title", new CPDF_String(L"Chapter 2"));
-    bookmarks[2].obj->SetFor(
-        "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num));
-    bookmarks[2].obj->SetFor(
-        "Prev", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num));
+    bookmarks[2].obj->SetNewFor<CPDF_String>("Title", L"Chapter 2");
+    bookmarks[2].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs,
+                                                bookmarks[0].num);
+    bookmarks[2].obj->SetNewFor<CPDF_Reference>("Prev", m_pIndirectObjs,
+                                                bookmarks[1].num);
 
-    bookmarks[0].obj->SetFor("Type", new CPDF_Name(nullptr, "Outlines"));
-    bookmarks[0].obj->SetFor("Count", new CPDF_Number(2));
-    bookmarks[0].obj->SetFor(
-        "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num));
-    bookmarks[0].obj->SetFor(
-        "Last", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num));
+    bookmarks[0].obj->SetNewFor<CPDF_Name>("Type", "Outlines");
+    bookmarks[0].obj->SetNewFor<CPDF_Number>("Count", 2);
+    bookmarks[0].obj->SetNewFor<CPDF_Reference>("First", m_pIndirectObjs,
+                                                bookmarks[1].num);
+    bookmarks[0].obj->SetNewFor<CPDF_Reference>("Last", m_pIndirectObjs,
+                                                bookmarks[2].num);
 
-    m_pRootObj->SetFor("Outlines",
-                       new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num));
+    m_pRootObj->SetNewFor<CPDF_Reference>("Outlines", m_pIndirectObjs,
+                                          bookmarks[0].num);
 
     // Title with no match.
     std::unique_ptr<unsigned short, pdfium::FreeDeleter> title =
@@ -160,27 +160,27 @@
     // Circular bookmarks in depth.
     auto bookmarks = CreateDictObjs(3);
 
-    bookmarks[1].obj->SetFor("Title", new CPDF_String(L"Chapter 1"));
-    bookmarks[1].obj->SetFor(
-        "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num));
-    bookmarks[1].obj->SetFor(
-        "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num));
+    bookmarks[1].obj->SetNewFor<CPDF_String>("Title", L"Chapter 1");
+    bookmarks[1].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs,
+                                                bookmarks[0].num);
+    bookmarks[1].obj->SetNewFor<CPDF_Reference>("First", m_pIndirectObjs,
+                                                bookmarks[2].num);
 
-    bookmarks[2].obj->SetFor("Title", new CPDF_String(L"Chapter 2"));
-    bookmarks[2].obj->SetFor(
-        "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num));
-    bookmarks[2].obj->SetFor(
-        "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num));
+    bookmarks[2].obj->SetNewFor<CPDF_String>("Title", L"Chapter 2");
+    bookmarks[2].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs,
+                                                bookmarks[1].num);
+    bookmarks[2].obj->SetNewFor<CPDF_Reference>("First", m_pIndirectObjs,
+                                                bookmarks[1].num);
 
-    bookmarks[0].obj->SetFor("Type", new CPDF_Name(nullptr, "Outlines"));
-    bookmarks[0].obj->SetFor("Count", new CPDF_Number(2));
-    bookmarks[0].obj->SetFor(
-        "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num));
-    bookmarks[0].obj->SetFor(
-        "Last", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num));
+    bookmarks[0].obj->SetNewFor<CPDF_Name>("Type", "Outlines");
+    bookmarks[0].obj->SetNewFor<CPDF_Number>("Count", 2);
+    bookmarks[0].obj->SetNewFor<CPDF_Reference>("First", m_pIndirectObjs,
+                                                bookmarks[1].num);
+    bookmarks[0].obj->SetNewFor<CPDF_Reference>("Last", m_pIndirectObjs,
+                                                bookmarks[2].num);
 
-    m_pRootObj->SetFor("Outlines",
-                       new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num));
+    m_pRootObj->SetNewFor<CPDF_Reference>("Outlines", m_pIndirectObjs,
+                                          bookmarks[0].num);
 
     // Title with no match.
     std::unique_ptr<unsigned short, pdfium::FreeDeleter> title =
@@ -195,33 +195,33 @@
     // Circular bookmarks in breadth.
     auto bookmarks = CreateDictObjs(4);
 
-    bookmarks[1].obj->SetFor("Title", new CPDF_String(L"Chapter 1"));
-    bookmarks[1].obj->SetFor(
-        "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num));
-    bookmarks[1].obj->SetFor(
-        "Next", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num));
+    bookmarks[1].obj->SetNewFor<CPDF_String>("Title", L"Chapter 1");
+    bookmarks[1].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs,
+                                                bookmarks[0].num);
+    bookmarks[1].obj->SetNewFor<CPDF_Reference>("Next", m_pIndirectObjs,
+                                                bookmarks[2].num);
 
-    bookmarks[2].obj->SetFor("Title", new CPDF_String(L"Chapter 2"));
-    bookmarks[2].obj->SetFor(
-        "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num));
-    bookmarks[2].obj->SetFor(
-        "Next", new CPDF_Reference(m_pIndirectObjs, bookmarks[3].num));
+    bookmarks[2].obj->SetNewFor<CPDF_String>("Title", L"Chapter 2");
+    bookmarks[2].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs,
+                                                bookmarks[0].num);
+    bookmarks[2].obj->SetNewFor<CPDF_Reference>("Next", m_pIndirectObjs,
+                                                bookmarks[3].num);
 
-    bookmarks[3].obj->SetFor("Title", new CPDF_String(L"Chapter 3"));
-    bookmarks[3].obj->SetFor(
-        "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num));
-    bookmarks[3].obj->SetFor(
-        "Next", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num));
+    bookmarks[3].obj->SetNewFor<CPDF_String>("Title", L"Chapter 3");
+    bookmarks[3].obj->SetNewFor<CPDF_Reference>("Parent", m_pIndirectObjs,
+                                                bookmarks[0].num);
+    bookmarks[3].obj->SetNewFor<CPDF_Reference>("Next", m_pIndirectObjs,
+                                                bookmarks[1].num);
 
-    bookmarks[0].obj->SetFor("Type", new CPDF_Name(nullptr, "Outlines"));
-    bookmarks[0].obj->SetFor("Count", new CPDF_Number(2));
-    bookmarks[0].obj->SetFor(
-        "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num));
-    bookmarks[0].obj->SetFor(
-        "Last", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num));
+    bookmarks[0].obj->SetNewFor<CPDF_Name>("Type", "Outlines");
+    bookmarks[0].obj->SetNewFor<CPDF_Number>("Count", 2);
+    bookmarks[0].obj->SetNewFor<CPDF_Reference>("First", m_pIndirectObjs,
+                                                bookmarks[1].num);
+    bookmarks[0].obj->SetNewFor<CPDF_Reference>("Last", m_pIndirectObjs,
+                                                bookmarks[2].num);
 
-    m_pRootObj->SetFor("Outlines",
-                       new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num));
+    m_pRootObj->SetNewFor<CPDF_Reference>("Outlines", m_pIndirectObjs,
+                                          bookmarks[0].num);
 
     // Title with no match.
     std::unique_ptr<unsigned short, pdfium::FreeDeleter> title =
diff --git a/fpdfsdk/fpdfeditpage.cpp b/fpdfsdk/fpdfeditpage.cpp
index 847adac..c864b82 100644
--- a/fpdfsdk/fpdfeditpage.cpp
+++ b/fpdfsdk/fpdfeditpage.cpp
@@ -83,11 +83,9 @@
   CPDF_Dictionary* pInfoDict = nullptr;
   pInfoDict = pDoc->GetInfo();
   if (pInfoDict) {
-    if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) {
-      pInfoDict->SetFor("CreationDate",
-                        new CPDF_String(nullptr, DateStr, false));
-    }
-    pInfoDict->SetFor("Creator", new CPDF_String(L"PDFium"));
+    if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
+      pInfoDict->SetNewFor<CPDF_String>("CreationDate", DateStr, false);
+    pInfoDict->SetNewFor<CPDF_String>("Creator", L"PDFium");
   }
 
   return FPDFDocumentFromCPDFDocument(pDoc);
@@ -111,15 +109,13 @@
   if (!pPageDict)
     return nullptr;
 
-  CPDF_Array* pMediaBoxArray = new CPDF_Array;
+  CPDF_Array* pMediaBoxArray = pPageDict->SetNewFor<CPDF_Array>("MediaBox");
   pMediaBoxArray->AddNew<CPDF_Number>(0);
   pMediaBoxArray->AddNew<CPDF_Number>(0);
   pMediaBoxArray->AddNew<CPDF_Number>(static_cast<FX_FLOAT>(width));
   pMediaBoxArray->AddNew<CPDF_Number>(static_cast<FX_FLOAT>(height));
-  pPageDict->SetFor("MediaBox", pMediaBoxArray);
-  pPageDict->SetFor("Rotate", new CPDF_Number(0));
-  pPageDict->SetFor("Resources",
-                    new CPDF_Dictionary(pDoc->GetByteStringPool()));
+  pPageDict->SetNewFor<CPDF_Number>("Rotate", 0);
+  pPageDict->SetNewFor<CPDF_Dictionary>("Resources");
 
 #ifdef PDF_ENABLE_XFA
   CPDFXFA_Page* pPage =
@@ -296,10 +292,9 @@
     rect.Transform(&matrix);
 
     CPDF_Array* pRectArray = pAnnot->GetAnnotDict()->GetArrayFor("Rect");
-    if (!pRectArray) {
-      pRectArray = new CPDF_Array;
-      pAnnot->GetAnnotDict()->SetFor("Rect", pRectArray);
-    }
+    if (!pRectArray)
+      pRectArray = pAnnot->GetAnnotDict()->SetNewFor<CPDF_Array>("Rect");
+
     pRectArray->SetNewAt<CPDF_Number>(0, rect.left);
     pRectArray->SetNewAt<CPDF_Number>(1, rect.bottom);
     pRectArray->SetNewAt<CPDF_Number>(2, rect.right);
@@ -316,5 +311,5 @@
 
   CPDF_Dictionary* pDict = pPage->m_pFormDict;
   rotate %= 4;
-  pDict->SetFor("Rotate", new CPDF_Number(rotate * 90));
+  pDict->SetNewFor<CPDF_Number>("Rotate", rotate * 90);
 }
diff --git a/fpdfsdk/fpdfppo.cpp b/fpdfsdk/fpdfppo.cpp
index 915de1a..4e06856 100644
--- a/fpdfsdk/fpdfppo.cpp
+++ b/fpdfsdk/fpdfppo.cpp
@@ -8,6 +8,7 @@
 
 #include <map>
 #include <memory>
+#include <utility>
 #include <vector>
 
 #include "core/fpdfapi/parser/cpdf_array.h"
@@ -64,7 +65,7 @@
   if (!pInheritable)
     return false;
 
-  pCurPageDict->SetFor(key, pInheritable->Clone().release());
+  pCurPageDict->SetFor(key, pInheritable->Clone());
   return true;
 }
 
@@ -158,31 +159,30 @@
   if (!pDocInfoDict)
     return false;
 
-  CFX_ByteString producerstr;
-  producerstr.Format("PDFium");
-  pDocInfoDict->SetFor("Producer",
-                       new CPDF_String(nullptr, producerstr, false));
+  pDocInfoDict->SetNewFor<CPDF_String>("Producer", "PDFium", false);
 
   CFX_ByteString cbRootType = pNewRoot->GetStringFor("Type", "");
   if (cbRootType.IsEmpty())
-    pNewRoot->SetFor("Type", new CPDF_Name(nullptr, "Catalog"));
+    pNewRoot->SetNewFor<CPDF_Name>("Type", "Catalog");
 
   CPDF_Object* pElement = pNewRoot->GetObjectFor("Pages");
   CPDF_Dictionary* pNewPages =
       pElement ? ToDictionary(pElement->GetDirect()) : nullptr;
   if (!pNewPages) {
     pNewPages = m_pDestPDFDoc->NewIndirect<CPDF_Dictionary>();
-    pNewRoot->SetReferenceFor("Pages", m_pDestPDFDoc, pNewPages);
+    pNewRoot->SetNewFor<CPDF_Reference>("Pages", m_pDestPDFDoc,
+                                        pNewPages->GetObjNum());
   }
 
   CFX_ByteString cbPageType = pNewPages->GetStringFor("Type", "");
   if (cbPageType.IsEmpty())
-    pNewPages->SetFor("Type", new CPDF_Name(nullptr, "Pages"));
+    pNewPages->SetNewFor<CPDF_Name>("Type", "Pages");
 
   if (!pNewPages->GetArrayFor("Kids")) {
-    pNewPages->SetIntegerFor("Count", 0);
-    pNewPages->SetReferenceFor("Kids", m_pDestPDFDoc,
-                               m_pDestPDFDoc->NewIndirect<CPDF_Array>());
+    pNewPages->SetNewFor<CPDF_Number>("Count", 0);
+    pNewPages->SetNewFor<CPDF_Reference>(
+        "Kids", m_pDestPDFDoc,
+        m_pDestPDFDoc->NewIndirect<CPDF_Array>()->GetObjNum());
   }
 
   return true;
@@ -202,11 +202,11 @@
     // Clone the page dictionary
     for (const auto& it : *pSrcPageDict) {
       const CFX_ByteString& cbSrcKeyStr = it.first;
-      CPDF_Object* pObj = it.second;
       if (cbSrcKeyStr == "Type" || cbSrcKeyStr == "Parent")
         continue;
 
-      pCurPageDict->SetFor(cbSrcKeyStr, pObj->Clone().release());
+      CPDF_Object* pObj = it.second.get();
+      pCurPageDict->SetFor(cbSrcKeyStr, pObj->Clone());
     }
 
     // inheritable item
@@ -217,15 +217,14 @@
       CPDF_Object* pInheritable =
           PageDictGetInheritableTag(pSrcPageDict, "CropBox");
       if (pInheritable) {
-        pCurPageDict->SetFor("MediaBox", pInheritable->Clone().release());
+        pCurPageDict->SetFor("MediaBox", pInheritable->Clone());
       } else {
         // Make the default size to be letter size (8.5'x11')
-        CPDF_Array* pArray = new CPDF_Array;
+        CPDF_Array* pArray = pCurPageDict->SetNewFor<CPDF_Array>("MediaBox");
         pArray->AddNew<CPDF_Number>(0);
         pArray->AddNew<CPDF_Number>(0);
         pArray->AddNew<CPDF_Number>(612);
         pArray->AddNew<CPDF_Number>(792);
-        pCurPageDict->SetFor("MediaBox", pArray);
       }
     }
 
@@ -265,7 +264,7 @@
       auto it = pDict->begin();
       while (it != pDict->end()) {
         const CFX_ByteString& key = it->first;
-        CPDF_Object* pNextObj = it->second;
+        CPDF_Object* pNextObj = it->second.get();
         ++it;
         if (key == "Parent" || key == "Prev" || key == "First")
           continue;
@@ -386,7 +385,6 @@
   if (!pDstDict)
     return false;
 
-  pDstDict->SetFor("ViewerPreferences",
-                   pSrcDict->CloneDirectObject().release());
+  pDstDict->SetFor("ViewerPreferences", pSrcDict->CloneDirectObject());
   return true;
 }
diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp
index 334c14c..d5f9a0e 100644
--- a/fpdfsdk/fpdfview.cpp
+++ b/fpdfsdk/fpdfview.cpp
@@ -1082,7 +1082,7 @@
     int i = 0;
     for (const auto& it : *pDest) {
       bsName = it.first;
-      pDestObj = it.second;
+      pDestObj = it.second.get();
       if (!pDestObj)
         continue;
       if (i == index)
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
index 26227c0..9bb1418 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
@@ -6,6 +6,8 @@
 
 #include "fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.h"
 
+#include <memory>
+
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
 #include "core/fpdfapi/parser/cpdf_string.h"
@@ -398,7 +400,7 @@
     return;
 
   if (CPDF_Dictionary* pInfoDict = m_pContext->GetPDFDoc()->GetInfo())
-    pInfoDict->SetFor("Title", new CPDF_String(wsTitle));
+    pInfoDict->SetNewFor<CPDF_String>("Title", wsTitle);
 }
 
 void CPDFXFA_DocEnvironment::ExportData(CXFA_FFDoc* hDoc,
diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp
index 54761a2..29f9764 100644
--- a/fpdfsdk/javascript/Document.cpp
+++ b/fpdfsdk/javascript/Document.cpp
@@ -13,6 +13,7 @@
 #include "core/fpdfapi/page/cpdf_page.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
+#include "core/fpdfapi/parser/cpdf_string.h"
 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
 #include "core/fpdfdoc/cpdf_interform.h"
 #include "core/fpdfdoc/cpdf_nametree.h"
@@ -809,7 +810,7 @@
   // It's to be compatible to non-standard info dictionary.
   for (const auto& it : *pDictionary) {
     const CFX_ByteString& bsKey = it.first;
-    CPDF_Object* pValueObj = it.second;
+    CPDF_Object* pValueObj = it.second.get();
     CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC());
     if (pValueObj->IsString() || pValueObj->IsName()) {
       pRuntime->PutObjectString(pObj, wsKey, pValueObj->GetUnicodeText());
@@ -844,7 +845,8 @@
     }
     CFX_WideString csProperty;
     vp >> csProperty;
-    pDictionary->SetStringFor(propName, PDF_EncodeText(csProperty));
+    pDictionary->SetNewFor<CPDF_String>(propName, PDF_EncodeText(csProperty),
+                                        false);
     m_pFormFillEnv->SetChangeMark();
   }
   return true;