Bug 1403026 - Make IMEStateManager release all string buffer of its static members at XPCOM shutdown r?smaug draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Thu, 28 Sep 2017 17:31:29 +0900
changeset 671975 b5e864acb023d95d29fdfa1eabf596dd9697656d
parent 671801 76a26ef7c493311c170ae83eb0c1d6592a21396d
child 733673 c30b3a4c2f0b3df6ddc8d5df11ef365967611a18
push id82105
push usermasayuki@d-toybox.com
push dateThu, 28 Sep 2017 15:10:26 +0000
reviewerssmaug
bugs1403026
milestone58.0a1
Bug 1403026 - Make IMEStateManager release all string buffer of its static members at XPCOM shutdown r?smaug IMEStateManager has a static instance of InputContext which has some nsString instances. Then, their members will be released after XPCOM shutdown and that is detected as unexpected destruction. Therefore, IMEStateManager should release them by itself at XPCOM shutdown. Using nsTSubstring::SetCapacity(0) is the simplest way to release only the string buffers. MozReview-Commit-ID: LMrQxQF9xPn
dom/events/IMEStateManager.cpp
widget/IMEData.h
--- a/dom/events/IMEStateManager.cpp
+++ b/dom/events/IMEStateManager.cpp
@@ -188,16 +188,19 @@ IMEStateManager::Shutdown()
 {
   MOZ_LOG(sISMLog, LogLevel::Info,
     ("Shutdown(), sTextCompositions=0x%p, sTextCompositions->Length()=%zu",
      sTextCompositions, sTextCompositions ? sTextCompositions->Length() : 0));
 
   MOZ_ASSERT(!sTextCompositions || !sTextCompositions->Length());
   delete sTextCompositions;
   sTextCompositions = nullptr;
+  // All string instances in the global space need to be empty after XPCOM
+  // shutdown.
+  sActiveChildInputContext.ShutDown();
 }
 
 // static
 void
 IMEStateManager::OnTabParentDestroying(TabParent* aTabParent)
 {
   if (sFocusedIMETabParent == aTabParent) {
     NotifyIMEOfBlurForChildProcess();
--- a/widget/IMEData.h
+++ b/widget/IMEData.h
@@ -274,16 +274,28 @@ struct InputContext final
 {
   InputContext()
     : mOrigin(XRE_IsParentProcess() ? ORIGIN_MAIN : ORIGIN_CONTENT)
     , mMayBeIMEUnaware(false)
     , mInPrivateBrowsing(false)
   {
   }
 
+  // If InputContext instance is a static variable, any heap allocated stuff
+  // of its members need to be deleted at XPCOM shutdown.  Otherwise, it's
+  // detected as memory leak.
+  void ShutDown()
+  {
+    // The buffer for nsString will be released with a call of SetCapacity(0).
+    // Truncate() isn't enough because it just sets length to 0.
+    mHTMLInputType.SetCapacity(0);
+    mHTMLInputInputmode.SetCapacity(0);
+    mActionHint.SetCapacity(0);
+  }
+
   bool IsPasswordEditor() const
   {
     return mHTMLInputType.LowerCaseEqualsLiteral("password");
   }
 
   IMEState mIMEState;
 
   /* The type of the input if the input is a html input field */