Fix CPDFSDK_FormFillEnvironment destruction order

This was broken in 6c659ab2.  Also fix m_PageMap naming while
we're at it to match conventions.

TEST=covered by forthcoming bug 662698 CL.

Review-Url: https://codereview.chromium.org/2489663002
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
index 9852bdc..8ab3b46 100644
--- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp
+++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
@@ -41,12 +41,14 @@
 
 CPDFSDK_FormFillEnvironment::~CPDFSDK_FormFillEnvironment() {
   m_bBeingDestroyed = true;
-
   ClearAllFocusedAnnots();
 
-  // |m_pAnnotHandlerMgr| will try to access |m_pFormFiller|
-  // when it cleans up. So, we must make sure it is cleaned up before
-  // |m_pFormFiller|.
+  // |m_PageMap| will try to access |m_pInterForm| when it cleans itself up.
+  // Make sure it is deleted before |m_pInterForm|.
+  m_PageMap.clear();
+
+  // |m_pAnnotHandlerMgr| will try to access |m_pFormFiller| when it cleans
+  // itself up. Make sure it is deleted before |m_pFormFiller|.
   m_pAnnotHandlerMgr.reset();
 
   // Must destroy the |m_pFormFiller| before the environment (|this|)
@@ -563,7 +565,7 @@
 #endif  // PDF_ENABLE_XFA
 
 void CPDFSDK_FormFillEnvironment::ClearAllFocusedAnnots() {
-  for (auto& it : m_pageMap) {
+  for (auto& it : m_PageMap) {
     if (it.second->IsValidSDKAnnot(GetFocusAnnot()))
       KillFocusAnnot(0);
   }
@@ -572,15 +574,15 @@
 CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(
     UnderlyingPageType* pUnderlyingPage,
     bool renew) {
-  auto it = m_pageMap.find(pUnderlyingPage);
-  if (it != m_pageMap.end())
+  auto it = m_PageMap.find(pUnderlyingPage);
+  if (it != m_PageMap.end())
     return it->second.get();
 
   if (!renew)
     return nullptr;
 
   CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage);
-  m_pageMap[pUnderlyingPage].reset(pPageView);
+  m_PageMap[pUnderlyingPage].reset(pPageView);
   // Delay to load all the annotations, to avoid endless loop.
   pPageView->LoadFXAnnots();
   return pPageView;
@@ -598,8 +600,8 @@
   if (!pTempPage)
     return nullptr;
 
-  auto it = m_pageMap.find(pTempPage);
-  return it != m_pageMap.end() ? it->second.get() : nullptr;
+  auto it = m_PageMap.find(pTempPage);
+  return it != m_PageMap.end() ? it->second.get() : nullptr;
 }
 
 void CPDFSDK_FormFillEnvironment::ProcJavascriptFun() {
@@ -647,8 +649,8 @@
 
 void CPDFSDK_FormFillEnvironment::RemovePageView(
     UnderlyingPageType* pUnderlyingPage) {
-  auto it = m_pageMap.find(pUnderlyingPage);
-  if (it == m_pageMap.end())
+  auto it = m_PageMap.find(pUnderlyingPage);
+  if (it == m_PageMap.end())
     return;
 
   CPDFSDK_PageView* pPageView = it->second.get();
@@ -669,7 +671,7 @@
 
   // Remove the page from the map to make sure we don't accidentally attempt
   // to use the |pPageView| while we're cleaning it up.
-  m_pageMap.erase(it);
+  m_PageMap.erase(it);
 }
 
 UnderlyingPageType* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) {
@@ -684,7 +686,7 @@
 
 void CPDFSDK_FormFillEnvironment::UpdateAllViews(CPDFSDK_PageView* pSender,
                                                  CPDFSDK_Annot* pAnnot) {
-  for (const auto& it : m_pageMap) {
+  for (const auto& it : m_PageMap) {
     CPDFSDK_PageView* pPageView = it.second.get();
     if (pPageView != pSender)
       pPageView->UpdateView(pAnnot);
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.h b/fpdfsdk/cpdfsdk_formfillenvironment.h
index 2438019..b7afbbc 100644
--- a/fpdfsdk/cpdfsdk_formfillenvironment.h
+++ b/fpdfsdk/cpdfsdk_formfillenvironment.h
@@ -112,7 +112,7 @@
   CPDFXFA_Context* GetXFAContext() const { return m_pUnderlyingDoc; }
   void ResetXFADocument() { m_pUnderlyingDoc = nullptr; }
 
-  int GetPageViewCount() const { return m_pageMap.size(); }
+  int GetPageViewCount() const { return m_PageMap.size(); }
 
   void DisplayCaret(FPDF_PAGE page,
                     FPDF_BOOL bVisible,
@@ -213,7 +213,7 @@
   std::unique_ptr<CPDFSDK_ActionHandler> m_pActionHandler;
   std::unique_ptr<IJS_Runtime> m_pJSRuntime;
   FPDF_FORMFILLINFO* const m_pInfo;
-  std::map<UnderlyingPageType*, std::unique_ptr<CPDFSDK_PageView>> m_pageMap;
+  std::map<UnderlyingPageType*, std::unique_ptr<CPDFSDK_PageView>> m_PageMap;
   std::unique_ptr<CPDFSDK_InterForm> m_pInterForm;
   CPDFSDK_Annot::ObservedPtr m_pFocusAnnot;
   UnderlyingDocumentType* m_pUnderlyingDoc;