Convert remaining UnownedPtr<CPDF_Object> to RetainPtr.

Avoids numerous lifetime issues associated with unowned pointers.

Bug: chromium:956947
Change-Id: I9272981752917a81d5e691bf333e1bc9b57ea722
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/53890
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index a678f39..a99f1ed 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -200,7 +200,8 @@
     const ByteString& bsType) const {
   ASSERT(pResource);
   if (!m_pObjHolder->m_pResources) {
-    m_pObjHolder->m_pResources = m_pDocument->NewIndirect<CPDF_Dictionary>();
+    m_pObjHolder->m_pResources.Reset(
+        m_pDocument->NewIndirect<CPDF_Dictionary>());
     m_pObjHolder->GetDict()->SetFor(
         "Resources",
         m_pObjHolder->m_pResources->MakeReference(m_pDocument.Get()));
diff --git a/core/fpdfapi/edit/cpdf_pagecontentmanager.cpp b/core/fpdfapi/edit/cpdf_pagecontentmanager.cpp
index f9057db..95a7a85 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentmanager.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentmanager.cpp
@@ -22,7 +22,7 @@
   CPDF_Object* contents_obj = page_dict->GetObjectFor("Contents");
   CPDF_Array* contents_array = ToArray(contents_obj);
   if (contents_array) {
-    contents_array_ = contents_array;
+    contents_array_.Reset(contents_array);
     return;
   }
 
@@ -34,9 +34,9 @@
 
     contents_array = indirect_obj->AsArray();
     if (contents_array)
-      contents_array_ = contents_array;
+      contents_array_.Reset(contents_array);
     else if (indirect_obj->IsStream())
-      contents_stream_ = indirect_obj->AsStream();
+      contents_stream_.Reset(indirect_obj->AsStream());
   }
 }
 
@@ -72,7 +72,7 @@
     CPDF_Dictionary* page_dict = obj_holder_->GetDict();
     page_dict->SetFor("Contents",
                       new_contents_array->MakeReference(doc_.Get()));
-    contents_array_ = new_contents_array;
+    contents_array_.Reset(new_contents_array);
     contents_stream_ = nullptr;
     return 1;
   }
@@ -87,7 +87,7 @@
   // Its index is 0.
   CPDF_Dictionary* page_dict = obj_holder_->GetDict();
   page_dict->SetFor("Contents", new_stream->MakeReference(doc_.Get()));
-  contents_stream_ = new_stream;
+  contents_stream_.Reset(new_stream);
   return 0;
 }
 
diff --git a/core/fpdfapi/edit/cpdf_pagecontentmanager.h b/core/fpdfapi/edit/cpdf_pagecontentmanager.h
index b49b403..cf5021c 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentmanager.h
+++ b/core/fpdfapi/edit/cpdf_pagecontentmanager.h
@@ -8,6 +8,7 @@
 #include <set>
 #include <sstream>
 
+#include "core/fxcrt/retain_ptr.h"
 #include "core/fxcrt/unowned_ptr.h"
 
 class CPDF_Array;
@@ -41,8 +42,8 @@
  private:
   UnownedPtr<const CPDF_PageObjectHolder> const obj_holder_;
   UnownedPtr<CPDF_Document> const doc_;
-  UnownedPtr<CPDF_Array> contents_array_;
-  UnownedPtr<CPDF_Stream> contents_stream_;
+  RetainPtr<CPDF_Array> contents_array_;
+  RetainPtr<CPDF_Stream> contents_stream_;
   std::set<size_t> streams_to_remove_;
 };
 
diff --git a/core/fpdfapi/font/cpdf_type3font.cpp b/core/fpdfapi/font/cpdf_type3font.cpp
index df56e5f..a80048a 100644
--- a/core/fpdfapi/font/cpdf_type3font.cpp
+++ b/core/fpdfapi/font/cpdf_type3font.cpp
@@ -44,7 +44,7 @@
 }
 
 bool CPDF_Type3Font::Load() {
-  m_pFontResources = m_pFontDict->GetDictFor("Resources");
+  m_pFontResources.Reset(m_pFontDict->GetDictFor("Resources"));
   const CPDF_Array* pMatrix = m_pFontDict->GetArrayFor("FontMatrix");
   float xscale = 1.0f;
   float yscale = 1.0f;
@@ -77,7 +77,7 @@
       }
     }
   }
-  m_pCharProcs = m_pFontDict->GetDictFor("CharProcs");
+  m_pCharProcs.Reset(m_pFontDict->GetDictFor("CharProcs"));
   if (m_pFontDict->GetDirectObjectFor("Encoding"))
     LoadPDFEncoding(false, false);
   return true;
