From a00960288bedb3cd9495222e7917c9a1092ce4c9 Mon Sep 17 00:00:00 2001
From: Marc Chevrier <marc.chevrier@gmail.com>
Date: Thu, 2 Jan 2020 17:07:09 +0100
Subject: GlobalGenerator family: modernize memory management

---
 Source/CPack/cmCPackGenerator.cxx                 |   4 +-
 Source/CTest/cmCTestBuildAndTestHandler.cxx       |   8 +-
 Source/CTest/cmCTestBuildCommand.cxx              |  11 +-
 Source/CTest/cmCTestBuildCommand.h                |   2 +-
 Source/CTest/cmCTestConfigureCommand.cxx          |   7 +-
 Source/QtDialog/QCMake.cxx                        |   1 +
 Source/cmExternalMakefileProjectGenerator.h       |   9 +-
 Source/cmGlobalBorlandMakefileGenerator.h         |   6 +-
 Source/cmGlobalGenerator.cxx                      |   4 +-
 Source/cmGlobalGenerator.h                        |   2 +-
 Source/cmGlobalGeneratorFactory.h                 |  14 ++-
 Source/cmGlobalGhsMultiGenerator.h                |   5 +-
 Source/cmGlobalJOMMakefileGenerator.h             |   6 +-
 Source/cmGlobalMSYSMakefileGenerator.h            |   7 +-
 Source/cmGlobalMinGWMakefileGenerator.h           |   8 +-
 Source/cmGlobalNMakeMakefileGenerator.h           |   7 +-
 Source/cmGlobalNinjaGenerator.cxx                 |   9 +-
 Source/cmGlobalNinjaGenerator.h                   |  10 +-
 Source/cmGlobalUnixMakefileGenerator3.h           |   6 +-
 Source/cmGlobalVisualStudio10Generator.cxx        |  24 ++--
 Source/cmGlobalVisualStudio10Generator.h          |   2 +-
 Source/cmGlobalVisualStudio11Generator.cxx        |  32 +++--
 Source/cmGlobalVisualStudio11Generator.h          |   3 +-
 Source/cmGlobalVisualStudio12Generator.cxx        |  24 ++--
 Source/cmGlobalVisualStudio12Generator.h          |   3 +-
 Source/cmGlobalVisualStudio14Generator.cxx        |  24 ++--
 Source/cmGlobalVisualStudio14Generator.h          |   3 +-
 Source/cmGlobalVisualStudio9Generator.cxx         |  32 +++--
 Source/cmGlobalVisualStudio9Generator.h           |   4 +-
 Source/cmGlobalVisualStudioVersionedGenerator.cxx |  46 +++----
 Source/cmGlobalVisualStudioVersionedGenerator.h   |   5 +-
 Source/cmGlobalWatcomWMakeGenerator.h             |   6 +-
 Source/cmGlobalXCodeGenerator.cxx                 |  26 ++--
 Source/cmGlobalXCodeGenerator.h                   |   2 +-
 Source/cmMakefile.cxx                             |  10 +-
 Source/cmServerProtocol.cxx                       |   4 +-
 Source/cmake.cxx                                  | 145 ++++++++++------------
 Source/cmake.h                                    |  18 ++-
 Source/cmcmd.cxx                                  |   8 +-
 39 files changed, 293 insertions(+), 254 deletions(-)

diff --git a/Source/CPack/cmCPackGenerator.cxx b/Source/CPack/cmCPackGenerator.cxx
index 9530227..6003493 100644
--- a/Source/CPack/cmCPackGenerator.cxx
+++ b/Source/CPack/cmCPackGenerator.cxx
@@ -616,9 +616,9 @@ int cmCPackGenerator::InstallProjectViaInstallCMakeProjects(
         buildConfigs.emplace_back();
       }
 
-      std::unique_ptr<cmGlobalGenerator> globalGenerator(
+      std::unique_ptr<cmGlobalGenerator> globalGenerator =
         this->MakefileMap->GetCMakeInstance()->CreateGlobalGenerator(
-          cmakeGenerator));
+          cmakeGenerator);
       if (!globalGenerator) {
         cmCPackLogger(cmCPackLog::LOG_ERROR,
                       "Specified package generator not found. "
diff --git a/Source/CTest/cmCTestBuildAndTestHandler.cxx b/Source/CTest/cmCTestBuildAndTestHandler.cxx
index 2ad661c..5e29386 100644
--- a/Source/CTest/cmCTestBuildAndTestHandler.cxx
+++ b/Source/CTest/cmCTestBuildAndTestHandler.cxx
@@ -215,11 +215,11 @@ int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
 
   if (this->BuildNoCMake) {
     // Make the generator available for the Build call below.
-    cmGlobalGenerator* gen = cm.CreateGlobalGenerator(this->BuildGenerator);
-    cm.SetGlobalGenerator(gen);
+    cm.SetGlobalGenerator(cm.CreateGlobalGenerator(this->BuildGenerator));
     if (!this->BuildGeneratorPlatform.empty()) {
-      cmMakefile mf(gen, cm.GetCurrentSnapshot());
-      if (!gen->SetGeneratorPlatform(this->BuildGeneratorPlatform, &mf)) {
+      cmMakefile mf(cm.GetGlobalGenerator(), cm.GetCurrentSnapshot());
+      if (!cm.GetGlobalGenerator()->SetGeneratorPlatform(
+            this->BuildGeneratorPlatform, &mf)) {
         return 1;
       }
     }
diff --git a/Source/CTest/cmCTestBuildCommand.cxx b/Source/CTest/cmCTestBuildCommand.cxx
index 18df214..d1b7701 100644
--- a/Source/CTest/cmCTestBuildCommand.cxx
+++ b/Source/CTest/cmCTestBuildCommand.cxx
@@ -29,13 +29,7 @@ void cmCTestBuildCommand::BindArguments()
   this->Bind("PROJECT_NAME"_s, this->ProjectName);
 }
 
-cmCTestBuildCommand::~cmCTestBuildCommand()
-{
-  if (this->GlobalGenerator) {
-    delete this->GlobalGenerator;
-    this->GlobalGenerator = nullptr;
-  }
-}
+cmCTestBuildCommand::~cmCTestBuildCommand() = default;
 
 cmCTestGenericHandler* cmCTestBuildCommand::InitializeHandler()
 {
@@ -79,8 +73,7 @@ cmCTestGenericHandler* cmCTestBuildCommand::InitializeHandler()
       }
       if (this->GlobalGenerator) {
         if (this->GlobalGenerator->GetName() != cmakeGeneratorName) {
-          delete this->GlobalGenerator;
-          this->GlobalGenerator = nullptr;
+          this->GlobalGenerator.reset();
         }
       }
       if (!this->GlobalGenerator) {
diff --git a/Source/CTest/cmCTestBuildCommand.h b/Source/CTest/cmCTestBuildCommand.h
index da00a43..0f82817 100644
--- a/Source/CTest/cmCTestBuildCommand.h
+++ b/Source/CTest/cmCTestBuildCommand.h
@@ -48,7 +48,7 @@ public:
   bool InitialPass(std::vector<std::string> const& args,
                    cmExecutionStatus& status) override;
 
-  cmGlobalGenerator* GlobalGenerator = nullptr;
+  std::unique_ptr<cmGlobalGenerator> GlobalGenerator;
 
 protected:
   cmCTestBuildHandler* Handler;
diff --git a/Source/CTest/cmCTestConfigureCommand.cxx b/Source/CTest/cmCTestConfigureCommand.cxx
index f2f42b4..3854710 100644
--- a/Source/CTest/cmCTestConfigureCommand.cxx
+++ b/Source/CTest/cmCTestConfigureCommand.cxx
@@ -69,12 +69,11 @@ cmCTestGenericHandler* cmCTestConfigureCommand::InitializeHandler()
       bool multiConfig = false;
       bool cmakeBuildTypeInOptions = false;
 
-      cmGlobalGenerator* gg =
-        this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
-          cmakeGeneratorName);
+      auto gg = this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
+        cmakeGeneratorName);
       if (gg) {
         multiConfig = gg->IsMultiConfig();
-        delete gg;
+        gg.reset();
       }
 
       std::string cmakeConfigureCommand =
diff --git a/Source/QtDialog/QCMake.cxx b/Source/QtDialog/QCMake.cxx
index b608fcb..3b5dc04 100644
--- a/Source/QtDialog/QCMake.cxx
+++ b/Source/QtDialog/QCMake.cxx
@@ -6,6 +6,7 @@
 #include <QDir>
 
 #include "cmExternalMakefileProjectGenerator.h"
+#include "cmGlobalGenerator.h"
 #include "cmState.h"
 #include "cmStringAlgorithms.h"
 #include "cmSystemTools.h"
diff --git a/Source/cmExternalMakefileProjectGenerator.h b/Source/cmExternalMakefileProjectGenerator.h
index a472a06..2b8d505 100644
--- a/Source/cmExternalMakefileProjectGenerator.h
+++ b/Source/cmExternalMakefileProjectGenerator.h
@@ -5,6 +5,7 @@
 
 #include "cmConfigure.h" // IWYU pragma: keep
 
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -78,7 +79,7 @@ public:
   std::vector<std::string> GetSupportedGlobalGenerators() const;
   std::vector<std::string> Aliases;
 
-  virtual cmExternalMakefileProjectGenerator*
+  virtual std::unique_ptr<cmExternalMakefileProjectGenerator>
   CreateExternalMakefileProjectGenerator() const = 0;
 
   void AddSupportedGlobalGenerator(const std::string& base);
@@ -100,10 +101,10 @@ public:
   {
   }
 
-  cmExternalMakefileProjectGenerator* CreateExternalMakefileProjectGenerator()
-    const override
+  std::unique_ptr<cmExternalMakefileProjectGenerator>
+  CreateExternalMakefileProjectGenerator() const override
   {
-    T* p = new T;
+    std::unique_ptr<cmExternalMakefileProjectGenerator> p(new T);
     p->SetName(GetName());
     return p;
   }
diff --git a/Source/cmGlobalBorlandMakefileGenerator.h b/Source/cmGlobalBorlandMakefileGenerator.h
index 291220c..9af0eac 100644
--- a/Source/cmGlobalBorlandMakefileGenerator.h
+++ b/Source/cmGlobalBorlandMakefileGenerator.h
@@ -17,10 +17,10 @@ class cmGlobalBorlandMakefileGenerator : public cmGlobalUnixMakefileGenerator3
 {
 public:
   cmGlobalBorlandMakefileGenerator(cmake* cm);
-  static cmGlobalGeneratorFactory* NewFactory()
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory()
   {
-    return new cmGlobalGeneratorSimpleFactory<
-      cmGlobalBorlandMakefileGenerator>();
+    return std::unique_ptr<cmGlobalGeneratorFactory>(
+      new cmGlobalGeneratorSimpleFactory<cmGlobalBorlandMakefileGenerator>());
   }
 
   //! Get the name for the generator.
diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx
index d6d8c7b..4cbcda0 100644
--- a/Source/cmGlobalGenerator.cxx
+++ b/Source/cmGlobalGenerator.cxx
@@ -2743,9 +2743,9 @@ bool cmGlobalGenerator::IsReservedTarget(std::string const& name)
 }
 
 void cmGlobalGenerator::SetExternalMakefileProjectGenerator(
-  cmExternalMakefileProjectGenerator* extraGenerator)
+  std::unique_ptr<cmExternalMakefileProjectGenerator> extraGenerator)
 {
-  this->ExtraGenerator.reset(extraGenerator);
+  this->ExtraGenerator = std::move(extraGenerator);
   if (this->ExtraGenerator) {
     this->ExtraGenerator->SetGlobalGenerator(this);
   }
diff --git a/Source/cmGlobalGenerator.h b/Source/cmGlobalGenerator.h
index 3d11c61..b427992 100644
--- a/Source/cmGlobalGenerator.h
+++ b/Source/cmGlobalGenerator.h
@@ -272,7 +272,7 @@ public:
 
   //! Set an generator for an "external makefile based project"
   void SetExternalMakefileProjectGenerator(
-    cmExternalMakefileProjectGenerator* extraGenerator);
+    std::unique_ptr<cmExternalMakefileProjectGenerator> extraGenerator);
 
   std::string GetExtraGeneratorName() const;
 
diff --git a/Source/cmGlobalGeneratorFactory.h b/Source/cmGlobalGeneratorFactory.h
index bb5f74c..3709365 100644
--- a/Source/cmGlobalGeneratorFactory.h
+++ b/Source/cmGlobalGeneratorFactory.h
@@ -8,6 +8,8 @@
 #include <string>
 #include <vector>
 
+#include <cm/memory>
+
 class cmGlobalGenerator;
 class cmake;
 struct cmDocumentationEntry;
@@ -23,8 +25,8 @@ public:
   virtual ~cmGlobalGeneratorFactory() = default;
 
   /** Create a GlobalGenerator */
-  virtual cmGlobalGenerator* CreateGlobalGenerator(const std::string& n,
-                                                   cmake* cm) const = 0;
+  virtual std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
+    const std::string& n, cmake* cm) const = 0;
 
   /** Get the documentation entry for this factory */
   virtual void GetDocumentation(cmDocumentationEntry& entry) const = 0;
