[lldb] Fix leak in test
authorVitaly Buka <vitalybuka@google.com>
Fri, 11 Jun 2021 01:43:35 +0000 (18:43 -0700)
committerVitaly Buka <vitalybuka@google.com>
Fri, 11 Jun 2021 07:20:35 +0000 (00:20 -0700)
Test leaks if we run
tools/lldb/unittests/Host/HostTests without --gtest_filter

Reviewed By: teemperor

Differential Revision: https://reviews.llvm.org/D104091

lldb/include/lldb/Host/linux/HostInfoLinux.h
lldb/source/Host/linux/HostInfoLinux.cpp
lldb/unittests/Host/HostInfoTest.cpp

index e808003..4a7bb6c 100644 (file)
@@ -28,6 +28,7 @@ private:
 
 public:
   static void Initialize(SharedLibraryDirectoryHelper *helper = nullptr);
+  static void Terminate();
 
   static llvm::VersionTuple GetOSVersion();
   static bool GetOSBuildString(std::string &s);
index 36ac0ec..6c37b97 100644 (file)
@@ -41,6 +41,13 @@ void HostInfoLinux::Initialize(SharedLibraryDirectoryHelper *helper) {
   g_fields = new HostInfoLinuxFields();
 }
 
+void HostInfoLinux::Terminate() {
+  assert(g_fields && "Missing call to Initialize?");
+  delete g_fields;
+  g_fields = nullptr;
+  HostInfoBase::Terminate();
+}
+
 llvm::VersionTuple HostInfoLinux::GetOSVersion() {
   assert(g_fields && "Missing call to Initialize?");
   llvm::call_once(g_fields->m_os_version_once_flag, []() {
index 96d47d7..0accdd8 100644 (file)
@@ -60,3 +60,16 @@ TEST_F(HostInfoTest, GetXcodeSDK) {
   EXPECT_TRUE(HostInfo::GetXcodeSDKPath(XcodeSDK("CeciNestPasUnOS.sdk")).empty());
 }
 #endif
+
+TEST(HostInfoTestInitialization, InitTwice) {
+  llvm::VersionTuple Version;
+  {
+    SubsystemRAII<FileSystem, HostInfo> subsystems;
+    Version = HostInfo::GetOSVersion();
+  }
+
+  {
+    SubsystemRAII<FileSystem, HostInfo> subsystems;
+    EXPECT_EQ(Version, HostInfo::GetOSVersion());
+  }
+}