diff --git a/core/fpdfapi/font/cpdf_type3font.h b/core/fpdfapi/font/cpdf_type3font.h
index fb32323..080b216 100644
--- a/core/fpdfapi/font/cpdf_type3font.h
+++ b/core/fpdfapi/font/cpdf_type3font.h
@@ -30,7 +30,7 @@
   FX_RECT GetCharBBox(uint32_t charcode) override;
 
   void SetPageResources(CPDF_Dictionary* pResources) {
-    m_pPageResources = pResources;
+    m_pPageResources.Reset(pResources);
   }
   CPDF_Type3Char* LoadChar(uint32_t charcode);
   void CheckType3FontMetrics();
@@ -46,9 +46,9 @@
 
   CFX_Matrix m_FontMatrix;
   uint32_t m_CharWidthL[256];
-  UnownedPtr<CPDF_Dictionary> m_pCharProcs;
-  UnownedPtr<CPDF_Dictionary> m_pPageResources;
-  UnownedPtr<CPDF_Dictionary> m_pFontResources;
+  RetainPtr<CPDF_Dictionary> m_pCharProcs;
+  RetainPtr<CPDF_Dictionary> m_pPageResources;
+  RetainPtr<CPDF_Dictionary> m_pFontResources;
   std::map<uint32_t, std::unique_ptr<CPDF_Type3Char>> m_CacheMap;
   // The depth char loading is in, to avoid recurive calling LoadChar().
   int m_CharLoadingDepth = 0;
diff --git a/core/fpdfapi/page/cpdf_contentmarkitem.cpp b/core/fpdfapi/page/cpdf_contentmarkitem.cpp
index 89191f7..a361e5b 100644
--- a/core/fpdfapi/page/cpdf_contentmarkitem.cpp
+++ b/core/fpdfapi/page/cpdf_contentmarkitem.cpp
@@ -46,6 +46,6 @@
     CPDF_Dictionary* pHolder,
     const ByteString& property_name) {
   m_ParamType = kPropertiesDict;
-  m_pPropertiesHolder = pHolder;
+  m_pPropertiesHolder.Reset(pHolder);
   m_PropertyName = property_name;
 }
diff --git a/core/fpdfapi/page/cpdf_contentmarkitem.h b/core/fpdfapi/page/cpdf_contentmarkitem.h
index 9a8ea46..15a34e9 100644
--- a/core/fpdfapi/page/cpdf_contentmarkitem.h
+++ b/core/fpdfapi/page/cpdf_contentmarkitem.h
@@ -12,7 +12,6 @@
 #include "core/fxcrt/fx_string.h"
 #include "core/fxcrt/fx_system.h"
 #include "core/fxcrt/retain_ptr.h"
-#include "core/fxcrt/unowned_ptr.h"
 
 class CPDF_Dictionary;
 
@@ -38,7 +37,7 @@
   ParamType m_ParamType = kNone;
   ByteString m_MarkName;
   ByteString m_PropertyName;
-  UnownedPtr<CPDF_Dictionary> m_pPropertiesHolder;
+  RetainPtr<CPDF_Dictionary> m_pPropertiesHolder;
   RetainPtr<CPDF_Dictionary> m_pDirectDict;
 };
 
diff --git a/core/fpdfapi/page/cpdf_form.h b/core/fpdfapi/page/cpdf_form.h
index f00a205..1fe0ae2 100644
--- a/core/fpdfapi/page/cpdf_form.h
+++ b/core/fpdfapi/page/cpdf_form.h
@@ -45,7 +45,7 @@
  private:
   std::unique_ptr<std::set<const uint8_t*>> m_ParsedSet;
 
-  UnownedPtr<CPDF_Stream> const m_pFormStream;
+  RetainPtr<CPDF_Stream> const m_pFormStream;
 };
 
 #endif  // CORE_FPDFAPI_PAGE_CPDF_FORM_H_
