Fixed memory leaks

This commit is contained in:
Leonetienne 2021-06-02 17:14:53 +02:00
parent c412e879a1
commit 131520e1ef
15 changed files with 84 additions and 56 deletions

Binary file not shown.

View File

@ -6,6 +6,7 @@ class FloatValue : public Value
{ {
public: public:
FloatValue(const long double& value); FloatValue(const long double& value);
~FloatValue() override {};
//! Will return a deeopopy of this object //! Will return a deeopopy of this object
Value* Deepcopy() const override; Value* Deepcopy() const override;

View File

@ -5,7 +5,6 @@
#include "StringValue.h" #include "StringValue.h"
#include "ListValue.h" #include "ListValue.h"
#include "StringTools.h" #include "StringTools.h"
#include <iostream>
Hazelnupp::Hazelnupp() Hazelnupp::Hazelnupp()
{ {
@ -18,6 +17,16 @@ Hazelnupp::Hazelnupp(const int argc, const char* const* argv)
return; return;
} }
Hazelnupp::~Hazelnupp()
{
for (auto& it : parameters)
delete it.second;
parameters.clear();
return;
}
void Hazelnupp::Parse(const int argc, const char* const* argv) void Hazelnupp::Parse(const int argc, const char* const* argv)
{ {
// Populate raw arguments // Populate raw arguments
@ -42,8 +51,6 @@ void Hazelnupp::Parse(const int argc, const char* const* argv)
i++; i++;
} }
std::cout << "Terminated." << std::endl;
return; return;
} }
@ -64,17 +71,24 @@ std::size_t Hazelnupp::ParseNextParameter(const std::size_t parIndex, Parameter*
} }
Value* parsedVal = ParseValue(values); Value* parsedVal = ParseValue(values);
if (parsedVal != nullptr)
{
out_Par = new Parameter(key, parsedVal); out_Par = new Parameter(key, parsedVal);
delete parsedVal; delete parsedVal;
parsedVal = nullptr; parsedVal = nullptr;
}
else
throw std::runtime_error("Unable to parse parameter!");
return i; return i;
} }
void Hazelnupp::PopulateRawArgs(const int argc, const char* const* argv) void Hazelnupp::PopulateRawArgs(const int argc, const char* const* argv)
{ {
rawArgs.clear();
rawArgs.reserve(argc); rawArgs.reserve(argc);
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++)
rawArgs.emplace_back(std::string(argv[i])); rawArgs.emplace_back(std::string(argv[i]));

View File

@ -9,6 +9,8 @@ public:
Hazelnupp(); Hazelnupp();
Hazelnupp(const int argc, const char* const* argv); Hazelnupp(const int argc, const char* const* argv);
~Hazelnupp();
//! Will parse command line arguments //! Will parse command line arguments
void Parse(const int argc, const char* const* argv); void Parse(const int argc, const char* const* argv);
@ -51,7 +53,4 @@ private:
std::unordered_map<std::string, std::string> abbreviations; std::unordered_map<std::string, std::string> abbreviations;
std::vector<std::string> rawArgs; std::vector<std::string> rawArgs;
// EvaluateValues
friend class ListValue;
}; };

View File