@@ -51,13 +53,13 @@ class cmGlobalGeneratorSimpleFactory : public cmGlobalGeneratorFactory
 {
 public:
   /** Create a GlobalGenerator */
-  cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
-                                           cmake* cm) const override
+  std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
+    const std::string& name, cmake* cm) const override
   {
     if (name != T::GetActualName()) {
-      return nullptr;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
-    return new T(cm);
+    return std::unique_ptr<cmGlobalGenerator>(cm::make_unique<T>(cm));
   }
 
   /** Get the documentation entry for this factory */
diff --git a/Source/cmGlobalGhsMultiGenerator.h b/Source/cmGlobalGhsMultiGenerator.h
index 989b12c..b82e9f5 100644
--- a/Source/cmGlobalGhsMultiGenerator.h
+++ b/Source/cmGlobalGhsMultiGenerator.h
@@ -29,9 +29,10 @@ public:
   cmGlobalGhsMultiGenerator(cmake* cm);
   ~cmGlobalGhsMultiGenerator() override;
 
-  static cmGlobalGeneratorFactory* NewFactory()
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory()
   {
-    return new cmGlobalGeneratorSimpleFactory<cmGlobalGhsMultiGenerator>();
+    return std::unique_ptr<cmGlobalGeneratorFactory>(
+      new cmGlobalGeneratorSimpleFactory<cmGlobalGhsMultiGenerator>());
   }
 
   //! create the correct local generator
diff --git a/Source/cmGlobalJOMMakefileGenerator.h b/Source/cmGlobalJOMMakefileGenerator.h
index fc39ddf..9f1ec8b 100644
--- a/Source/cmGlobalJOMMakefileGenerator.h
+++ b/Source/cmGlobalJOMMakefileGenerator.h
@@ -4,6 +4,7 @@
 #define cmGlobalJOMMakefileGenerator_h
 
 #include <iosfwd>
+#include <memory>
 
 #include "cmGlobalUnixMakefileGenerator3.h"
 
@@ -16,9 +17,10 @@ class cmGlobalJOMMakefileGenerator : public cmGlobalUnixMakefileGenerator3
 {
 public:
   cmGlobalJOMMakefileGenerator(cmake* cm);
-  static cmGlobalGeneratorFactory* NewFactory()
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory()
   {
-    return new cmGlobalGeneratorSimpleFactory<cmGlobalJOMMakefileGenerator>();
+    return std::unique_ptr<cmGlobalGeneratorFactory>(
+      new cmGlobalGeneratorSimpleFactory<cmGlobalJOMMakefileGenerator>());
   }
   //! Get the name for the generator.
   std::string GetName() const override
diff --git a/Source/cmGlobalMSYSMakefileGenerator.h b/Source/cmGlobalMSYSMakefileGenerator.h
index d6e4847..b2de4ff 100644
--- a/Source/cmGlobalMSYSMakefileGenerator.h
+++ b/Source/cmGlobalMSYSMakefileGenerator.h
@@ -3,6 +3,8 @@
 #ifndef cmGlobalMSYSMakefileGenerator_h
 #define cmGlobalMSYSMakefileGenerator_h
 
+#include <memory>
+
 #include "cmGlobalUnixMakefileGenerator3.h"
 
 /** \class cmGlobalMSYSMakefileGenerator
@@ -14,9 +16,10 @@ class cmGlobalMSYSMakefileGenerator : public cmGlobalUnixMakefileGenerator3
 {
 public:
   cmGlobalMSYSMakefileGenerator(cmake* cm);
-  static cmGlobalGeneratorFactory* NewFactory()
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory()
   {
-    return new cmGlobalGeneratorSimpleFactory<cmGlobalMSYSMakefileGenerator>();
+    return std::unique_ptr<cmGlobalGeneratorFactory>(
+      new cmGlobalGeneratorSimpleFactory<cmGlobalMSYSMakefileGenerator>());
   }
 
   //! Get the name for the generator.
diff --git a/Source/cmGlobalMinGWMakefileGenerator.h b/Source/cmGlobalMinGWMakefileGenerator.h
index 15297e3..a9f92a1 100644
--- a/Source/cmGlobalMinGWMakefileGenerator.h
+++ b/Source/cmGlobalMinGWMakefileGenerator.h
@@ -3,6 +3,8 @@
 #ifndef cmGlobalMinGWMakefileGenerator_h
 #define cmGlobalMinGWMakefileGenerator_h
 
+#include <memory>
+
 #include "cmGlobalUnixMakefileGenerator3.h"
 
 /** \class cmGlobalMinGWMakefileGenerator
@@ -14,10 +16,10 @@ class cmGlobalMinGWMakefileGenerator : public cmGlobalUnixMakefileGenerator3
 {
 public:
   cmGlobalMinGWMakefileGenerator(cmake* cm);
-  static cmGlobalGeneratorFactory* NewFactory()
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory()
   {
-    return new cmGlobalGeneratorSimpleFactory<
-      cmGlobalMinGWMakefileGenerator>();
+    return std::unique_ptr<cmGlobalGeneratorFactory>(
+      new cmGlobalGeneratorSimpleFactory<cmGlobalMinGWMakefileGenerator>());
   }
   //! Get the name for the generator.
   virtual std::string GetName() const
diff --git a/Source/cmGlobalNMakeMakefileGenerator.h b/Source/cmGlobalNMakeMakefileGenerator.h
index 4586b77..fdf6006 100644
--- a/Source/cmGlobalNMakeMakefileGenerator.h
+++ b/Source/cmGlobalNMakeMakefileGenerator.h
@@ -4,6 +4,7 @@
 #define cmGlobalNMakeMakefileGenerator_h
 
 #include <iosfwd>
+#include <memory>
 
 #include "cmGlobalUnixMakefileGenerator3.h"
 
@@ -16,10 +17,10 @@ class cmGlobalNMakeMakefileGenerator : public cmGlobalUnixMakefileGenerator3
 {
 public:
   cmGlobalNMakeMakefileGenerator(cmake* cm);
-  static cmGlobalGeneratorFactory* NewFactory()
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory()
   {
-    return new cmGlobalGeneratorSimpleFactory<
-      cmGlobalNMakeMakefileGenerator>();
+    return std::unique_ptr<cmGlobalGeneratorFactory>(
+      new cmGlobalGeneratorSimpleFactory<cmGlobalNMakeMakefileGenerator>());
   }
   //! Get the name for the generator.
   std::string GetName() const override
diff --git a/Source/cmGlobalNinjaGenerator.cxx b/Source/cmGlobalNinjaGenerator.cxx
index f5f5af8..4d43566 100644
--- a/Source/cmGlobalNinjaGenerator.cxx
+++ b/Source/cmGlobalNinjaGenerator.cxx
@@ -2223,12 +2223,11 @@ int cmcmd_cmake_ninja_dyndep(std::vector<std::string>::const_iterator argBeg,
   cmake cm(cmake::RoleInternal, cmState::Unknown);
   cm.SetHomeDirectory(dir_top_src);
   cm.SetHomeOutputDirectory(dir_top_bld);
-  std::unique_ptr<cmGlobalNinjaGenerator> ggd(
-    static_cast<cmGlobalNinjaGenerator*>(cm.CreateGlobalGenerator("Ninja")));
+  auto ggd = cm.CreateGlobalGenerator("Ninja");
   if (!ggd ||
-      !ggd->WriteDyndepFile(dir_top_src, dir_top_bld, dir_cur_src, dir_cur_bld,
-                            arg_dd, arg_ddis, module_dir, linked_target_dirs,
-                            arg_lang)) {
+      !cm::static_reference_cast<cmGlobalNinjaGenerator>(ggd).WriteDyndepFile(
+        dir_top_src, dir_top_bld, dir_cur_src, dir_cur_bld, arg_dd, arg_ddis,
+        module_dir, linked_target_dirs, arg_lang)) {
     return 1;
   }
   return 0;
diff --git a/Source/cmGlobalNinjaGenerator.h b/Source/cmGlobalNinjaGenerator.h
index 20e2c8e..2115042 100644
--- a/Source/cmGlobalNinjaGenerator.h
+++ b/Source/cmGlobalNinjaGenerator.h
@@ -153,9 +153,10 @@ public:
 public:
   cmGlobalNinjaGenerator(cmake* cm);
 
-  static cmGlobalGeneratorFactory* NewFactory()
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory()
   {
-    return new cmGlobalGeneratorSimpleFactory<cmGlobalNinjaGenerator>();
+    return std::unique_ptr<cmGlobalGeneratorFactory>(
+      new cmGlobalGeneratorSimpleFactory<cmGlobalNinjaGenerator>());
   }
 
   std::unique_ptr<cmLocalGenerator> CreateLocalGenerator(
@@ -524,9 +525,10 @@ public:
 
   cmGlobalNinjaMultiGenerator(cmake* cm);
   bool IsMultiConfig() const override { return true; }
-  static cmGlobalGeneratorFactory* NewFactory()
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory()
   {
-    return new cmGlobalGeneratorSimpleFactory<cmGlobalNinjaMultiGenerator>();
+    return std::unique_ptr<cmGlobalGeneratorFactory>(
+      new cmGlobalGeneratorSimpleFactory<cmGlobalNinjaMultiGenerator>());
   }
 
   static void GetDocumentation(cmDocumentationEntry& entry);
diff --git a/Source/cmGlobalUnixMakefileGenerator3.h b/Source/cmGlobalUnixMakefileGenerator3.h
index 5608baf..340a7ef 100644
--- a/Source/cmGlobalUnixMakefileGenerator3.h
+++ b/Source/cmGlobalUnixMakefileGenerator3.h
@@ -62,10 +62,10 @@ class cmGlobalUnixMakefileGenerator3 : public cmGlobalCommonGenerator
 {
 public:
   cmGlobalUnixMakefileGenerator3(cmake* cm);
-  static cmGlobalGeneratorFactory* NewFactory()
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory()
   {
-    return new cmGlobalGeneratorSimpleFactory<
-      cmGlobalUnixMakefileGenerator3>();
+    return std::unique_ptr<cmGlobalGeneratorFactory>(
+      new cmGlobalGeneratorSimpleFactory<cmGlobalUnixMakefileGenerator3>());
   }
 
   //! Get the name for the generator.
diff --git a/Source/cmGlobalVisualStudio10Generator.cxx b/Source/cmGlobalVisualStudio10Generator.cxx
index bdf4cf2..a720cc3 100644
--- a/Source/cmGlobalVisualStudio10Generator.cxx
+++ b/Source/cmGlobalVisualStudio10Generator.cxx
@@ -57,27 +57,30 @@ class cmGlobalVisualStudio10Generator::Factory
   : public cmGlobalGeneratorFactory
 {
 public:
-  cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
-                                           cmake* cm) const override
+  std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
+    const std::string& name, cmake* cm) const override
   {
     std::string genName;
     const char* p = cmVS10GenName(name, genName);
     if (!p) {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
     if (!*p) {
-      return new cmGlobalVisualStudio10Generator(cm, genName, "");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio10Generator(cm, genName, ""));
     }
     if (*p++ != ' ') {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
     if (strcmp(p, "Win64") == 0) {
-      return new cmGlobalVisualStudio10Generator(cm, genName, "x64");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio10Generator(cm, genName, "x64"));
     }
     if (strcmp(p, "IA64") == 0) {
-      return new cmGlobalVisualStudio10Generator(cm, genName, "Itanium");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio10Generator(cm, genName, "Itanium"));
     }
-    return 0;
+    return std::unique_ptr<cmGlobalGenerator>();
   }
 
   void GetDocumentation(cmDocumentationEntry& entry) const override
@@ -117,9 +120,10 @@ public:
   std::string GetDefaultPlatformName() const override { return "Win32"; }
 };
 
-cmGlobalGeneratorFactory* cmGlobalVisualStudio10Generator::NewFactory()
+std::unique_ptr<cmGlobalGeneratorFactory>
+cmGlobalVisualStudio10Generator::NewFactory()
 {
-  return new Factory;
+  return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory);
 }
 
 cmGlobalVisualStudio10Generator::cmGlobalVisualStudio10Generator(
diff --git a/Source/cmGlobalVisualStudio10Generator.h b/Source/cmGlobalVisualStudio10Generator.h
index c991c76..2120785 100644
--- a/Source/cmGlobalVisualStudio10Generator.h
+++ b/Source/cmGlobalVisualStudio10Generator.h
@@ -16,7 +16,7 @@
 class cmGlobalVisualStudio10Generator : public cmGlobalVisualStudio8Generator
 {
 public:
-  static cmGlobalGeneratorFactory* NewFactory();
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory();
 
   bool MatchesGeneratorName(const std::string& name) const override;
 
diff --git a/Source/cmGlobalVisualStudio11Generator.cxx b/Source/cmGlobalVisualStudio11Generator.cxx
index 4b74ef1..a385375 100644
--- a/Source/cmGlobalVisualStudio11Generator.cxx
+++ b/Source/cmGlobalVisualStudio11Generator.cxx
@@ -2,6 +2,8 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "cmGlobalVisualStudio11Generator.h"
 
+#include <utility>
+
 #include "cmAlgorithms.h"
 #include "cmDocumentationEntry.h"
 #include "cmLocalVisualStudio10Generator.h"
@@ -28,38 +30,41 @@ class cmGlobalVisualStudio11Generator::Factory
   : public cmGlobalGeneratorFactory
 {
 public:
-  cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
-                                           cmake* cm) const override
+  std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
+    const std::string& name, cmake* cm) const override
   {
     std::string genName;
     const char* p = cmVS11GenName(name, genName);
     if (!p) {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
     if (!*p) {
-      return new cmGlobalVisualStudio11Generator(cm, genName, "");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio11Generator(cm, genName, ""));
     }
     if (*p++ != ' ') {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
     if (strcmp(p, "Win64") == 0) {
-      return new cmGlobalVisualStudio11Generator(cm, genName, "x64");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio11Generator(cm, genName, "x64"));
     }
     if (strcmp(p, "ARM") == 0) {
-      return new cmGlobalVisualStudio11Generator(cm, genName, "ARM");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio11Generator(cm, genName, "ARM"));
     }
 
     std::set<std::string> installedSDKs =
       cmGlobalVisualStudio11Generator::GetInstalledWindowsCESDKs();
 
     if (installedSDKs.find(p) == installedSDKs.end()) {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
 
-    cmGlobalVisualStudio11Generator* ret =
-      new cmGlobalVisualStudio11Generator(cm, name, p);
+    auto ret = std::unique_ptr<cmGlobalVisualStudio11Generator>(
+      new cmGlobalVisualStudio11Generator(cm, name, p));
     ret->WindowsCEVersion = "8.00";
-    return ret;
+    return std::unique_ptr<cmGlobalGenerator>(std::move(ret));
   }
 
   void GetDocumentation(cmDocumentationEntry& entry) const override
@@ -113,9 +118,10 @@ public:
   std::string GetDefaultPlatformName() const override { return "Win32"; }
 };
 
-cmGlobalGeneratorFactory* cmGlobalVisualStudio11Generator::NewFactory()
+std::unique_ptr<cmGlobalGeneratorFactory>
+cmGlobalVisualStudio11Generator::NewFactory()
 {
-  return new Factory;
+  return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory);
 }
 
 cmGlobalVisualStudio11Generator::cmGlobalVisualStudio11Generator(
diff --git a/Source/cmGlobalVisualStudio11Generator.h b/Source/cmGlobalVisualStudio11Generator.h
index f8cce18..5f1ff73 100644
--- a/Source/cmGlobalVisualStudio11Generator.h
+++ b/Source/cmGlobalVisualStudio11Generator.h
@@ -6,6 +6,7 @@
 #include "cmConfigure.h" // IWYU pragma: keep
 
 #include <iosfwd>
+#include <memory>
 #include <set>
 #include <string>
 
@@ -20,7 +21,7 @@ class cmake;
 class cmGlobalVisualStudio11Generator : public cmGlobalVisualStudio10Generator
 {
 public:
-  static cmGlobalGeneratorFactory* NewFactory();
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory();
 
   bool MatchesGeneratorName(const std::string& name) const override;
 
diff --git a/Source/cmGlobalVisualStudio12Generator.cxx b/Source/cmGlobalVisualStudio12Generator.cxx
index d9702c9..5a27994 100644
--- a/Source/cmGlobalVisualStudio12Generator.cxx
+++ b/Source/cmGlobalVisualStudio12Generator.cxx
@@ -28,27 +28,30 @@ class cmGlobalVisualStudio12Generator::Factory
   : public cmGlobalGeneratorFactory
 {
 public:
-  cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
-                                           cmake* cm) const override
+  std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
+    const std::string& name, cmake* cm) const override
   {
     std::string genName;
     const char* p = cmVS12GenName(name, genName);
     if (!p) {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
     if (!*p) {
-      return new cmGlobalVisualStudio12Generator(cm, genName, "");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio12Generator(cm, genName, ""));
     }
     if (*p++ != ' ') {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
     if (strcmp(p, "Win64") == 0) {
-      return new cmGlobalVisualStudio12Generator(cm, genName, "x64");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio12Generator(cm, genName, "x64"));
     }
     if (strcmp(p, "ARM") == 0) {
-      return new cmGlobalVisualStudio12Generator(cm, genName, "ARM");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio12Generator(cm, genName, "ARM"));
     }
-    return 0;
+    return std::unique_ptr<cmGlobalGenerator>();
   }
 
   void GetDocumentation(cmDocumentationEntry& entry) const override
@@ -88,9 +91,10 @@ public:
   std::string GetDefaultPlatformName() const override { return "Win32"; }
 };
 
-cmGlobalGeneratorFactory* cmGlobalVisualStudio12Generator::NewFactory()
+std::unique_ptr<cmGlobalGeneratorFactory>
+cmGlobalVisualStudio12Generator::NewFactory()
 {
-  return new Factory;
+  return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory);
 }
 
 cmGlobalVisualStudio12Generator::cmGlobalVisualStudio12Generator(
diff --git a/Source/cmGlobalVisualStudio12Generator.h b/Source/cmGlobalVisualStudio12Generator.h
index 53b7091..bdd40ff 100644
--- a/Source/cmGlobalVisualStudio12Generator.h
+++ b/Source/cmGlobalVisualStudio12Generator.h
@@ -6,6 +6,7 @@
 #include "cmConfigure.h" // IWYU pragma: keep
 
 #include <iosfwd>
+#include <memory>
 #include <string>
 
 #include "cmGlobalVisualStudio11Generator.h"
@@ -18,7 +19,7 @@ class cmake;
 class cmGlobalVisualStudio12Generator : public cmGlobalVisualStudio11Generator
 {
 public:
-  static cmGlobalGeneratorFactory* NewFactory();
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory();
 
   bool MatchesGeneratorName(const std::string& name) const override;
 
diff --git a/Source/cmGlobalVisualStudio14Generator.cxx b/Source/cmGlobalVisualStudio14Generator.cxx
index cd48474..779753e 100644
--- a/Source/cmGlobalVisualStudio14Generator.cxx
+++ b/Source/cmGlobalVisualStudio14Generator.cxx
@@ -28,27 +28,30 @@ class cmGlobalVisualStudio14Generator::Factory
   : public cmGlobalGeneratorFactory
 {
 public:
-  cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
-                                           cmake* cm) const override
+  std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
+    const std::string& name, cmake* cm) const override
   {
     std::string genName;
     const char* p = cmVS14GenName(name, genName);
     if (!p) {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
     if (!*p) {
-      return new cmGlobalVisualStudio14Generator(cm, genName, "");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio14Generator(cm, genName, ""));
     }
     if (*p++ != ' ') {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
     if (strcmp(p, "Win64") == 0) {
-      return new cmGlobalVisualStudio14Generator(cm, genName, "x64");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio14Generator(cm, genName, "x64"));
     }
     if (strcmp(p, "ARM") == 0) {
-      return new cmGlobalVisualStudio14Generator(cm, genName, "ARM");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio14Generator(cm, genName, "ARM"));
     }
-    return 0;
+    return std::unique_ptr<cmGlobalGenerator>();
   }
 
   void GetDocumentation(cmDocumentationEntry& entry) const override
@@ -88,9 +91,10 @@ public:
   std::string GetDefaultPlatformName() const override { return "Win32"; }
 };
 
-cmGlobalGeneratorFactory* cmGlobalVisualStudio14Generator::NewFactory()
+std::unique_ptr<cmGlobalGeneratorFactory>
+cmGlobalVisualStudio14Generator::NewFactory()
 {
-  return new Factory;
+  return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory);
 }
 
 cmGlobalVisualStudio14Generator::cmGlobalVisualStudio14Generator(
diff --git a/Source/cmGlobalVisualStudio14Generator.h b/Source/cmGlobalVisualStudio14Generator.h
index 6e12d3e..ccc2917 100644
--- a/Source/cmGlobalVisualStudio14Generator.h
+++ b/Source/cmGlobalVisualStudio14Generator.h
@@ -6,6 +6,7 @@
 #include "cmConfigure.h" // IWYU pragma: keep
 
 #include <iosfwd>
+#include <memory>
 #include <string>
 
 #include "cmGlobalVisualStudio12Generator.h"
@@ -18,7 +19,7 @@ class cmake;
 class cmGlobalVisualStudio14Generator : public cmGlobalVisualStudio12Generator
 {
 public:
-  static cmGlobalGeneratorFactory* NewFactory();
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory();
 
   bool MatchesGeneratorName(const std::string& name) const override;
 
diff --git a/Source/cmGlobalVisualStudio9Generator.cxx b/Source/cmGlobalVisualStudio9Generator.cxx
index 6e61d26..9f73c15 100644
--- a/Source/cmGlobalVisualStudio9Generator.cxx
+++ b/Source/cmGlobalVisualStudio9Generator.cxx
@@ -2,6 +2,8 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "cmGlobalVisualStudio9Generator.h"
 
+#include <utility>
+
 #include "cmDocumentationEntry.h"
 #include "cmLocalVisualStudio7Generator.h"
 #include "cmMakefile.h"
@@ -13,43 +15,46 @@ static const char vs9generatorName[] = "Visual Studio 9 2008";
 class cmGlobalVisualStudio9Generator::Factory : public cmGlobalGeneratorFactory
 {
 public:
-  cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
-                                           cmake* cm) const override
+  std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
+    const std::string& name, cmake* cm) const override
   {
     if (strncmp(name.c_str(), vs9generatorName,
                 sizeof(vs9generatorName) - 1) != 0) {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
 
     const char* p = name.c_str() + sizeof(vs9generatorName) - 1;
     if (p[0] == '\0') {
-      return new cmGlobalVisualStudio9Generator(cm, name, "");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio9Generator(cm, name, ""));
     }
 
     if (p[0] != ' ') {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
 
     ++p;
 
     if (!strcmp(p, "IA64")) {
-      return new cmGlobalVisualStudio9Generator(cm, name, "Itanium");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio9Generator(cm, name, "Itanium"));
     }
 
     if (!strcmp(p, "Win64")) {
-      return new cmGlobalVisualStudio9Generator(cm, name, "x64");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudio9Generator(cm, name, "x64"));
     }
 
     cmVisualStudioWCEPlatformParser parser(p);
     parser.ParseVersion("9.0");
     if (!parser.Found()) {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
 
-    cmGlobalVisualStudio9Generator* ret =
-      new cmGlobalVisualStudio9Generator(cm, name, p);
+    auto ret = std::unique_ptr<cmGlobalVisualStudio9Generator>(
+      new cmGlobalVisualStudio9Generator(cm, name, p));
     ret->WindowsCEVersion = parser.GetOSVersion();
-    return ret;
+    return std::unique_ptr<cmGlobalGenerator>(std::move(ret));
   }
 
   void GetDocumentation(cmDocumentationEntry& entry) const override
@@ -103,9 +108,10 @@ public:
   std::string GetDefaultPlatformName() const override { return "Win32"; }
 };
 
-cmGlobalGeneratorFactory* cmGlobalVisualStudio9Generator::NewFactory()
+std::unique_ptr<cmGlobalGeneratorFactory>
+cmGlobalVisualStudio9Generator::NewFactory()
 {
-  return new Factory;
+  return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory);
 }
 
 cmGlobalVisualStudio9Generator::cmGlobalVisualStudio9Generator(
diff --git a/Source/cmGlobalVisualStudio9Generator.h b/Source/cmGlobalVisualStudio9Generator.h
index 7bebfd6..53318a6 100644
--- a/Source/cmGlobalVisualStudio9Generator.h
+++ b/Source/cmGlobalVisualStudio9Generator.h
@@ -3,6 +3,8 @@
 #ifndef cmGlobalVisualStudio9Generator_h
 #define cmGlobalVisualStudio9Generator_h
 
+#include <memory>
+
 #include "cmGlobalVisualStudio8Generator.h"
 
 /** \class cmGlobalVisualStudio9Generator
@@ -13,7 +15,7 @@
 class cmGlobalVisualStudio9Generator : public cmGlobalVisualStudio8Generator
 {
 public:
-  static cmGlobalGeneratorFactory* NewFactory();
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory();
 
   /**
    * Where does this version of Visual Studio look for macros for the
diff --git a/Source/cmGlobalVisualStudioVersionedGenerator.cxx b/Source/cmGlobalVisualStudioVersionedGenerator.cxx
index a371633..13ae32a 100644
--- a/Source/cmGlobalVisualStudioVersionedGenerator.cxx
+++ b/Source/cmGlobalVisualStudioVersionedGenerator.cxx
@@ -121,30 +121,33 @@ class cmGlobalVisualStudioVersionedGenerator::Factory15
   : public cmGlobalGeneratorFactory
 {
 public:
-  cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
-                                           cmake* cm) const override
+  std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
+    const std::string& name, cmake* cm) const override
   {
     std::string genName;
     const char* p = cmVS15GenName(name, genName);
     if (!p) {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
     if (!*p) {
-      return new cmGlobalVisualStudioVersionedGenerator(
-        cmGlobalVisualStudioGenerator::VS15, cm, genName, "");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudioVersionedGenerator(
+          cmGlobalVisualStudioGenerator::VS15, cm, genName, ""));
     }
     if (*p++ != ' ') {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
     if (strcmp(p, "Win64") == 0) {
-      return new cmGlobalVisualStudioVersionedGenerator(
-        cmGlobalVisualStudioGenerator::VS15, cm, genName, "x64");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudioVersionedGenerator(
+          cmGlobalVisualStudioGenerator::VS15, cm, genName, "x64"));
     }
     if (strcmp(p, "ARM") == 0) {
-      return new cmGlobalVisualStudioVersionedGenerator(
-        cmGlobalVisualStudioGenerator::VS15, cm, genName, "ARM");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudioVersionedGenerator(
+          cmGlobalVisualStudioGenerator::VS15, cm, genName, "ARM"));
     }
-    return 0;
+    return std::unique_ptr<cmGlobalGenerator>();
   }
 
   void GetDocumentation(cmDocumentationEntry& entry) const override
@@ -185,10 +188,10 @@ public:
   std::string GetDefaultPlatformName() const override { return "Win32"; }
 };
 
-cmGlobalGeneratorFactory*
+std::unique_ptr<cmGlobalGeneratorFactory>
 cmGlobalVisualStudioVersionedGenerator::NewFactory15()
 {
-  return new Factory15;
+  return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory15);
 }
 
 static const char vs16generatorName[] = "Visual Studio 16 2019";
@@ -212,19 +215,20 @@ class cmGlobalVisualStudioVersionedGenerator::Factory16
   : public cmGlobalGeneratorFactory
 {
 public:
-  cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
-                                           cmake* cm) const override
+  std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
+    const std::string& name, cmake* cm) const override
   {
     std::string genName;
     const char* p = cmVS16GenName(name, genName);
     if (!p) {
-      return 0;
+      return std::unique_ptr<cmGlobalGenerator>();
     }
     if (!*p) {
-      return new cmGlobalVisualStudioVersionedGenerator(
-        cmGlobalVisualStudioGenerator::VS16, cm, genName, "");
+      return std::unique_ptr<cmGlobalGenerator>(
+        new cmGlobalVisualStudioVersionedGenerator(
+          cmGlobalVisualStudioGenerator::VS16, cm, genName, ""));
     }
-    return 0;
+    return std::unique_ptr<cmGlobalGenerator>();
   }
 
   void GetDocumentation(cmDocumentationEntry& entry) const override
@@ -265,10 +269,10 @@ public:
   }
 };
 
-cmGlobalGeneratorFactory*
+std::unique_ptr<cmGlobalGeneratorFactory>
 cmGlobalVisualStudioVersionedGenerator::NewFactory16()
 {
-  return new Factory16;
+  return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory16);
 }
 
 cmGlobalVisualStudioVersionedGenerator::cmGlobalVisualStudioVersionedGenerator(
diff --git a/Source/cmGlobalVisualStudioVersionedGenerator.h b/Source/cmGlobalVisualStudioVersionedGenerator.h
index 466816b..abb6095 100644
--- a/Source/cmGlobalVisualStudioVersionedGenerator.h
+++ b/Source/cmGlobalVisualStudioVersionedGenerator.h
@@ -6,6 +6,7 @@
 #include "cmConfigure.h" // IWYU pragma: keep
 
 #include <iosfwd>
+#include <memory>
 #include <string>
 
 #include "cmGlobalVisualStudio14Generator.h"
@@ -19,8 +20,8 @@ class cmGlobalVisualStudioVersionedGenerator
   : public cmGlobalVisualStudio14Generator
 {
 public:
-  static cmGlobalGeneratorFactory* NewFactory15();
-  static cmGlobalGeneratorFactory* NewFactory16();
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory15();
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory16();
 
   bool MatchesGeneratorName(const std::string& name) const override;
 
diff --git a/Source/cmGlobalWatcomWMakeGenerator.h b/Source/cmGlobalWatcomWMakeGenerator.h
index 64ace13..c0daf8a 100644
--- a/Source/cmGlobalWatcomWMakeGenerator.h
+++ b/Source/cmGlobalWatcomWMakeGenerator.h
@@ -6,6 +6,7 @@
 #include "cmConfigure.h" // IWYU pragma: keep
 
 #include <iosfwd>
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -25,9 +26,10 @@ class cmGlobalWatcomWMakeGenerator : public cmGlobalUnixMakefileGenerator3
 {
 public:
   cmGlobalWatcomWMakeGenerator(cmake* cm);
-  static cmGlobalGeneratorFactory* NewFactory()
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory()
   {
-    return new cmGlobalGeneratorSimpleFactory<cmGlobalWatcomWMakeGenerator>();
+    return std::unique_ptr<cmGlobalGeneratorFactory>(
+      new cmGlobalGeneratorSimpleFactory<cmGlobalWatcomWMakeGenerator>());
   }
   //! Get the name for the generator.
   std::string GetName() const override
diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx
index dcd9705..607b2b3 100644
--- a/Source/cmGlobalXCodeGenerator.cxx
+++ b/Source/cmGlobalXCodeGenerator.cxx
@@ -131,8 +131,8 @@ public:
 class cmGlobalXCodeGenerator::Factory : public cmGlobalGeneratorFactory
 {
 public:
-  cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
-                                           cmake* cm) const override;
+  std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
+    const std::string& name, cmake* cm) const override;
 
   void GetDocumentation(cmDocumentationEntry& entry) const override
   {
@@ -181,16 +181,17 @@ cmGlobalXCodeGenerator::cmGlobalXCodeGenerator(
   cm->GetState()->SetIsGeneratorMultiConfig(true);
 }
 
-cmGlobalGeneratorFactory* cmGlobalXCodeGenerator::NewFactory()
+std::unique_ptr<cmGlobalGeneratorFactory> cmGlobalXCodeGenerator::NewFactory()
 {
-  return new Factory;
+  return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory);
 }
 
-cmGlobalGenerator* cmGlobalXCodeGenerator::Factory::CreateGlobalGenerator(
-  const std::string& name, cmake* cm) const
+std::unique_ptr<cmGlobalGenerator>
+cmGlobalXCodeGenerator::Factory::CreateGlobalGenerator(const std::string& name,
+                                                       cmake* cm) const
 {
   if (name != GetActualName()) {
-    return nullptr;
+    return std::unique_ptr<cmGlobalGenerator>();
   }
 #if !defined(CMAKE_BOOTSTRAP)
   cmXcodeVersionParser parser;
@@ -226,16 +227,17 @@ cmGlobalGenerator* cmGlobalXCodeGenerator::Factory::CreateGlobalGenerator(
   if (version_number < 50) {
     cm->IssueMessage(MessageType::FATAL_ERROR,
                      "Xcode " + version_string + " not supported.");
-    return nullptr;
+    return std::unique_ptr<cmGlobalGenerator>();
   }
 
-  auto gg = cm::make_unique<cmGlobalXCodeGenerator>(cm, version_string,
-                                                    version_number);
-  return gg.release();
+  return std::unique_ptr<cmGlobalGenerator>(
+    cm::make_unique<cmGlobalXCodeGenerator>(cm, version_string,
+                                            version_number));
 #else
   std::cerr << "CMake should be built with cmake to use Xcode, "
                "default to Xcode 1.5\n";
-  return new cmGlobalXCodeGenerator(cm);
+  return std::unique_ptr<cmGlobalGenerator>(
+    cm::make_unique<cmGlobalXCodeGenerator>(cm));
 #endif
 }
 
diff --git a/Source/cmGlobalXCodeGenerator.h b/Source/cmGlobalXCodeGenerator.h
index a12021d..df68f80 100644
--- a/Source/cmGlobalXCodeGenerator.h
+++ b/Source/cmGlobalXCodeGenerator.h
@@ -35,7 +35,7 @@ class cmGlobalXCodeGenerator : public cmGlobalGenerator
 public:
   cmGlobalXCodeGenerator(cmake* cm, std::string const& version_string,
                          unsigned int version_number);
-  static cmGlobalGeneratorFactory* NewFactory();
+  static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory();
 
   //! Get the name for the generator.
   std::string GetName() const override
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index 44328d3..fa7bce1 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -3618,8 +3618,7 @@ int cmMakefile::TryCompile(const std::string& srcdir,
   // be run that way but the cmake object requires a vailid path
   cmake cm(cmake::RoleProject, cmState::Project);
   cm.SetIsInTryCompile(true);
-  cmGlobalGenerator* gg =
-    cm.CreateGlobalGenerator(this->GetGlobalGenerator()->GetName());
+  auto gg = cm.CreateGlobalGenerator(this->GetGlobalGenerator()->GetName());
   if (!gg) {
     this->IssueMessage(MessageType::INTERNAL_ERROR,
                        "Global generator '" +
@@ -3630,7 +3629,7 @@ int cmMakefile::TryCompile(const std::string& srcdir,
     return 1;
   }
   gg->RecursionDepth = this->RecursionDepth;
-  cm.SetGlobalGenerator(gg);
+  cm.SetGlobalGenerator(std::move(gg));
 
   // do a configure
   cm.SetHomeDirectory(srcdir);
@@ -3639,7 +3638,7 @@ int cmMakefile::TryCompile(const std::string& srcdir,
   cm.SetGeneratorPlatform(this->GetSafeDefinition("CMAKE_GENERATOR_PLATFORM"));
   cm.SetGeneratorToolset(this->GetSafeDefinition("CMAKE_GENERATOR_TOOLSET"));
   cm.LoadCache();
-  if (!gg->IsMultiConfig()) {
+  if (!cm.GetGlobalGenerator()->IsMultiConfig()) {
     if (const char* config =
           this->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION")) {
       // Tell the single-configuration generator which one to use.
@@ -3685,7 +3684,8 @@ int cmMakefile::TryCompile(const std::string& srcdir,
     cm.SetCacheArgs(*cmakeArgs);
   }
   // to save time we pass the EnableLanguage info directly
-  gg->EnableLanguagesFromGenerator(this->GetGlobalGenerator(), this);
+  cm.GetGlobalGenerator()->EnableLanguagesFromGenerator(
+    this->GetGlobalGenerator(), this);
   if (this->IsOn("CMAKE_SUPPRESS_DEVELOPER_WARNINGS")) {
     cm.AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", "TRUE", "",
                      cmStateEnums::INTERNAL);
diff --git a/Source/cmServerProtocol.cxx b/Source/cmServerProtocol.cxx
index 56003df..1d4ea01 100644
--- a/Source/cmServerProtocol.cxx
+++ b/Source/cmServerProtocol.cxx
@@ -744,7 +744,7 @@ void cmServerProtocol1::GeneratorInformation::SetupGenerator(
   cm->SetHomeDirectory(SourceDirectory);
   cm->SetHomeOutputDirectory(BuildDirectory);
 
-  cmGlobalGenerator* gg = cm->CreateGlobalGenerator(fullGeneratorName);
+  auto gg = cm->CreateGlobalGenerator(fullGeneratorName);
   if (!gg) {
     setErrorMessage(
       errorMessage,
@@ -753,7 +753,7 @@ void cmServerProtocol1::GeneratorInformation::SetupGenerator(
     return;
   }
 
-  cm->SetGlobalGenerator(gg);
+  cm->SetGlobalGenerator(std::move(gg));
 
   cm->SetGeneratorToolset(Toolset);
   cm->SetGeneratorPlatform(Platform);
diff --git a/Source/cmake.cxx b/Source/cmake.cxx
index 5341971..ab76df9 100644
--- a/Source/cmake.cxx
+++ b/Source/cmake.cxx
@@ -71,6 +71,8 @@
 // include the generator
 #if defined(_WIN32) && !defined(__CYGWIN__)
 #  if !defined(CMAKE_BOOT_MINGW)
+#    include <cmext/memory>
+
 #    include "cmGlobalBorlandMakefileGenerator.h"
 #    include "cmGlobalJOMMakefileGenerator.h"
 #    include "cmGlobalNMakeMakefileGenerator.h"
@@ -203,14 +205,7 @@ cmake::cmake(Role role, cmState::Mode mode)
   }
 }
 
-cmake::~cmake()
-{
-  if (this->GlobalGenerator) {
-    delete this->GlobalGenerator;
-    this->GlobalGenerator = nullptr;
-  }
-  cmDeleteAll(this->Generators);
-}
+cmake::~cmake() = default;
 
 #if !defined(CMAKE_BOOTSTRAP)
 Json::Value cmake::ReportVersionJson() const
@@ -463,12 +458,12 @@ void cmake::ReadListFile(const std::vector<std::string>& args,
 {
   // if a generator was not yet created, temporarily create one
   cmGlobalGenerator* gg = this->GetGlobalGenerator();
-  bool created = false;
 
   // if a generator was not specified use a generic one
+  std::unique_ptr<cmGlobalGenerator> gen;
   if (!gg) {
-    gg = new cmGlobalGenerator(this);
-    created = true;
+    gen = cm::make_unique<cmGlobalGenerator>(this);
+    gg = gen.get();
   }
 
   // read in the list file to fill the cache
@@ -490,11 +485,6 @@ void cmake::ReadListFile(const std::vector<std::string>& args,
       cmSystemTools::Error("Error processing file: " + path);
     }
   }
-
-  // free generic one if generated
-  if (created) {
-    delete gg;
-  }
 }
 
 bool cmake::FindPackage(const std::vector<std::string>& args)
@@ -502,9 +492,7 @@ bool cmake::FindPackage(const std::vector<std::string>& args)
   this->SetHomeDirectory(cmSystemTools::GetCurrentWorkingDirectory());
   this->SetHomeOutputDirectory(cmSystemTools::GetCurrentWorkingDirectory());
 
-  // if a generator was not yet created, temporarily create one
-  cmGlobalGenerator* gg = new cmGlobalGenerator(this);
-  this->SetGlobalGenerator(gg);
+  this->SetGlobalGenerator(cm::make_unique<cmGlobalGenerator>(this));
 
   cmStateSnapshot snapshot = this->GetCurrentSnapshot();
   snapshot.GetDirectory().SetCurrentBinary(
@@ -513,9 +501,9 @@ bool cmake::FindPackage(const std::vector<std::string>& args)
     cmSystemTools::GetCurrentWorkingDirectory());
   // read in the list file to fill the cache
   snapshot.SetDefaultDefinitions();
-  auto mfu = cm::make_unique<cmMakefile>(gg, snapshot);
+  auto mfu = cm::make_unique<cmMakefile>(this->GetGlobalGenerator(), snapshot);
   cmMakefile* mf = mfu.get();
-  gg->AddMakefile(std::move(mfu));
+  this->GlobalGenerator->AddMakefile(std::move(mfu));
 
   mf->SetArgcArgv(args);
 
@@ -540,8 +528,8 @@ bool cmake::FindPackage(const std::vector<std::string>& args)
     std::string includes = mf->GetSafeDefinition("PACKAGE_INCLUDE_DIRS");
     std::vector<std::string> includeDirs = cmExpandedList(includes);
 
-    gg->CreateGenerationObjects();
-    const auto& lg = gg->LocalGenerators[0];
+    this->GlobalGenerator->CreateGenerationObjects();
+    const auto& lg = this->GlobalGenerator->LocalGenerators[0];
     std::string includeFlags =
       lg->GetIncludeFlags(includeDirs, nullptr, language);
 
@@ -567,8 +555,9 @@ bool cmake::FindPackage(const std::vector<std::string>& args)
     std::string linkPath;
     std::string flags;
     std::string linkFlags;
-    gg->CreateGenerationObjects();
-    cmGeneratorTarget* gtgt = gg->FindGeneratorTarget(tgt->GetName());
+    this->GlobalGenerator->CreateGenerationObjects();
+    cmGeneratorTarget* gtgt =
+      this->GlobalGenerator->FindGeneratorTarget(tgt->GetName());
     cmLocalGenerator* lg = gtgt->GetLocalGenerator();
     cmLinkLineComputer linkLineComputer(lg,
                                         lg->GetStateSnapshot().GetDirectory());
@@ -588,10 +577,6 @@ bool cmake::FindPackage(const std::vector<std::string>& args)
           }*/
   }
 
-  // free generic one if generated
-  //  this->SetGlobalGenerator(0); // setting 0-pointer is not possible
-  //  delete gg; // this crashes inside the cmake instance
-
   return packageFound;
 }
 