diff --git a/core/fpdfapi/page/cpdf_generalstate.cpp b/core/fpdfapi/page/cpdf_generalstate.cpp
index cf942ab..7b9ea36 100644
--- a/core/fpdfapi/page/cpdf_generalstate.cpp
+++ b/core/fpdfapi/page/cpdf_generalstate.cpp
@@ -150,7 +150,7 @@
 }
 
 void CPDF_GeneralState::SetSoftMask(CPDF_Object* pObject) {
-  m_Ref.GetPrivateCopy()->m_pSoftMask = pObject;
+  m_Ref.GetPrivateCopy()->m_pSoftMask.Reset(pObject);
 }
 
 const CPDF_Object* CPDF_GeneralState::GetTR() const {
diff --git a/core/fpdfapi/page/cpdf_generalstate.h b/core/fpdfapi/page/cpdf_generalstate.h
index 7eff896..20b8679 100644
--- a/core/fpdfapi/page/cpdf_generalstate.h
+++ b/core/fpdfapi/page/cpdf_generalstate.h
@@ -85,7 +85,7 @@
 
     ByteString m_BlendMode = pdfium::transparency::kNormal;
     BlendMode m_BlendType = BlendMode::kNormal;
-    UnownedPtr<CPDF_Object> m_pSoftMask;
+    RetainPtr<CPDF_Object> m_pSoftMask;
     CFX_Matrix m_SMaskMatrix;
     float m_StrokeAlpha = 1.0f;
     float m_FillAlpha = 1.0f;
diff --git a/core/fpdfapi/page/cpdf_page.cpp b/core/fpdfapi/page/cpdf_page.cpp
index 56874fb..8d25410 100644
--- a/core/fpdfapi/page/cpdf_page.cpp
+++ b/core/fpdfapi/page/cpdf_page.cpp
@@ -34,7 +34,7 @@
   // CPDF_PageObjectHolder ctor because GetPageAttr() requires
   // CPDF_PageObjectHolder to finish initializing first.
   CPDF_Object* pPageAttr = GetPageAttr(pdfium::page_object::kResources);
-  m_pResources = pPageAttr ? pPageAttr->GetDict() : nullptr;
+  m_pResources.Reset(pPageAttr ? pPageAttr->GetDict() : nullptr);
   m_pPageResources = m_pResources;
 
   UpdateDimensions();
diff --git a/core/fpdfapi/page/cpdf_pageobjectholder.h b/core/fpdfapi/page/cpdf_pageobjectholder.h
index 3c17a3a..524ab98 100644
--- a/core/fpdfapi/page/cpdf_pageobjectholder.h
+++ b/core/fpdfapi/page/cpdf_pageobjectholder.h
@@ -94,8 +94,8 @@
   const std::set<int32_t>& GetDirtyStreams() const { return m_DirtyStreams; }
   void ClearDirtyStreams() { m_DirtyStreams.clear(); }
 
-  UnownedPtr<CPDF_Dictionary> m_pPageResources;
-  UnownedPtr<CPDF_Dictionary> m_pResources;
+  RetainPtr<CPDF_Dictionary> m_pPageResources;
+  RetainPtr<CPDF_Dictionary> m_pResources;
   std::map<GraphicsData, ByteString> m_GraphicsMap;
   std::map<FontData, ByteString> m_FontsMap;
 
@@ -108,7 +108,7 @@
  private:
   bool m_bBackgroundAlphaNeeded = false;
   ParseState m_ParseState = ParseState::kNotParsed;
-  UnownedPtr<CPDF_Dictionary> const m_pDict;
+  RetainPtr<CPDF_Dictionary> const m_pDict;
   UnownedPtr<CPDF_Document> m_pDocument;
   std::vector<CFX_FloatRect> m_MaskBoundingBoxes;
   std::unique_ptr<CPDF_ContentParser> m_pParser;
diff --git a/core/fpdfapi/page/cpdf_pattern.h b/core/fpdfapi/page/cpdf_pattern.h
index 307c677..2fc6b8e 100644
--- a/core/fpdfapi/page/cpdf_pattern.h
+++ b/core/fpdfapi/page/cpdf_pattern.h
@@ -10,6 +10,7 @@
 #include "core/fpdfapi/page/cpdf_countedobject.h"
 #include "core/fxcrt/fx_coordinates.h"
 #include "core/fxcrt/fx_system.h"
+#include "core/fxcrt/retain_ptr.h"
 #include "core/fxcrt/unowned_ptr.h"
 
 class CPDF_Document;
@@ -41,7 +42,7 @@
 
  private:
   UnownedPtr<CPDF_Document> const m_pDocument;
-  UnownedPtr<CPDF_Object> const m_pPatternObj;
+  RetainPtr<CPDF_Object> const m_pPatternObj;
   CFX_Matrix m_Pattern2Form;
   const CFX_Matrix m_ParentMatrix;
 };
diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.h b/core/fpdfapi/page/cpdf_streamcontentparser.h
index 25ac268..b6d36b7 100644
--- a/core/fpdfapi/page/cpdf_streamcontentparser.h
+++ b/core/fpdfapi/page/cpdf_streamcontentparser.h
@@ -202,9 +202,9 @@
   void Handle_Invalid();
 
   UnownedPtr<CPDF_Document> const m_pDocument;