@ -5,6 +5,7 @@ class IntValue : public Value
{ {
public: public:
IntValue(const long long int& value); IntValue(const long long int& value);
~IntValue() override {};
//! Will return a deeopopy of this object //! Will return a deeopopy of this object
Value* Deepcopy() const override; Value* Deepcopy() const override;

View File

@ -1,5 +1,4 @@
#include "ListValue.h" #include "ListValue.h"
#include "Hazelnupp.h"
#include <sstream> #include <sstream>
ListValue::ListValue() : ListValue::ListValue() :

View File

@ -6,7 +6,7 @@ class ListValue : public Value
{ {
public: public:
ListValue(); ListValue();
~ListValue(); ~ListValue() override;
//! Will return a deeopopy of this object //! Will return a deeopopy of this object
Value* Deepcopy() const override; Value* Deepcopy() const override;

View File

@ -8,6 +8,14 @@ Parameter::Parameter(const std::string& key, const ::Value* value)
return; return;
} }
Parameter::~Parameter()
{
delete value;
value = nullptr;
return;
}
const std::string& Parameter::Key() const const std::string& Parameter::Key() const
{ {
return key; return key;

View File

@ -7,6 +7,7 @@ class Parameter
{ {
public: public:
explicit Parameter(const std::string& key, const Value* value); explicit Parameter(const std::string& key, const Value* value);
~Parameter();
//! Will return the key of this parameter //! Will return the key of this parameter
const std::string& Key() const; const std::string& Key() const;

View File

@ -9,7 +9,7 @@ bool StringTools::Contains(const std::string& str, const char c)
return false; return false;
} }
std::string StringTools::Replace(const std::string str, const char find, const std::string subst) std::string StringTools::Replace(const std::string& str, const char find, const std::string& subst)
{ {
std::stringstream ss; std::stringstream ss;
@ -22,7 +22,7 @@ std::string StringTools::Replace(const std::string str, const char find, const s
return ss.str(); return ss.str();
} }
std::string StringTools::Replace(const std::string str, const std::string find, const std::string subst) std::string StringTools::Replace(const std::string& str, const std::string& find, const std::string& subst)
{ {
if (find.length() == 0) return str; if (find.length() == 0) return str;
@ -51,7 +51,7 @@ std::string StringTools::Replace(const std::string str, const std::string find,
} }
bool StringTools::IsNumeric(const std::string str, const bool allowDecimalPoint) bool StringTools::IsNumeric(const std::string& str, const bool allowDecimalPoint)
{ {
if (str.length() == 0) return false; if (str.length() == 0) return false;
@ -77,7 +77,7 @@ bool StringTools::IsNumeric(const std::string str, const bool allowDecimalPoint)
return digitCount > 0; return digitCount > 0;
} }
bool StringTools::ParseNumber(const std::string str, bool& out_isInt, long double& out_number) bool StringTools::ParseNumber(const std::string& str, bool& out_isInt, long double& out_number)
{ {
bool isNormalNum = true; bool isNormalNum = true;
@ -124,14 +124,14 @@ bool StringTools::ParseNumber(const std::string str, bool& out_isInt, long doubl
return true; return true;
} }
std::vector<std::string> StringTools::SplitString(const std::string str, const char delimiter) std::vector<std::string> StringTools::SplitString(const std::string& str, const char delimiter)
{ {
if (str.length() == 0) return std::vector<std::string>(); if (str.length() == 0) return std::vector<std::string>();
return SplitString(str, delimiter); return SplitString(str, delimiter);
} }
std::vector<std::string> StringTools::SplitString(const std::string str, const std::string delimiter) std::vector<std::string> StringTools::SplitString(const std::string& str, const std::string& delimiter)
{ {
if (str.length() == 0) return std::vector<std::string>(); if (str.length() == 0) return std::vector<std::string>();
@ -172,7 +172,7 @@ std::vector<std::string> StringTools::SplitString(const std::string str, const s
return parts; return parts;
} }
std::string StringTools::ToLower(const std::string str) std::string StringTools::ToLower(const std::string& str)
{ {
std::stringstream ss; std::stringstream ss;
for (std::size_t i = 0; i < str.length(); i++) for (std::size_t i = 0; i < str.length(); i++)

View File

@ -14,25 +14,25 @@ public:
static bool Contains(const std::string& str, const char c); static bool Contains(const std::string& str, const char c);
//! Will replace a part of a string with another string //! Will replace a part of a string with another string
static std::string Replace(const std::string str, const char find, const std::string subst); static std::string Replace(const std::string& str, const char find, const std::string& subst);
//! Will replace a part of a string with another string //! Will replace a part of a string with another string
static std::string Replace(const std::string str, const std::string find, const std::string subst); static std::string Replace(const std::string& str, const std::string& find, const std::string& subst);
//! Will return true if the given string consists only of digits (including signage) //! Will return true if the given string consists only of digits (including signage)
static bool IsNumeric(const std::string str, const bool allowDecimalPoint = false); static bool IsNumeric(const std::string& str, const bool allowDecimalPoint = false);
//! Will convert the number in str to a number. //! Will convert the number in str to a number.
//! Returns wether or not the operation was successful. //! Returns wether or not the operation was successful.
//! Also returns wether the number is an integer, or floating point. If int, cast out_number to int. //! Also returns wether the number is an integer, or floating point. If int, cast out_number to int.
static bool ParseNumber(const std::string str, bool& out_isInt, long double& out_number); static bool ParseNumber(const std::string& str, bool& out_isInt, long double& out_number);
//! Will split a string by a delimiter char. The delimiter will be excluded! //! Will split a string by a delimiter char. The delimiter will be excluded!
static std::vector<std::string> SplitString(const std::string str, const char delimiter); static std::vector<std::string> SplitString(const std::string& str, const char delimiter);
//! Will split a string by a delimiter string. The delimiter will be excluded! //! Will split a string by a delimiter string. The delimiter will be excluded!
static std::vector<std::string> SplitString(const std::string str, const std::string delimiter); static std::vector<std::string> SplitString(const std::string& str, const std::string& delimiter);
//! Will make a string all lower-case //! Will make a string all lower-case
static std::string ToLower(const std::string str); static std::string ToLower(const std::string& str);
}; };

View File

@ -6,6 +6,7 @@ class StringValue : public Value
{ {
public: public:
StringValue(const std::string& value); StringValue(const std::string& value);
~StringValue() override {};
//! Will return a deeopopy of this object //! Will return a deeopopy of this object
Value* Deepcopy() const override; Value* Deepcopy() const override;

View File

@ -6,6 +6,8 @@
class Value class Value
{ {
public: public:
virtual ~Value() {};
//! Will return a deeopopy of this object //! Will return a deeopopy of this object
virtual Value* Deepcopy() const = 0; virtual Value* Deepcopy() const = 0;

View File

@ -5,6 +5,7 @@ class VoidValue : public Value
{ {
public: public:
VoidValue(); VoidValue();
~VoidValue() override {};
//! Will return a deeopopy of this object //! Will return a deeopopy of this object
Value* Deepcopy() const override; Value* Deepcopy() const override;

View File

@ -1,42 +1,43 @@
#include <iostream> #include <iostream>
#include <array>
#include "Hazelnupp.h" #include "Hazelnupp.h"
#include "IntValue.h" #include "IntValue.h"
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
std::string arg0 = "meinpfad"; while (1)
std::string arg1 = "-w"; {
std::string arg2 = "6669"; std::vector<const char*> testArgv = {
std::string arg3 = "--alfred"; "meinpfad",
std::string arg4 = "banane7"; "-w",
"hallo",
std::array<const char*, 5> testArgv = { "--alfred",
arg0.data(), "apfel",
arg1.data(), "banane",
arg2.data(), "triangle",
arg3.data(), "--numbers",
arg4.data() "1",
"2",
"3",
"4",
"5",
}; };
Hazelnupp args; Hazelnupp args;
args.RegisterAbbreviation("-w", "--word"); args.RegisterAbbreviation("-w", "--word");
//args.Parse(testArgv.size(), testArgv.data()); args.Parse(testArgv.size(), testArgv.data());
args.Parse(argc, argv); //args.Parse(argc, argv);
if (args.HasParam("--word")) if (args.HasParam("--word"))
{ {
int i = args["--word"]->GetInt32(); std::cout << *args["--word"] << std::endl;
std::cout << i << std::endl;
} }
else else
{ {
std::cout << "No --word!" << std::endl; std::cout << "No --word!" << std::endl;
} }
}
//std::cout << args.GetExecutableName() << std::endl;
return 0; return 0;
} }