@@ -839,7 +824,7 @@ void cmake::SetArgs(const std::vector<std::string>& args)
         }
         value = args[i];
       }
-      cmGlobalGenerator* gen = this->CreateGlobalGenerator(value);
+      auto gen = this->CreateGlobalGenerator(value);
       if (!gen) {
         std::string kdevError;
         if (value.find("KDevelop3", 0) != std::string::npos) {
@@ -851,7 +836,7 @@ void cmake::SetArgs(const std::vector<std::string>& args)
         this->PrintGeneratorList();
         return;
       }
-      this->SetGlobalGenerator(gen);
+      this->SetGlobalGenerator(std::move(gen));
     }
     // no option assume it is the path to the source or an existing build
     else {
@@ -1119,7 +1104,7 @@ void cmake::AddDefaultExtraGenerators()
 void cmake::GetRegisteredGenerators(std::vector<GeneratorInfo>& generators,
                                     bool includeNamesWithPlatform) const
 {
-  for (cmGlobalGeneratorFactory* gen : this->Generators) {
+  for (const auto& gen : this->Generators) {
     std::vector<std::string> names = gen->GetGeneratorNames();
 
     if (includeNamesWithPlatform) {
@@ -1168,7 +1153,8 @@ void cmake::GetRegisteredGenerators(std::vector<GeneratorInfo>& generators,
   }
 }
 
-static std::pair<cmExternalMakefileProjectGenerator*, std::string>
+static std::pair<std::unique_ptr<cmExternalMakefileProjectGenerator>,
+                 std::string>
 createExtraGenerator(
   const std::vector<cmExternalMakefileProjectGeneratorFactory*>& in,
   const std::string& name)
@@ -1191,15 +1177,17 @@ createExtraGenerator(
   return { nullptr, name };
 }
 
-cmGlobalGenerator* cmake::CreateGlobalGenerator(const std::string& gname)
+std::unique_ptr<cmGlobalGenerator> cmake::CreateGlobalGenerator(
+  const std::string& gname)
 {
-  std::pair<cmExternalMakefileProjectGenerator*, std::string> extra =
-    createExtraGenerator(this->ExtraGenerators, gname);
-  cmExternalMakefileProjectGenerator* extraGenerator = extra.first;
-  const std::string name = extra.second;
+  std::pair<std::unique_ptr<cmExternalMakefileProjectGenerator>, std::string>
+    extra = createExtraGenerator(this->ExtraGenerators, gname);
+  std::unique_ptr<cmExternalMakefileProjectGenerator>& extraGenerator =
+    extra.first;
+  const std::string& name = extra.second;
 
-  cmGlobalGenerator* generator = nullptr;
-  for (cmGlobalGeneratorFactory* g : this->Generators) {
+  std::unique_ptr<cmGlobalGenerator> generator;
+  for (const auto& g : this->Generators) {
     generator = g->CreateGlobalGenerator(name, this);
     if (generator) {
       break;
@@ -1207,9 +1195,7 @@ cmGlobalGenerator* cmake::CreateGlobalGenerator(const std::string& gname)
   }
 
   if (generator) {
-    generator->SetExternalMakefileProjectGenerator(extraGenerator);
-  } else {
-    delete extraGenerator;
+    generator->SetExternalMakefileProjectGenerator(std::move(extraGenerator));
   }
 
   return generator;
@@ -1261,15 +1247,13 @@ std::string cmake::FindCacheFile(const std::string& binaryDir)
   return cachePath;
 }
 
-void cmake::SetGlobalGenerator(cmGlobalGenerator* gg)
+void cmake::SetGlobalGenerator(std::unique_ptr<cmGlobalGenerator> gg)
 {
   if (!gg) {
     cmSystemTools::Error("Error SetGlobalGenerator called with null");
     return;
   }
-  // delete the old generator
   if (this->GlobalGenerator) {
-    delete this->GlobalGenerator;
     // restore the original environment variables CXX and CC
     // Restore CC
     std::string env = "CC=";
@@ -1285,7 +1269,7 @@ void cmake::SetGlobalGenerator(cmGlobalGenerator* gg)
   }
 
   // set the new
-  this->GlobalGenerator = gg;
+  this->GlobalGenerator = std::move(gg);
 
   // set the global flag for unix style paths on cmSystemTools as soon as
   // the generator is set.  This allows gmake to be used on windows.
@@ -1669,13 +1653,12 @@ int cmake::ActualConfigure()
 std::unique_ptr<cmGlobalGenerator> cmake::EvaluateDefaultGlobalGenerator()
 {
   if (!this->EnvironmentGenerator.empty()) {
-    cmGlobalGenerator* gen =
-      this->CreateGlobalGenerator(this->EnvironmentGenerator);
+    auto gen = this->CreateGlobalGenerator(this->EnvironmentGenerator);
     if (!gen) {
       cmSystemTools::Error("CMAKE_GENERATOR was set but the specified "
                            "generator doesn't exist. Using CMake default.");
     } else {
-      return std::unique_ptr<cmGlobalGenerator>(gen);
+      return gen;
     }
   }
 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
@@ -1725,13 +1708,14 @@ std::unique_ptr<cmGlobalGenerator> cmake::EvaluateDefaultGlobalGenerator()
       }
     }
   }
-  cmGlobalGenerator* gen = this->CreateGlobalGenerator(found);
+  auto gen = this->CreateGlobalGenerator(found);
   if (!gen) {
-    gen = new cmGlobalNMakeMakefileGenerator(this);
+    gen = cm::make_unique<cmGlobalNMakeMakefileGenerator>(this);
   }
-  return std::unique_ptr<cmGlobalGenerator>(gen);
+  return std::unique_ptr<cmGlobalGenerator>(std::move(gen));
 #else
-  return cm::make_unique<cmGlobalUnixMakefileGenerator3>(this);
+  return std::unique_ptr<cmGlobalGenerator>(
+    cm::make_unique<cmGlobalUnixMakefileGenerator3>(this));
 #endif
 }
 
@@ -1742,7 +1726,7 @@ void cmake::CreateDefaultGlobalGenerator()
   // This print could be unified for all platforms
   std::cout << "-- Building for: " << gen->GetName() << "\n";
 #endif
-  this->SetGlobalGenerator(gen.release());
+  this->SetGlobalGenerator(std::move(gen));
 }
 
 void cmake::PreLoadCMakeFiles()
@@ -1845,10 +1829,11 @@ int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
       cmSystemTools::Message("CMake Configure step failed.  "
                              "Build files cannot be regenerated correctly.  "
                              "Attempting to stop IDE build.");
-      cmGlobalVisualStudioGenerator* gg =
-        static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
-      gg->CallVisualStudioMacro(cmGlobalVisualStudioGenerator::MacroStop,
-                                this->VSSolutionFile);
+      cmGlobalVisualStudioGenerator& gg =
+        cm::static_reference_cast<cmGlobalVisualStudioGenerator>(
+          this->GlobalGenerator);
+      gg.CallVisualStudioMacro(cmGlobalVisualStudioGenerator::MacroStop,
+                               this->VSSolutionFile);
     }
 #endif
     return ret;
@@ -2104,7 +2089,7 @@ void cmake::AppendGlobalGeneratorsDocumentation(
   const std::string defaultName = defaultGenerator->GetName();
   bool foundDefaultOne = false;
 
-  for (cmGlobalGeneratorFactory* g : this->Generators) {
+  for (const auto& g : this->Generators) {
     cmDocumentationEntry e;
     g->GetDocumentation(e);
     if (!foundDefaultOne && cmHasPrefix(e.Name, defaultName)) {
@@ -2244,8 +2229,8 @@ int cmake::CheckBuildSystem()
     }
 
     // Create the generator and use it to clear the dependencies.
-    std::unique_ptr<cmGlobalGenerator> ggd(
-      this->CreateGlobalGenerator(genName));
+    std::unique_ptr<cmGlobalGenerator> ggd =
+      this->CreateGlobalGenerator(genName);
     if (ggd) {
       cm.GetCurrentSnapshot().SetDefaultDefinitions();
       cmMakefile mfd(ggd.get(), cm.GetCurrentSnapshot());
@@ -2458,12 +2443,12 @@ int cmake::GetSystemInformation(std::vector<std::string>& args)
         }
         value = args[i];
       }
-      cmGlobalGenerator* gen = this->CreateGlobalGenerator(value);
+      auto gen = this->CreateGlobalGenerator(value);
       if (!gen) {
         cmSystemTools::Error("Could not create named generator " + value);
         this->PrintGeneratorList();
       } else {
-        this->SetGlobalGenerator(gen);
+        this->SetGlobalGenerator(std::move(gen));
       }
     }
     // no option assume it is the output file
@@ -2675,34 +2660,37 @@ int cmake::Build(int jobs, const std::string& dir,
     std::cerr << "Error: could not find CMAKE_GENERATOR in Cache\n";
     return 1;
   }
-  cmGlobalGenerator* gen = this->CreateGlobalGenerator(cachedGenerator);
+  auto gen = this->CreateGlobalGenerator(cachedGenerator);
   if (!gen) {
     std::cerr << "Error: could create CMAKE_GENERATOR \"" << cachedGenerator
               << "\"\n";
     return 1;
   }
-  this->SetGlobalGenerator(gen);
+  this->SetGlobalGenerator(std::move(gen));
   const char* cachedGeneratorInstance =
     this->State->GetCacheEntryValue("CMAKE_GENERATOR_INSTANCE");
   if (cachedGeneratorInstance) {
-    cmMakefile mf(gen, this->GetCurrentSnapshot());
-    if (!gen->SetGeneratorInstance(cachedGeneratorInstance, &mf)) {
+    cmMakefile mf(this->GetGlobalGenerator(), this->GetCurrentSnapshot());
+    if (!this->GlobalGenerator->SetGeneratorInstance(cachedGeneratorInstance,
+                                                     &mf)) {
       return 1;
     }
   }
   const char* cachedGeneratorPlatform =
     this->State->GetCacheEntryValue("CMAKE_GENERATOR_PLATFORM");
   if (cachedGeneratorPlatform) {
-    cmMakefile mf(gen, this->GetCurrentSnapshot());
-    if (!gen->SetGeneratorPlatform(cachedGeneratorPlatform, &mf)) {
+    cmMakefile mf(this->GetGlobalGenerator(), this->GetCurrentSnapshot());
+    if (!this->GlobalGenerator->SetGeneratorPlatform(cachedGeneratorPlatform,
+                                                     &mf)) {
       return 1;
     }
   }
   const char* cachedGeneratorToolset =
     this->State->GetCacheEntryValue("CMAKE_GENERATOR_TOOLSET");
   if (cachedGeneratorToolset) {
-    cmMakefile mf(gen, this->GetCurrentSnapshot());
-    if (!gen->SetGeneratorToolset(cachedGeneratorToolset, true, &mf)) {
+    cmMakefile mf(this->GetGlobalGenerator(), this->GetCurrentSnapshot());
+    if (!this->GlobalGenerator->SetGeneratorToolset(cachedGeneratorToolset,
+                                                    true, &mf)) {
       return 1;
     }
   }
@@ -2777,10 +2765,11 @@ int cmake::Build(int jobs, const std::string& dir,
   }
 #endif
 
-  gen->PrintBuildCommandAdvice(std::cerr, jobs);
-  return gen->Build(jobs, "", dir, projName, targets, output, "", config,
-                    clean, false, verbose, cmDuration::zero(),
-                    cmSystemTools::OUTPUT_PASSTHROUGH, nativeOptions);
+  this->GlobalGenerator->PrintBuildCommandAdvice(std::cerr, jobs);
+  return this->GlobalGenerator->Build(
+    jobs, "", dir, projName, targets, output, "", config, clean, false,
+    verbose, cmDuration::zero(), cmSystemTools::OUTPUT_PASSTHROUGH,
+    nativeOptions);
 }
 
 bool cmake::Open(const std::string& dir, bool dryRun)
@@ -2808,8 +2797,8 @@ bool cmake::Open(const std::string& dir, bool dryRun)
     cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
       genName, extraGenName ? *extraGenName : "");
 
-  std::unique_ptr<cmGlobalGenerator> gen(
-    this->CreateGlobalGenerator(fullName));
+  std::unique_ptr<cmGlobalGenerator> gen =
+    this->CreateGlobalGenerator(fullName);
   if (!gen) {
     std::cerr << "Error: could create CMAKE_GENERATOR \"" << fullName
               << "\"\n";
diff --git a/Source/cmake.h b/Source/cmake.h
index 266d66c..22d3c39 100644
--- a/Source/cmake.h
+++ b/Source/cmake.h
@@ -213,21 +213,25 @@ public:
   void PreLoadCMakeFiles();
 
   //! Create a GlobalGenerator
-  cmGlobalGenerator* CreateGlobalGenerator(const std::string& name);
+  std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
+    const std::string& name);
 
   //! Return the global generator assigned to this instance of cmake
-  cmGlobalGenerator* GetGlobalGenerator() { return this->GlobalGenerator; }
+  cmGlobalGenerator* GetGlobalGenerator()
+  {
+    return this->GlobalGenerator.get();
+  }
   //! Return the global generator assigned to this instance of cmake, const
   const cmGlobalGenerator* GetGlobalGenerator() const
   {
-    return this->GlobalGenerator;
+    return this->GlobalGenerator.get();
   }
 
   //! Return the full path to where the CMakeCache.txt file should be.
   static std::string FindCacheFile(const std::string& binaryDir);
 
   //! Return the global generator assigned to this instance of cmake
-  void SetGlobalGenerator(cmGlobalGenerator*);
+  void SetGlobalGenerator(std::unique_ptr<cmGlobalGenerator>);
 
   //! Get the names of the current registered generators
   void GetRegisteredGenerators(std::vector<GeneratorInfo>& generators,
@@ -547,7 +551,8 @@ protected:
   void RunCheckForUnusedVariables();
   int HandleDeleteCacheVariables(const std::string& var);
 
-  using RegisteredGeneratorsVector = std::vector<cmGlobalGeneratorFactory*>;
+  using RegisteredGeneratorsVector =
+    std::vector<std::unique_ptr<cmGlobalGeneratorFactory>>;
   RegisteredGeneratorsVector Generators;
   using RegisteredExtraGeneratorsVector =
     std::vector<cmExternalMakefileProjectGeneratorFactory*>;
@@ -557,7 +562,6 @@ protected:
   void AddDefaultGenerators();
   void AddDefaultExtraGenerators();
 
-  cmGlobalGenerator* GlobalGenerator = nullptr;
   std::map<std::string, DiagLevel> DiagLevels;
   std::string GeneratorInstance;
   std::string GeneratorPlatform;
@@ -638,6 +642,8 @@ private:
 
   std::stack<std::string> CheckInProgressMessages;
 
+  std::unique_ptr<cmGlobalGenerator> GlobalGenerator;
+
   void UpdateConversionPathTable();
 
   //! Print a list of valid generators to stderr.
diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx
index 9a07aef..67c776e 100644
--- a/Source/cmcmd.cxx
+++ b/Source/cmcmd.cxx
@@ -1080,13 +1080,13 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string> const& args)
       cm.SetHomeDirectory(homeDir);
       cm.SetHomeOutputDirectory(homeOutDir);
       cm.GetCurrentSnapshot().SetDefaultDefinitions();
-      if (cmGlobalGenerator* ggd = cm.CreateGlobalGenerator(gen)) {
-        cm.SetGlobalGenerator(ggd);
+      if (auto ggd = cm.CreateGlobalGenerator(gen)) {
+        cm.SetGlobalGenerator(std::move(ggd));
         cmStateSnapshot snapshot = cm.GetCurrentSnapshot();
         snapshot.GetDirectory().SetCurrentBinary(startOutDir);
         snapshot.GetDirectory().SetCurrentSource(startDir);
-        cmMakefile mf(ggd, snapshot);
-        auto lgd = ggd->CreateLocalGenerator(&mf);
+        cmMakefile mf(cm.GetGlobalGenerator(), snapshot);
+        auto lgd = cm.GetGlobalGenerator()->CreateLocalGenerator(&mf);
 
         // Actually scan dependencies.
         return lgd->UpdateDependencies(depInfo, verbose, color) ? 0 : 2;
-- 
cgit v0.12