-  UnownedPtr<CPDF_Dictionary> const m_pPageResources;
-  UnownedPtr<CPDF_Dictionary> const m_pParentResources;
-  UnownedPtr<CPDF_Dictionary> const m_pResources;
+  RetainPtr<CPDF_Dictionary> const m_pPageResources;
+  RetainPtr<CPDF_Dictionary> const m_pParentResources;
+  RetainPtr<CPDF_Dictionary> const m_pResources;
   UnownedPtr<CPDF_PageObjectHolder> const m_pObjectHolder;
   UnownedPtr<std::set<const uint8_t*>> const m_ParsedSet;
   CFX_Matrix m_mtContentToUser;
diff --git a/core/fpdfapi/parser/cfdf_document.cpp b/core/fpdfapi/parser/cfdf_document.cpp
index b39db02..054f999 100644
--- a/core/fpdfapi/parser/cfdf_document.cpp
+++ b/core/fpdfapi/parser/cfdf_document.cpp
@@ -23,7 +23,7 @@
 
 std::unique_ptr<CFDF_Document> CFDF_Document::CreateNewDoc() {
   auto pDoc = pdfium::MakeUnique<CFDF_Document>();
-  pDoc->m_pRootDict = pDoc->NewIndirect<CPDF_Dictionary>();
+  pDoc->m_pRootDict.Reset(pDoc->NewIndirect<CPDF_Dictionary>());
   pDoc->m_pRootDict->SetNewFor<CPDF_Dictionary>("FDF");
   return pDoc;
 }
@@ -69,7 +69,7 @@
       RetainPtr<CPDF_Dictionary> pMainDict =
           ToDictionary(parser.GetObjectBody(this));
       if (pMainDict)
-        m_pRootDict = pMainDict->GetDictFor("Root");
+        m_pRootDict.Reset(pMainDict->GetDictFor("Root"));
 
       break;
     }
diff --git a/core/fpdfapi/parser/cfdf_document.h b/core/fpdfapi/parser/cfdf_document.h
index 3c93a5e..cad94d0 100644
--- a/core/fpdfapi/parser/cfdf_document.h
+++ b/core/fpdfapi/parser/cfdf_document.h
@@ -10,7 +10,7 @@
 #include <memory>
 
 #include "core/fpdfapi/parser/cpdf_indirect_object_holder.h"
-#include "core/fxcrt/unowned_ptr.h"
+#include "core/fxcrt/retain_ptr.h"
 #include "third_party/base/span.h"
 
 class CPDF_Dictionary;
@@ -31,7 +31,7 @@
  private:
   void ParseStream(RetainPtr<IFX_SeekableReadStream> pFile);
 
-  UnownedPtr<CPDF_Dictionary> m_pRootDict;
+  RetainPtr<CPDF_Dictionary> m_pRootDict;
   RetainPtr<IFX_SeekableReadStream> m_pFile;
 };
 
diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp
index 459cad1..5b2c416 100644
--- a/core/fpdfapi/parser/cpdf_document.cpp
+++ b/core/fpdfapi/parser/cpdf_document.cpp
@@ -200,7 +200,7 @@
 
   CPDF_Object* pRootObj = GetOrParseIndirectObject(m_pParser->GetRootObjNum());
   if (pRootObj)
-    m_pRootDict = pRootObj->GetDict();
+    m_pRootDict.Reset(pRootObj->GetDict());
 
   LoadPages();
   return GetRoot() && GetPageCount() > 0;
@@ -501,7 +501,7 @@
 void CPDF_Document::CreateNewDoc() {
   ASSERT(!m_pRootDict);
   ASSERT(!m_pInfoDict);
-  m_pRootDict = NewIndirect<CPDF_Dictionary>();
+  m_pRootDict.Reset(NewIndirect<CPDF_Dictionary>());
   m_pRootDict->SetNewFor<CPDF_Name>("Type", "Catalog");
 
   CPDF_Dictionary* pPages = NewIndirect<CPDF_Dictionary>();
@@ -509,7 +509,7 @@
   pPages->SetNewFor<CPDF_Number>("Count", 0);
   pPages->SetNewFor<CPDF_Array>("Kids");
   m_pRootDict->SetFor("Pages", pPages->MakeReference(this));
-  m_pInfoDict = NewIndirect<CPDF_Dictionary>();
+  m_pInfoDict.Reset(NewIndirect<CPDF_Dictionary>());
 }
 
 CPDF_Dictionary* CPDF_Document::CreateNewPage(int iPage) {
@@ -605,7 +605,7 @@
 
   auto ref =
       pdfium::MakeRetain<CPDF_Reference>(this, m_pParser->GetInfoObjNum());
-  m_pInfoDict = ToDictionary(ref->GetDirect());
+  m_pInfoDict.Reset(ToDictionary(ref->GetDirect()));
   return m_pInfoDict.Get();
 }
 
