Modernize memory management

Update internals of various classes.
This commit is contained in:
Marc Chevrier
2020-03-02 10:41:17 +01:00
committed by Brad King
parent 07a7bc0e3f
commit 9de0355d4f
13 changed files with 76 additions and 64 deletions
+6 -7
View File
@@ -2,6 +2,8 @@
file Copyright.txt or https://cmake.org/licensing for details. */ file Copyright.txt or https://cmake.org/licensing for details. */
#include "CMakeSetupDialog.h" #include "CMakeSetupDialog.h"
#include <cm/memory>
#include <QCloseEvent> #include <QCloseEvent>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDesktopServices> #include <QDesktopServices>
@@ -39,23 +41,21 @@
QCMakeThread::QCMakeThread(QObject* p) QCMakeThread::QCMakeThread(QObject* p)
: QThread(p) : QThread(p)
, CMakeInstance(nullptr)
{ {
} }
QCMake* QCMakeThread::cmakeInstance() const QCMake* QCMakeThread::cmakeInstance() const
{ {
return this->CMakeInstance; return this->CMakeInstance.get();
} }
void QCMakeThread::run() void QCMakeThread::run()
{ {
this->CMakeInstance = new QCMake; this->CMakeInstance = cm::make_unique<QCMake>();
// emit that this cmake thread is ready for use // emit that this cmake thread is ready for use
emit this->cmakeInitialized(); emit this->cmakeInitialized();
this->exec(); this->exec();
delete this->CMakeInstance; this->CMakeInstance.reset();
this->CMakeInstance = nullptr;
} }
CMakeSetupDialog::CMakeSetupDialog() CMakeSetupDialog::CMakeSetupDialog()
@@ -1206,7 +1206,7 @@ void CMakeSetupDialog::setSearchFilter(const QString& str)
void CMakeSetupDialog::doOutputContextMenu(QPoint pt) void CMakeSetupDialog::doOutputContextMenu(QPoint pt)
{ {
QMenu* menu = this->Output->createStandardContextMenu(); std::unique_ptr<QMenu> menu(this->Output->createStandardContextMenu());
menu->addSeparator(); menu->addSeparator();
menu->addAction(tr("Find..."), this, SLOT(doOutputFindDialog()), menu->addAction(tr("Find..."), this, SLOT(doOutputFindDialog()),
@@ -1220,7 +1220,6 @@ void CMakeSetupDialog::doOutputContextMenu(QPoint pt)
QKeySequence(Qt::Key_F8)); QKeySequence(Qt::Key_F8));
menu->exec(this->Output->mapToGlobal(pt)); menu->exec(this->Output->mapToGlobal(pt));
delete menu;
} }
void CMakeSetupDialog::doOutputFindDialog() void CMakeSetupDialog::doOutputFindDialog()
+3 -1
View File
@@ -3,6 +3,8 @@
#ifndef CMakeSetupDialog_h #ifndef CMakeSetupDialog_h
#define CMakeSetupDialog_h #define CMakeSetupDialog_h
#include <memory>
#include "QCMake.h" #include "QCMake.h"
#include <QEventLoop> #include <QEventLoop>
#include <QMainWindow> #include <QMainWindow>
@@ -143,7 +145,7 @@ signals:
protected: protected:
virtual void run(); virtual void run();
QCMake* CMakeInstance; std::unique_ptr<QCMake> CMakeInstance;
}; };
#endif // CMakeSetupDialog_h #endif // CMakeSetupDialog_h
+5 -6
View File
@@ -2,6 +2,8 @@
file Copyright.txt or https://cmake.org/licensing for details. */ file Copyright.txt or https://cmake.org/licensing for details. */
#include "QCMake.h" #include "QCMake.h"
#include <cm/memory>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDir> #include <QDir>
@@ -35,7 +37,8 @@ QCMake::QCMake(QObject* p)
cmSystemTools::SetStderrCallback( cmSystemTools::SetStderrCallback(
[this](std::string const& msg) { this->stderrCallback(msg); }); [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( this->CMakeInstance->SetCMakeEditCommand(
cmSystemTools::GetCMakeGUICommand()); cmSystemTools::GetCMakeGUICommand());
this->CMakeInstance->SetProgressCallback( this->CMakeInstance->SetProgressCallback(
@@ -55,11 +58,7 @@ QCMake::QCMake(QObject* p)
} }
} }
QCMake::~QCMake() QCMake::~QCMake() = default;
{
delete this->CMakeInstance;
// cmDynamicLoader::FlushCache();
}
void QCMake::loadCache(const QString& dir) void QCMake::loadCache(const QString& dir)
{ {
+2 -1
View File
@@ -12,6 +12,7 @@
# pragma warning(disable : 4512) # pragma warning(disable : 4512)
#endif #endif
#include <memory>
#include <vector> #include <vector>
#include <QAtomicInt> #include <QAtomicInt>
@@ -165,7 +166,7 @@ signals:
void openPossible(bool possible); void openPossible(bool possible);
protected: protected:
cmake* CMakeInstance; std::unique_ptr<cmake> CMakeInstance;
bool interruptCallback(); bool interruptCallback();
void progressCallback(std::string const& msg, float percent); void progressCallback(std::string const& msg, float percent);
+1 -4
View File
@@ -102,10 +102,7 @@ cmDependsFortran::cmDependsFortran(cmLocalGenerator* lg)
this->SModExt = mf->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_EXT"); this->SModExt = mf->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_EXT");
} }
cmDependsFortran::~cmDependsFortran() cmDependsFortran::~cmDependsFortran() = default;
{
delete this->Internal;
}
bool cmDependsFortran::WriteDependencies(const std::set<std::string>& sources, bool cmDependsFortran::WriteDependencies(const std::set<std::string>& sources,
const std::string& obj, const std::string& obj,
+2 -1
View File
@@ -6,6 +6,7 @@
#include "cmConfigure.h" // IWYU pragma: keep #include "cmConfigure.h" // IWYU pragma: keep
#include <iosfwd> #include <iosfwd>
#include <memory>
#include <set> #include <set>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -84,7 +85,7 @@ protected:
std::set<std::string> PPDefinitions; std::set<std::string> PPDefinitions;
// Internal implementation details. // Internal implementation details.
cmDependsFortranInternals* Internal = nullptr; std::unique_ptr<cmDependsFortranInternals> Internal;
private: private:
std::string MaybeConvertToRelativePath(std::string const& base, std::string MaybeConvertToRelativePath(std::string const& base,
+17 -16
View File
@@ -8,6 +8,7 @@
#include <cstring> #include <cstring>
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
#include <utility>
#include <cm/memory> #include <cm/memory>
#include <cmext/algorithm> #include <cmext/algorithm>
@@ -642,7 +643,8 @@ void cmGlobalXCodeGenerator::CreateReRunCMakeFile(
<< "\n"; << "\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(); return l->GetId() < r->GetId();
} }
@@ -656,9 +658,6 @@ void cmGlobalXCodeGenerator::SortXCodeObjects()
void cmGlobalXCodeGenerator::ClearXCodeObjects() void cmGlobalXCodeGenerator::ClearXCodeObjects()
{ {
this->TargetDoneSet.clear(); this->TargetDoneSet.clear();
for (auto& obj : this->XCodeObjects) {
delete obj;
}
this->XCodeObjects.clear(); this->XCodeObjects.clear();
this->XCodeObjectIDs.clear(); this->XCodeObjectIDs.clear();
this->XCodeObjectMap.clear(); this->XCodeObjectMap.clear();
@@ -668,7 +667,7 @@ void cmGlobalXCodeGenerator::ClearXCodeObjects()
this->FileRefs.clear(); this->FileRefs.clear();
} }
void cmGlobalXCodeGenerator::addObject(cmXCodeObject* obj) void cmGlobalXCodeGenerator::addObject(std::unique_ptr<cmXCodeObject> obj)
{ {
if (obj->GetType() == cmXCodeObject::OBJECT) { if (obj->GetType() == cmXCodeObject::OBJECT) {
const std::string& id = obj->GetId(); const std::string& id = obj->GetId();
@@ -683,22 +682,24 @@ void cmGlobalXCodeGenerator::addObject(cmXCodeObject* obj)
this->XCodeObjectIDs.insert(id); this->XCodeObjectIDs.insert(id);
} }
this->XCodeObjects.push_back(obj); this->XCodeObjects.push_back(std::move(obj));
} }
cmXCodeObject* cmGlobalXCodeGenerator::CreateObject( cmXCodeObject* cmGlobalXCodeGenerator::CreateObject(
cmXCodeObject::PBXType ptype) cmXCodeObject::PBXType ptype)
{ {
cmXCodeObject* obj = new cmXCode21Object(ptype, cmXCodeObject::OBJECT); auto obj = cm::make_unique<cmXCode21Object>(ptype, cmXCodeObject::OBJECT);
this->addObject(obj); auto ptr = obj.get();
return obj; this->addObject(std::move(obj));
return ptr;
} }
cmXCodeObject* cmGlobalXCodeGenerator::CreateObject(cmXCodeObject::Type type) cmXCodeObject* cmGlobalXCodeGenerator::CreateObject(cmXCodeObject::Type type)
{ {
cmXCodeObject* obj = new cmXCodeObject(cmXCodeObject::None, type); auto obj = cm::make_unique<cmXCodeObject>(cmXCodeObject::None, type);
this->addObject(obj); auto ptr = obj.get();
return obj; this->addObject(std::move(obj));
return ptr;
} }
cmXCodeObject* cmGlobalXCodeGenerator::CreateString(const std::string& s) cmXCodeObject* cmGlobalXCodeGenerator::CreateString(const std::string& s)
@@ -3390,7 +3391,7 @@ bool cmGlobalXCodeGenerator::OutputXCodeSharedSchemes(
// collect all tests for the targets // collect all tests for the targets
std::map<std::string, cmXCodeScheme::TestObjects> testables; std::map<std::string, cmXCodeScheme::TestObjects> testables;
for (auto obj : this->XCodeObjects) { for (const auto& obj : this->XCodeObjects) {
if (obj->GetType() != cmXCodeObject::OBJECT || if (obj->GetType() != cmXCodeObject::OBJECT ||
obj->GetIsA() != cmXCodeObject::PBXNativeTarget) { obj->GetIsA() != cmXCodeObject::PBXNativeTarget) {
continue; continue;
@@ -3405,7 +3406,7 @@ bool cmGlobalXCodeGenerator::OutputXCodeSharedSchemes(
continue; continue;
} }
testables[testee].push_back(obj); testables[testee].push_back(obj.get());
} }
// generate scheme // generate scheme
@@ -3414,14 +3415,14 @@ bool cmGlobalXCodeGenerator::OutputXCodeSharedSchemes(
// Since the lowest available Xcode version for testing was 6.4, // Since the lowest available Xcode version for testing was 6.4,
// I'm setting this as a limit then // I'm setting this as a limit then
if (this->XcodeVersion >= 64) { if (this->XcodeVersion >= 64) {
for (auto obj : this->XCodeObjects) { for (const auto& obj : this->XCodeObjects) {
if (obj->GetType() == cmXCodeObject::OBJECT && if (obj->GetType() == cmXCodeObject::OBJECT &&
(obj->GetIsA() == cmXCodeObject::PBXNativeTarget || (obj->GetIsA() == cmXCodeObject::PBXNativeTarget ||
obj->GetIsA() == cmXCodeObject::PBXAggregateTarget) && obj->GetIsA() == cmXCodeObject::PBXAggregateTarget) &&
(root->GetMakefile()->GetCMakeInstance()->GetIsInTryCompile() || (root->GetMakefile()->GetCMakeInstance()->GetIsInTryCompile() ||
obj->GetTarget()->GetPropertyAsBool("XCODE_GENERATE_SCHEME"))) { obj->GetTarget()->GetPropertyAsBool("XCODE_GENERATE_SCHEME"))) {
const std::string& targetName = obj->GetTarget()->GetName(); const std::string& targetName = obj->GetTarget()->GetName();
cmXCodeScheme schm(root, obj, testables[targetName], cmXCodeScheme schm(root, obj.get(), testables[targetName],
this->CurrentConfigurationTypes, this->CurrentConfigurationTypes,
this->XcodeVersion); this->XcodeVersion);
schm.WriteXCodeSharedScheme(xcProjDir, schm.WriteXCodeSharedScheme(xcProjDir,
+6 -2
View File
@@ -37,6 +37,10 @@ public:
unsigned int version_number); unsigned int version_number);
static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory(); static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory();
cmGlobalXCodeGenerator(const cmGlobalXCodeGenerator&) = delete;
const cmGlobalXCodeGenerator& operator=(const cmGlobalXCodeGenerator&) =
delete;
//! Get the name for the generator. //! Get the name for the generator.
std::string GetName() const override std::string GetName() const override
{ {
@@ -249,7 +253,7 @@ protected:
unsigned int XcodeVersion; unsigned int XcodeVersion;
std::string VersionString; std::string VersionString;
std::set<std::string> XCodeObjectIDs; std::set<std::string> XCodeObjectIDs;
std::vector<cmXCodeObject*> XCodeObjects; std::vector<std::unique_ptr<cmXCodeObject>> XCodeObjects;
cmXCodeObject* RootObject; cmXCodeObject* RootObject;
private: private:
@@ -273,7 +277,7 @@ private:
void ComputeArchitectures(cmMakefile* mf); void ComputeArchitectures(cmMakefile* mf);
void ComputeObjectDirArch(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 PostBuildMakeTarget(std::string const& tName,
std::string const& configName); std::string const& configName);
cmXCodeObject* MainGroupChildren; cmXCodeObject* MainGroupChildren;
+4 -5
View File
@@ -2,6 +2,8 @@
file Copyright.txt or https://cmake.org/licensing for details. */ file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmLocalVisualStudio7Generator.h" #include "cmLocalVisualStudio7Generator.h"
#include <cm/memory>
#include <windows.h> #include <windows.h>
#include <ctype.h> // for isspace #include <ctype.h> // for isspace
@@ -52,14 +54,11 @@ extern cmVS7FlagTable cmLocalVisualStudio7GeneratorFlagTable[];
cmLocalVisualStudio7Generator::cmLocalVisualStudio7Generator( cmLocalVisualStudio7Generator::cmLocalVisualStudio7Generator(
cmGlobalGenerator* gg, cmMakefile* mf) cmGlobalGenerator* gg, cmMakefile* mf)
: cmLocalVisualStudioGenerator(gg, mf) : cmLocalVisualStudioGenerator(gg, mf)
, Internal(cm::make_unique<cmLocalVisualStudio7GeneratorInternals>(this))
{ {
this->Internal = new cmLocalVisualStudio7GeneratorInternals(this);
} }
cmLocalVisualStudio7Generator::~cmLocalVisualStudio7Generator() cmLocalVisualStudio7Generator::~cmLocalVisualStudio7Generator() = default;
{
delete this->Internal;
}
void cmLocalVisualStudio7Generator::AddHelperCommands() void cmLocalVisualStudio7Generator::AddHelperCommands()
{ {
+6 -1
View File
@@ -6,6 +6,7 @@
#include "cmConfigure.h" // IWYU pragma: keep #include "cmConfigure.h" // IWYU pragma: keep
#include <iosfwd> #include <iosfwd>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -48,6 +49,10 @@ public:
virtual ~cmLocalVisualStudio7Generator(); virtual ~cmLocalVisualStudio7Generator();
cmLocalVisualStudio7Generator(const cmLocalVisualStudio7Generator&) = delete;
const cmLocalVisualStudio7Generator& operator=(
const cmLocalVisualStudio7Generator&) = delete;
void AddHelperCommands() override; void AddHelperCommands() override;
/** /**
@@ -144,7 +149,7 @@ private:
bool FortranProject; bool FortranProject;
bool WindowsCEProject; bool WindowsCEProject;
cmLocalVisualStudio7GeneratorInternals* Internal; std::unique_ptr<cmLocalVisualStudio7GeneratorInternals> Internal;
}; };
#endif #endif
+12 -11
View File
@@ -4,7 +4,10 @@
#include "cmProcessOutput.h" #include "cmProcessOutput.h"
#if defined(_WIN32) #if defined(_WIN32)
# include <cm/memory>
# include <windows.h> # include <windows.h>
unsigned int cmProcessOutput::defaultCodepage = unsigned int cmProcessOutput::defaultCodepage =
KWSYS_ENCODING_DEFAULT_CODEPAGE; KWSYS_ENCODING_DEFAULT_CODEPAGE;
#endif #endif
@@ -143,9 +146,9 @@ bool cmProcessOutput::DoDecodeText(std::string raw, std::string& decoded,
bool success = false; bool success = false;
const int wlength = const int wlength =
MultiByteToWideChar(codepage, 0, raw.c_str(), int(raw.size()), NULL, 0); MultiByteToWideChar(codepage, 0, raw.c_str(), int(raw.size()), NULL, 0);
wchar_t* wdata = new wchar_t[wlength]; auto wdata = cm::make_unique<wchar_t[]>(wlength);
int r = MultiByteToWideChar(codepage, 0, raw.c_str(), int(raw.size()), wdata, int r = MultiByteToWideChar(codepage, 0, raw.c_str(), int(raw.size()),
wlength); wdata.get(), wlength);
if (r > 0) { if (r > 0) {
if (lastChar) { if (lastChar) {
*lastChar = 0; *lastChar = 0;
@@ -154,18 +157,16 @@ bool cmProcessOutput::DoDecodeText(std::string raw, std::string& decoded,
*lastChar = wdata[wlength - 1]; *lastChar = wdata[wlength - 1];
} }
} }
int length = WideCharToMultiByte(defaultCodepage, 0, wdata, wlength, NULL, int length = WideCharToMultiByte(defaultCodepage, 0, wdata.get(), wlength,
0, NULL, NULL); NULL, 0, NULL, NULL);
char* data = new char[length]; auto data = cm::make_unique<char[]>(length);
r = WideCharToMultiByte(defaultCodepage, 0, wdata, wlength, data, length, r = WideCharToMultiByte(defaultCodepage, 0, wdata.get(), wlength,
NULL, NULL); data.get(), length, NULL, NULL);
if (r > 0) { if (r > 0) {
decoded = std::string(data, length); decoded = std::string(data.get(), length);
success = true; success = true;
} }
delete[] data;
} }
delete[] wdata;
return success; return success;
} }
#endif #endif
+7 -6
View File
@@ -30,11 +30,12 @@ void cmXCode21Object::PrintComment(std::ostream& out)
out << " */"; out << " */";
} }
void cmXCode21Object::PrintList(std::vector<cmXCodeObject*> const& v, void cmXCode21Object::PrintList(
std::ostream& out, PBXType t) std::vector<std::unique_ptr<cmXCodeObject>> const& v, std::ostream& out,
PBXType t)
{ {
bool hasOne = false; bool hasOne = false;
for (auto obj : v) { for (const auto& obj : v) {
if (obj->GetType() == OBJECT && obj->GetIsA() == t) { if (obj->GetType() == OBJECT && obj->GetIsA() == t) {
hasOne = true; hasOne = true;
break; break;
@@ -44,7 +45,7 @@ void cmXCode21Object::PrintList(std::vector<cmXCodeObject*> const& v,
return; return;
} }
out << "\n/* Begin " << PBXTypeNames[t] << " section */\n"; out << "\n/* Begin " << PBXTypeNames[t] << " section */\n";
for (auto obj : v) { for (const auto& obj : v) {
if (obj->GetType() == OBJECT && obj->GetIsA() == t) { if (obj->GetType() == OBJECT && obj->GetIsA() == t) {
obj->Print(out); obj->Print(out);
} }
@@ -52,8 +53,8 @@ void cmXCode21Object::PrintList(std::vector<cmXCodeObject*> const& v,
out << "/* End " << PBXTypeNames[t] << " section */\n"; out << "/* End " << PBXTypeNames[t] << " section */\n";
} }
void cmXCode21Object::PrintList(std::vector<cmXCodeObject*> const& v, void cmXCode21Object::PrintList(
std::ostream& out) std::vector<std::unique_ptr<cmXCodeObject>> const& v, std::ostream& out)
{ {
cmXCodeObject::Indent(1, out); cmXCodeObject::Indent(1, out);
out << "objects = {\n"; out << "objects = {\n";
+5 -3
View File
@@ -6,6 +6,7 @@
#include "cmConfigure.h" // IWYU pragma: keep #include "cmConfigure.h" // IWYU pragma: keep
#include <iosfwd> #include <iosfwd>
#include <memory>
#include <vector> #include <vector>
#include "cmXCodeObject.h" #include "cmXCodeObject.h"
@@ -15,8 +16,9 @@ class cmXCode21Object : public cmXCodeObject
public: public:
cmXCode21Object(PBXType ptype, Type type); cmXCode21Object(PBXType ptype, Type type);
void PrintComment(std::ostream&) override; void PrintComment(std::ostream&) override;
static void PrintList(std::vector<cmXCodeObject*> const&, std::ostream& out, static void PrintList(std::vector<std::unique_ptr<cmXCodeObject>> const&,
PBXType t); 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);
}; };
#endif #endif