Quellcode durchsuchen

Modernize memory management

Update internals of various classes.
Marc Chevrier vor 5 Jahren
Ursprung
Commit
9de0355d4f

+ 6 - 7
Source/QtDialog/CMakeSetupDialog.cxx

@@ -2,6 +2,8 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "CMakeSetupDialog.h"
 
+#include <cm/memory>
+
 #include <QCloseEvent>
 #include <QCoreApplication>
 #include <QDesktopServices>
@@ -39,23 +41,21 @@
 
 QCMakeThread::QCMakeThread(QObject* p)
   : QThread(p)
-  , CMakeInstance(nullptr)
 {
 }
 
 QCMake* QCMakeThread::cmakeInstance() const
 {
-  return this->CMakeInstance;
+  return this->CMakeInstance.get();
 }
 
 void QCMakeThread::run()
 {
-  this->CMakeInstance = new QCMake;
+  this->CMakeInstance = cm::make_unique<QCMake>();
   // emit that this cmake thread is ready for use
   emit this->cmakeInitialized();
   this->exec();
-  delete this->CMakeInstance;
-  this->CMakeInstance = nullptr;
+  this->CMakeInstance.reset();
 }
 
 CMakeSetupDialog::CMakeSetupDialog()
@@ -1206,7 +1206,7 @@ void CMakeSetupDialog::setSearchFilter(const QString& str)
 
 void CMakeSetupDialog::doOutputContextMenu(QPoint pt)
 {
-  QMenu* menu = this->Output->createStandardContextMenu();
+  std::unique_ptr<QMenu> menu(this->Output->createStandardContextMenu());
 
   menu->addSeparator();
   menu->addAction(tr("Find..."), this, SLOT(doOutputFindDialog()),
@@ -1220,7 +1220,6 @@ void CMakeSetupDialog::doOutputContextMenu(QPoint pt)
                   QKeySequence(Qt::Key_F8));
 
   menu->exec(this->Output->mapToGlobal(pt));
-  delete menu;
 }
 
 void CMakeSetupDialog::doOutputFindDialog()

+ 3 - 1
Source/QtDialog/CMakeSetupDialog.h

@@ -3,6 +3,8 @@
 #ifndef CMakeSetupDialog_h
 #define CMakeSetupDialog_h
 
+#include <memory>
+
 #include "QCMake.h"
 #include <QEventLoop>
 #include <QMainWindow>
@@ -143,7 +145,7 @@ signals:
 
 protected:
   virtual void run();
-  QCMake* CMakeInstance;
+  std::unique_ptr<QCMake> CMakeInstance;
 };
 
 #endif // CMakeSetupDialog_h

+ 5 - 6
Source/QtDialog/QCMake.cxx

@@ -2,6 +2,8 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "QCMake.h"
 
+#include <cm/memory>
+
 #include <QCoreApplication>
 #include <QDir>
 
@@ -35,7 +37,8 @@ QCMake::QCMake(QObject* p)
   cmSystemTools::SetStderrCallback(
     [this](std::string const& msg) { this->stderrCallback(msg); });
 
-  this->CMakeInstance = new cmake(cmake::RoleProject, cmState::Project);
+  this->CMakeInstance =
+    cm::make_unique<cmake>(cmake::RoleProject, cmState::Project);
   this->CMakeInstance->SetCMakeEditCommand(
     cmSystemTools::GetCMakeGUICommand());
   this->CMakeInstance->SetProgressCallback(
@@ -55,11 +58,7 @@ QCMake::QCMake(QObject* p)
   }
 }
 
-QCMake::~QCMake()
-{
-  delete this->CMakeInstance;
-  // cmDynamicLoader::FlushCache();
-}
+QCMake::~QCMake() = default;
 
 void QCMake::loadCache(const QString& dir)
 {

+ 2 - 1
Source/QtDialog/QCMake.h

@@ -12,6 +12,7 @@
 #  pragma warning(disable : 4512)
 #endif
 
+#include <memory>
 #include <vector>
 
 #include <QAtomicInt>
@@ -165,7 +166,7 @@ signals:
   void openPossible(bool possible);
 
 protected:
-  cmake* CMakeInstance;
+  std::unique_ptr<cmake> CMakeInstance;
 
   bool interruptCallback();
   void progressCallback(std::string const& msg, float percent);

+ 1 - 4
Source/cmDependsFortran.cxx

@@ -102,10 +102,7 @@ cmDependsFortran::cmDependsFortran(cmLocalGenerator* lg)
   this->SModExt = mf->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_EXT");
 }
 