diff --git a/core/fpdfapi/parser/cpdf_document.h b/core/fpdfapi/parser/cpdf_document.h
index 2153813..5e1a28f 100644
--- a/core/fpdfapi/parser/cpdf_document.h
+++ b/core/fpdfapi/parser/cpdf_document.h
@@ -169,8 +169,8 @@
   CPDF_Parser::Error HandleLoadResult(CPDF_Parser::Error error);
 
   std::unique_ptr<CPDF_Parser> m_pParser;
-  UnownedPtr<CPDF_Dictionary> m_pRootDict;
-  UnownedPtr<CPDF_Dictionary> m_pInfoDict;
+  RetainPtr<CPDF_Dictionary> m_pRootDict;
+  RetainPtr<CPDF_Dictionary> m_pInfoDict;
 
   // Vector of pairs to know current position in the page tree. The index in the
   // vector corresponds to the level being described. The pair contains a
diff --git a/core/fpdfapi/parser/cpdf_document_unittest.cpp b/core/fpdfapi/parser/cpdf_document_unittest.cpp
index d20a654..5078bf4 100644
--- a/core/fpdfapi/parser/cpdf_document_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_document_unittest.cpp
@@ -86,7 +86,7 @@
     CPDF_Dictionary* pagesDict =
         CreatePageTreeNode(std::move(allPages), this, kNumTestPages);
 
-    m_pRootDict = NewIndirect<CPDF_Dictionary>();
+    m_pRootDict.Reset(NewIndirect<CPDF_Dictionary>());
     m_pRootDict->SetNewFor<CPDF_Reference>("Pages", this,
                                            pagesDict->GetObjNum());
     m_PageList.resize(kNumTestPages);
@@ -111,7 +111,7 @@
     inlined_page_ = allPages->Add(CreateNumberedPage(2));
     CPDF_Dictionary* pagesDict =
         CreatePageTreeNode(std::move(allPages), this, 3);
-    m_pRootDict = NewIndirect<CPDF_Dictionary>();
+    m_pRootDict.Reset(NewIndirect<CPDF_Dictionary>());
     m_pRootDict->SetNewFor<CPDF_Reference>("Pages", this,
                                            pagesDict->GetObjNum());
     m_PageList.resize(3);
@@ -136,7 +136,7 @@
     pagesDict->SetNewFor<CPDF_Name>("Type", "Pages");
     pagesDict->SetNewFor<CPDF_Number>("Count", 3);
     m_PageList.resize(10);
-    m_pRootDict = NewIndirect<CPDF_Dictionary>();
+    m_pRootDict.Reset(NewIndirect<CPDF_Dictionary>());
     m_pRootDict->SetNewFor<CPDF_Reference>("Pages", this,
                                            pagesDict->GetObjNum());
   }
diff --git a/core/fpdfapi/render/cpdf_rendercontext.h b/core/fpdfapi/render/cpdf_rendercontext.h
index 606e5b9..5329338 100644
--- a/core/fpdfapi/render/cpdf_rendercontext.h
+++ b/core/fpdfapi/render/cpdf_rendercontext.h
@@ -66,7 +66,7 @@
 
  protected:
   UnownedPtr<CPDF_Document> const m_pDocument;
-  UnownedPtr<CPDF_Dictionary> m_pPageResources;
+  RetainPtr<CPDF_Dictionary> m_pPageResources;
   UnownedPtr<CPDF_PageRenderCache> m_pPageCache;
   std::vector<Layer> m_Layers;
 };
diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp
index b604186..4ed7b35 100644
--- a/core/fpdfapi/render/cpdf_renderstatus.cpp
+++ b/core/fpdfapi/render/cpdf_renderstatus.cpp
@@ -1008,7 +1008,7 @@
 void CPDF_RenderStatus::Initialize(const CPDF_RenderStatus* pParentState,
                                    const CPDF_GraphicStates* pInitialStates) {
   m_bPrint = m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
-  m_pPageResource = m_pContext->GetPageResources();
+  m_pPageResource.Reset(m_pContext->GetPageResources());
   if (pInitialStates && !m_pType3Char) {
     m_InitialStates.CopyStates(*pInitialStates);
     if (pParentState) {
diff --git a/core/fpdfapi/render/cpdf_renderstatus.h b/core/fpdfapi/render/cpdf_renderstatus.h
index 228a6a8..1839316 100644
--- a/core/fpdfapi/render/cpdf_renderstatus.h
+++ b/core/fpdfapi/render/cpdf_renderstatus.h
@@ -184,7 +184,7 @@
 
   CPDF_RenderOptions m_Options;
   UnownedPtr<const CPDF_Dictionary> m_pFormResource;
-  UnownedPtr<CPDF_Dictionary> m_pPageResource;
+  RetainPtr<CPDF_Dictionary> m_pPageResource;
   std::vector<CPDF_Type3Font*> m_Type3FontCache;
   UnownedPtr<CPDF_RenderContext> const m_pContext;
   bool m_bStopped = false;
diff --git a/core/fpdfdoc/cpdf_apsettings.h b/core/fpdfdoc/cpdf_apsettings.h
index a807c2b..d7a6eda 100644
--- a/core/fpdfdoc/cpdf_apsettings.h
+++ b/core/fpdfdoc/cpdf_apsettings.h
@@ -10,7 +10,7 @@
 #include "core/fpdfdoc/cpdf_iconfit.h"
 #include "core/fxcrt/fx_string.h"
 #include "core/fxcrt/fx_system.h"
-#include "core/fxcrt/unowned_ptr.h"
+#include "core/fxcrt/retain_ptr.h"
 #include "core/fxge/fx_dib.h"
 
 class CPDF_Dictionary;
@@ -69,7 +69,7 @@
   CPDF_Stream* GetIcon(const ByteString& csEntry) const;
 
  private:
-  UnownedPtr<CPDF_Dictionary> const m_pDict;
+  RetainPtr<CPDF_Dictionary> const m_pDict;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_APSETTINGS_H_
diff --git a/core/fpdfdoc/cpdf_filespec.h b/core/fpdfdoc/cpdf_filespec.h
index 7050f69..3b00cdf 100644
--- a/core/fpdfdoc/cpdf_filespec.h
+++ b/core/fpdfdoc/cpdf_filespec.h
@@ -41,7 +41,7 @@
 
  private:
   UnownedPtr<const CPDF_Object> const m_pObj;
-  UnownedPtr<CPDF_Object> const m_pWritableObj;
+  RetainPtr<CPDF_Object> const m_pWritableObj;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_FILESPEC_H_
diff --git a/core/fpdfdoc/cpdf_formcontrol.h b/core/fpdfdoc/cpdf_formcontrol.h
index d66ae0a..778e079 100644
--- a/core/fpdfdoc/cpdf_formcontrol.h
+++ b/core/fpdfdoc/cpdf_formcontrol.h
@@ -116,7 +116,7 @@
   CPDF_ApSettings GetMK() const;
 
   UnownedPtr<CPDF_FormField> const m_pField;
-  UnownedPtr<CPDF_Dictionary> const m_pWidgetDict;
+  RetainPtr<CPDF_Dictionary> const m_pWidgetDict;
   UnownedPtr<const CPDF_InteractiveForm> const m_pForm;
 };
 
diff --git a/core/fpdfdoc/cpdf_formfield.h b/core/fpdfdoc/cpdf_formfield.h
index 8ce888f..ab81651 100644
--- a/core/fpdfdoc/cpdf_formfield.h
+++ b/core/fpdfdoc/cpdf_formfield.h
@@ -200,7 +200,7 @@
   bool m_bNoExport = false;
 
   UnownedPtr<CPDF_InteractiveForm> const m_pForm;
-  UnownedPtr<CPDF_Dictionary> const m_pDict;
+  RetainPtr<CPDF_Dictionary> const m_pDict;
   float m_FontSize = 0;
   UnownedPtr<CPDF_Font> m_pFont;
 };
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index b5616ec..6a05b63 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -588,7 +588,7 @@
   if (!pRoot)
     return;
 
-  m_pFormDict = pRoot->GetDictFor("AcroForm");
+  m_pFormDict.Reset(pRoot->GetDictFor("AcroForm"));
   if (!m_pFormDict)
     return;
 
diff --git a/core/fpdfdoc/cpdf_interactiveform.h b/core/fpdfdoc/cpdf_interactiveform.h
index 49335e9..87a99bc 100644
--- a/core/fpdfdoc/cpdf_interactiveform.h
+++ b/core/fpdfdoc/cpdf_interactiveform.h
@@ -104,7 +104,7 @@
 
   ByteString m_bsEncoding;
   UnownedPtr<CPDF_Document> const m_pDocument;
-  UnownedPtr<CPDF_Dictionary> m_pFormDict;
+  RetainPtr<CPDF_Dictionary> m_pFormDict;
   std::unique_ptr<CFieldTree> m_pFieldTree;
   std::map<const CPDF_Dictionary*, std::unique_ptr<CPDF_FormControl>>
       m_ControlMap;
diff --git a/core/fpdfdoc/cpdf_link.h b/core/fpdfdoc/cpdf_link.h
index 18200cc..aaa6e8e 100644
--- a/core/fpdfdoc/cpdf_link.h
+++ b/core/fpdfdoc/cpdf_link.h
@@ -10,7 +10,7 @@
 #include "core/fpdfdoc/cpdf_action.h"
 #include "core/fpdfdoc/cpdf_dest.h"
 #include "core/fxcrt/fx_coordinates.h"
-#include "core/fxcrt/unowned_ptr.h"
+#include "core/fxcrt/retain_ptr.h"
 
 class CPDF_Dictionary;
 
@@ -27,7 +27,7 @@
   CPDF_Action GetAction();
 
  private:
-  UnownedPtr<CPDF_Dictionary> m_pDict;
+  RetainPtr<CPDF_Dictionary> m_pDict;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_LINK_H_
diff --git a/core/fpdfdoc/cpdf_nametree.cpp b/core/fpdfdoc/cpdf_nametree.cpp
index de04ed7..ff0fbbe 100644
--- a/core/fpdfdoc/cpdf_nametree.cpp
+++ b/core/fpdfdoc/cpdf_nametree.cpp
@@ -307,7 +307,7 @@
   if (!pNames)
     return;
 
-  m_pRoot = pNames->GetDictFor(category);
+  m_pRoot.Reset(pNames->GetDictFor(category));
 }
 
 CPDF_NameTree::~CPDF_NameTree() {}