-cmDependsFortran::~cmDependsFortran()
-{
-  delete this->Internal;
-}
+cmDependsFortran::~cmDependsFortran() = default;
 
 bool cmDependsFortran::WriteDependencies(const std::set<std::string>& sources,
                                          const std::string& obj,

+ 2 - 1
Source/cmDependsFortran.h

@@ -6,6 +6,7 @@
 #include "cmConfigure.h" // IWYU pragma: keep
 
 #include <iosfwd>
+#include <memory>
 #include <set>
 #include <string>
 #include <vector>
@@ -84,7 +85,7 @@ protected:
   std::set<std::string> PPDefinitions;
 
   // Internal implementation details.
-  cmDependsFortranInternals* Internal = nullptr;
+  std::unique_ptr<cmDependsFortranInternals> Internal;
 
 private:
   std::string MaybeConvertToRelativePath(std::string const& base,

+ 17 - 16
Source/cmGlobalXCodeGenerator.cxx

@@ -8,6 +8,7 @@
 #include <cstring>
 #include <iomanip>
 #include <sstream>
+#include <utility>
 
 #include <cm/memory>
 #include <cmext/algorithm>
@@ -642,7 +643,8 @@ void cmGlobalXCodeGenerator::CreateReRunCMakeFile(
                  << "\n";
 }
 
-static bool objectIdLessThan(cmXCodeObject* l, cmXCodeObject* r)
+static bool objectIdLessThan(const std::unique_ptr<cmXCodeObject>& l,
+                             const std::unique_ptr<cmXCodeObject>& r)
 {
   return l->GetId() < r->GetId();
 }
@@ -656,9 +658,6 @@ void cmGlobalXCodeGenerator::SortXCodeObjects()
 void cmGlobalXCodeGenerator::ClearXCodeObjects()
 {
   this->TargetDoneSet.clear();
-  for (auto& obj : this->XCodeObjects) {
-    delete obj;
-  }
   this->XCodeObjects.clear();
   this->XCodeObjectIDs.clear();
   this->XCodeObjectMap.clear();
@@ -668,7 +667,7 @@ void cmGlobalXCodeGenerator::ClearXCodeObjects()
   this->FileRefs.clear();
 }
 
-void cmGlobalXCodeGenerator::addObject(cmXCodeObject* obj)
+void cmGlobalXCodeGenerator::addObject(std::unique_ptr<cmXCodeObject> obj)
 {
   if (obj->GetType() == cmXCodeObject::OBJECT) {
     const std::string& id = obj->GetId();
@@ -683,22 +682,24 @@ void cmGlobalXCodeGenerator::addObject(cmXCodeObject* obj)
     this->XCodeObjectIDs.insert(id);
   }
 
-  this->XCodeObjects.push_back(obj);
+  this->XCodeObjects.push_back(std::move(obj));
 }
 
 cmXCodeObject* cmGlobalXCodeGenerator::CreateObject(
   cmXCodeObject::PBXType ptype)
 {
-  cmXCodeObject* obj = new cmXCode21Object(ptype, cmXCodeObject::OBJECT);
-  this->addObject(obj);
-  return obj;
+  auto obj = cm::make_unique<cmXCode21Object>(ptype, cmXCodeObject::OBJECT);
+  auto ptr = obj.get();
+  this->addObject(std::move(obj));
+  return ptr;
 }
 
 cmXCodeObject* cmGlobalXCodeGenerator::CreateObject(cmXCodeObject::Type type)
 {
-  cmXCodeObject* obj = new cmXCodeObject(cmXCodeObject::None, type);
-  this->addObject(obj);
-  return obj;
+  auto obj = cm::make_unique<cmXCodeObject>(cmXCodeObject::None, type);
+  auto ptr = obj.get();
+  this->addObject(std::move(obj));
+  return ptr;
 }
 
 cmXCodeObject* cmGlobalXCodeGenerator::CreateString(const std::string& s)
@@ -3390,7 +3391,7 @@ bool cmGlobalXCodeGenerator::OutputXCodeSharedSchemes(
   // collect all tests for the targets
   std::map<std::string, cmXCodeScheme::TestObjects> testables;
 
-  for (auto obj : this->XCodeObjects) {
+  for (const auto& obj : this->XCodeObjects) {
     if (obj->GetType() != cmXCodeObject::OBJECT ||
         obj->GetIsA() != cmXCodeObject::PBXNativeTarget) {
       continue;
@@ -3405,7 +3406,7 @@ bool cmGlobalXCodeGenerator::OutputXCodeSharedSchemes(
       continue;
     }
 
-    testables[testee].push_back(obj);
+    testables[testee].push_back(obj.get());
   }
 
   // generate scheme
@@ -3414,14 +3415,14 @@ bool cmGlobalXCodeGenerator::OutputXCodeSharedSchemes(
   // Since the lowest available Xcode version for testing was 6.4,
   // I'm setting this as a limit then
   if (this->XcodeVersion >= 64) {
-    for (auto obj : this->XCodeObjects) {
+    for (const auto& obj : this->XCodeObjects) {
       if (obj->GetType() == cmXCodeObject::OBJECT &&
           (obj->GetIsA() == cmXCodeObject::PBXNativeTarget ||
            obj->GetIsA() == cmXCodeObject::PBXAggregateTarget) &&
           (root->GetMakefile()->GetCMakeInstance()->GetIsInTryCompile() ||
            obj->GetTarget()->GetPropertyAsBool("XCODE_GENERATE_SCHEME"))) {
         const std::string& targetName = obj->GetTarget()->GetName();
-        cmXCodeScheme schm(root, obj, testables[targetName],
+        cmXCodeScheme schm(root, obj.get(), testables[targetName],
                            this->CurrentConfigurationTypes,
                            this->XcodeVersion);
         schm.WriteXCodeSharedScheme(xcProjDir,

+ 6 - 2
Source/cmGlobalXCodeGenerator.h

@@ -37,6 +37,10 @@ public:
                          unsigned int version_number);
   static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory();
 
+  cmGlobalXCodeGenerator(const cmGlobalXCodeGenerator&) = delete;
+  const cmGlobalXCodeGenerator& operator=(const cmGlobalXCodeGenerator&) =
+    delete;
+
   //! Get the name for the generator.
   std::string GetName() const override
   {
@@ -249,7 +253,7 @@ protected:
   unsigned int XcodeVersion;
   std::string VersionString;
   std::set<std::string> XCodeObjectIDs;
-  std::vector<cmXCodeObject*> XCodeObjects;
+  std::vector<std::unique_ptr<cmXCodeObject>> XCodeObjects;
   cmXCodeObject* RootObject;
 
 private:
@@ -273,7 +277,7 @@ private:
   void ComputeArchitectures(cmMakefile* mf);
   void ComputeObjectDirArch(cmMakefile* mf);
 
-  void addObject(cmXCodeObject* obj);
+  void addObject(std::unique_ptr<cmXCodeObject> obj);
   std::string PostBuildMakeTarget(std::string const& tName,
                                   std::string const& configName);
   cmXCodeObject* MainGroupChildren;

+ 4 - 5
Source/cmLocalVisualStudio7Generator.cxx

@@ -2,6 +2,8 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "cmLocalVisualStudio7Generator.h"
 
+#include <cm/memory>
+
 #include <windows.h>
 
 #include <ctype.h> // for isspace
@@ -52,14 +54,11 @@ extern cmVS7FlagTable cmLocalVisualStudio7GeneratorFlagTable[];
 cmLocalVisualStudio7Generator::cmLocalVisualStudio7Generator(
   cmGlobalGenerator* gg, cmMakefile* mf)
   : cmLocalVisualStudioGenerator(gg, mf)
+  , Internal(cm::make_unique<cmLocalVisualStudio7GeneratorInternals>(this))
 {
-  this->Internal = new cmLocalVisualStudio7GeneratorInternals(this);
 }
 
-cmLocalVisualStudio7Generator::~cmLocalVisualStudio7Generator()
-{
-  delete this->Internal;
-}
+cmLocalVisualStudio7Generator::~cmLocalVisualStudio7Generator() = default;
 
 void cmLocalVisualStudio7Generator::AddHelperCommands()
 {

+ 6 - 1
Source/cmLocalVisualStudio7Generator.h

@@ -6,6 +6,7 @@
 #include "cmConfigure.h" // IWYU pragma: keep
 
 #include <iosfwd>
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -48,6 +49,10 @@ public:
 
   virtual ~cmLocalVisualStudio7Generator();
 
+  cmLocalVisualStudio7Generator(const cmLocalVisualStudio7Generator&) = delete;
+  const cmLocalVisualStudio7Generator& operator=(
+    const cmLocalVisualStudio7Generator&) = delete;
+
   void AddHelperCommands() override;
 
   /**
@@ -144,7 +149,7 @@ private:
 
   bool FortranProject;
   bool WindowsCEProject;
-  cmLocalVisualStudio7GeneratorInternals* Internal;
+  std::unique_ptr<cmLocalVisualStudio7GeneratorInternals> Internal;
 };
 
 #endif

+ 12 - 11
Source/cmProcessOutput.cxx

@@ -4,7 +4,10 @@
 #include "cmProcessOutput.h"
 
 #if defined(_WIN32)
+#  include <cm/memory>
+
 #  include <windows.h>
+
 unsigned int cmProcessOutput::defaultCodepage =
   KWSYS_ENCODING_DEFAULT_CODEPAGE;
 #endif
@@ -143,9 +146,9 @@ bool cmProcessOutput::DoDecodeText(std::string raw, std::string& decoded,
   bool success = false;
   const int wlength =
     MultiByteToWideChar(codepage, 0, raw.c_str(), int(raw.size()), NULL, 0);
-  wchar_t* wdata = new wchar_t[wlength];
-  int r = MultiByteToWideChar(codepage, 0, raw.c_str(), int(raw.size()), wdata,
-                              wlength);
+  auto wdata = cm::make_unique<wchar_t[]>(wlength);
+  int r = MultiByteToWideChar(codepage, 0, raw.c_str(), int(raw.size()),
+                              wdata.get(), wlength);
   if (r > 0) {
     if (lastChar) {
       *lastChar = 0;
@@ -154,18 +157,16 @@ bool cmProcessOutput::DoDecodeText(std::string raw, std::string& decoded,
         *lastChar = wdata[wlength - 1];
       }
     }
-    int length = WideCharToMultiByte(defaultCodepage, 0, wdata, wlength, NULL,
-                                     0, NULL, NULL);
-    char* data = new char[length];
-    r = WideCharToMultiByte(defaultCodepage, 0, wdata, wlength, data, length,
-                            NULL, NULL);
+    int length = WideCharToMultiByte(defaultCodepage, 0, wdata.get(), wlength,
+                                     NULL, 0, NULL, NULL);
+    auto data = cm::make_unique<char[]>(length);
+    r = WideCharToMultiByte(defaultCodepage, 0, wdata.get(), wlength,
+                            data.get(), length, NULL, NULL);
     if (r > 0) {
-      decoded = std::string(data, length);
+      decoded = std::string(data.get(), length);
       success = true;
     }
-    delete[] data;
   }
-  delete[] wdata;
   return success;
 }
 #endif

+ 7 - 6
Source/cmXCode21Object.cxx

@@ -30,11 +30,12 @@ void cmXCode21Object::PrintComment(std::ostream& out)
   out << " */";
 }
 
-void cmXCode21Object::PrintList(std::vector<cmXCodeObject*> const& v,
-                                std::ostream& out, PBXType t)
+void cmXCode21Object::PrintList(
+  std::vector<std::unique_ptr<cmXCodeObject>> const& v, std::ostream& out,
+  PBXType t)
 {
   bool hasOne = false;
-  for (auto obj : v) {
+  for (const auto& obj : v) {
     if (obj->GetType() == OBJECT && obj->GetIsA() == t) {
       hasOne = true;
       break;
@@ -44,7 +45,7 @@ void cmXCode21Object::PrintList(std::vector<cmXCodeObject*> const& v,
     return;
   }
   out << "\n/* Begin " << PBXTypeNames[t] << " section */\n";
-  for (auto obj : v) {
+  for (const auto& obj : v) {
     if (obj->GetType() == OBJECT && obj->GetIsA() == t) {
       obj->Print(out);
     }
@@ -52,8 +53,8 @@ void cmXCode21Object::PrintList(std::vector<cmXCodeObject*> const& v,
   out << "/* End " << PBXTypeNames[t] << " section */\n";
 }
 
-void cmXCode21Object::PrintList(std::vector<cmXCodeObject*> const& v,
-                                std::ostream& out)
+void cmXCode21Object::PrintList(
+  std::vector<std::unique_ptr<cmXCodeObject>> const& v, std::ostream& out)
 {
   cmXCodeObject::Indent(1, out);
   out << "objects = {\n";

+ 5 - 3
Source/cmXCode21Object.h

@@ -6,6 +6,7 @@
 #include "cmConfigure.h" // IWYU pragma: keep
 
 #include <iosfwd>
+#include <memory>
 #include <vector>
 
 #include "cmXCodeObject.h"
@@ -15,8 +16,9 @@ class cmXCode21Object : public cmXCodeObject
 public:
   cmXCode21Object(PBXType ptype, Type type);
   void PrintComment(std::ostream&) override;
-  static void PrintList(std::vector<cmXCodeObject*> const&, std::ostream& out,
-                        PBXType t);
-  static void PrintList(std::vector<cmXCodeObject*> const&, std::ostream& out);
+  static void PrintList(std::vector<std::unique_ptr<cmXCodeObject>> const&,
+                        std::ostream& out, PBXType t);
+  static void PrintList(std::vector<std::unique_ptr<cmXCodeObject>> const&,
+                        std::ostream& out);
 };
 #endif