diff --git a/core/fpdfdoc/cpdf_nametree.h b/core/fpdfdoc/cpdf_nametree.h
index ea8c817..e6a50e7 100644
--- a/core/fpdfdoc/cpdf_nametree.h
+++ b/core/fpdfdoc/cpdf_nametree.h
@@ -10,7 +10,7 @@
 #include <memory>
 
 #include "core/fxcrt/fx_string.h"
-#include "core/fxcrt/unowned_ptr.h"
+#include "core/fxcrt/retain_ptr.h"
 
 class CPDF_Array;
 class CPDF_Dictionary;
@@ -35,7 +35,7 @@
   CPDF_Dictionary* GetRoot() const { return m_pRoot.Get(); }
 
  private:
-  UnownedPtr<CPDF_Dictionary> m_pRoot;
+  RetainPtr<CPDF_Dictionary> m_pRoot;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_NAMETREE_H_
diff --git a/core/fpdfdoc/cpvt_fontmap.h b/core/fpdfdoc/cpvt_fontmap.h
index 6d8ca06..cce03bf 100644
--- a/core/fpdfdoc/cpvt_fontmap.h
+++ b/core/fpdfdoc/cpvt_fontmap.h
@@ -40,7 +40,7 @@
 
  private:
   UnownedPtr<CPDF_Document> const m_pDocument;
-  UnownedPtr<CPDF_Dictionary> const m_pResDict;
+  RetainPtr<CPDF_Dictionary> const m_pResDict;
   UnownedPtr<CPDF_Font> const m_pDefFont;
   UnownedPtr<CPDF_Font> m_pSysFont;
   const ByteString m_sDefFontAlias;
diff --git a/fpdfsdk/cpdf_annotcontext.h b/fpdfsdk/cpdf_annotcontext.h
index cab81e7..0561357 100644
--- a/fpdfsdk/cpdf_annotcontext.h
+++ b/fpdfsdk/cpdf_annotcontext.h
@@ -9,6 +9,7 @@
 
 #include <memory>
 
+#include "core/fxcrt/retain_ptr.h"
 #include "core/fxcrt/unowned_ptr.h"
 
 class CPDF_Dictionary;
@@ -33,7 +34,7 @@
 
  private:
   std::unique_ptr<CPDF_Form> m_pAnnotForm;
-  UnownedPtr<CPDF_Dictionary> const m_pAnnotDict;
+  RetainPtr<CPDF_Dictionary> const m_pAnnotDict;
   UnownedPtr<CPDF_Page> const m_pPage;
 };
 
diff --git a/fpdfsdk/formfiller/cba_fontmap.h b/fpdfsdk/formfiller/cba_fontmap.h
index 6ae7d7d..9bfced4 100644
--- a/fpdfsdk/formfiller/cba_fontmap.h
+++ b/fpdfsdk/formfiller/cba_fontmap.h
@@ -90,7 +90,7 @@
   std::vector<std::unique_ptr<Native>> m_NativeFont;
   UnownedPtr<CFX_SystemHandler> const m_pSystemHandler;
   UnownedPtr<CPDF_Document> const m_pDocument;
-  UnownedPtr<CPDF_Dictionary> const m_pAnnotDict;
+  RetainPtr<CPDF_Dictionary> const m_pAnnotDict;
   UnownedPtr<CPDF_Font> m_pDefaultFont;
   ByteString m_sDefaultFontName;
   ByteString m_sAPType = "N";
diff --git a/fpdfsdk/fpdf_catalog_unittest.cpp b/fpdfsdk/fpdf_catalog_unittest.cpp
index 5c129ca..180fb79 100644
--- a/fpdfsdk/fpdf_catalog_unittest.cpp
+++ b/fpdfsdk/fpdf_catalog_unittest.cpp
@@ -21,9 +21,7 @@
  public:
   CPDF_TestDocument() : CPDF_Document() {}
 
-  void SetRoot(CPDF_Dictionary* root) {
-    m_pRootDict = root;
-  }
+  void SetRoot(CPDF_Dictionary* root) { m_pRootDict.Reset(root); }
 };
 
 class PDFCatalogTest : public testing::Test {
diff --git a/fpdfsdk/fpdf_doc_unittest.cpp b/fpdfsdk/fpdf_doc_unittest.cpp
index 8faa2d1..b0337e8 100644
--- a/fpdfsdk/fpdf_doc_unittest.cpp
+++ b/fpdfsdk/fpdf_doc_unittest.cpp
@@ -28,7 +28,7 @@
  public:
   CPDF_TestDocument() : CPDF_Document() {}
 
-  void SetRoot(CPDF_Dictionary* root) { m_pRootDict = root; }
+  void SetRoot(CPDF_Dictionary* root) { m_pRootDict.Reset(root); }
   CPDF_IndirectObjectHolder* GetHolder() { return this; }
 };
 
@@ -43,7 +43,7 @@
     CPDF_ModuleMgr::Get()->Init();
     auto pTestDoc = pdfium::MakeUnique<CPDF_TestDocument>();
     m_pIndirectObjs = pTestDoc->GetHolder();
-    m_pRootObj = m_pIndirectObjs->NewIndirect<CPDF_Dictionary>();
+    m_pRootObj.Reset(m_pIndirectObjs->NewIndirect<CPDF_Dictionary>());
     pTestDoc->SetRoot(m_pRootObj.Get());
     m_pDoc.reset(FPDFDocumentFromCPDFDocument(pTestDoc.release()));
   }
@@ -68,7 +68,7 @@
  protected:
   ScopedFPDFDocument m_pDoc;
   UnownedPtr<CPDF_IndirectObjectHolder> m_pIndirectObjs;
-  UnownedPtr<CPDF_Dictionary> m_pRootObj;
+  RetainPtr<CPDF_Dictionary> m_pRootObj;
 };
 
 TEST_F(PDFDocTest, FindBookmark) {
diff --git a/fpdfsdk/pwl/cpwl_appstream.h b/fpdfsdk/pwl/cpwl_appstream.h
index 1c3afab..bf839cf 100644
--- a/fpdfsdk/pwl/cpwl_appstream.h
+++ b/fpdfsdk/pwl/cpwl_appstream.h
@@ -38,7 +38,7 @@
   ByteString GetBorderAppStream() const;
 
   UnownedPtr<CPDFSDK_Widget> const widget_;
-  UnownedPtr<CPDF_Dictionary> const dict_;
+  RetainPtr<CPDF_Dictionary> const dict_;
 };
 
 #endif  // FPDFSDK_PWL_CPWL_APPSTREAM_H_
diff --git a/fpdfsdk/pwl/cpwl_icon.h b/fpdfsdk/pwl/cpwl_icon.h
index b36c697..c1cc195 100644
--- a/fpdfsdk/pwl/cpwl_icon.h
+++ b/fpdfsdk/pwl/cpwl_icon.h
@@ -20,7 +20,7 @@
   ~CPWL_Icon() override;
 
   void SetIconFit(CPDF_IconFit* pIconFit) { m_pIconFit = pIconFit; }
-  void SetPDFStream(CPDF_Stream* pStream) { m_pPDFStream = pStream; }
+  void SetPDFStream(CPDF_Stream* pStream) { m_pPDFStream.Reset(pStream); }
 
   // horizontal scale, vertical scale
   std::pair<float, float> GetScale();
@@ -38,7 +38,7 @@
   // width, height
   std::pair<float, float> GetImageSize();
 
-  UnownedPtr<CPDF_Stream> m_pPDFStream;
+  RetainPtr<CPDF_Stream> m_pPDFStream;
   UnownedPtr<CPDF_IconFit> m_pIconFit;
 };