commit 6544ca2e7bf353ff58cecdd859e570c639fd4161
parent af6c439ab1462502f8527887927b7425d5958cb2
Author: Christos Margiolis <christos@margiolis.net>
Date: Thu, 14 May 2020 03:31:15 +0300
set pointers to nullptr in destructors, pending double free bug in 2.4
Diffstat:
15 files changed, 69 insertions(+), 24 deletions(-)
diff --git a/assignment-2.2-classes/classes.cpp b/assignment-2.2-classes/classes.cpp
@@ -65,8 +65,16 @@ Student::Student(const Student& s)
Student::~Student()
{
- if (this->AM != nullptr) delete[] this->AM;
- if (this->AM != nullptr) delete[] this->grades;
+ if (this->AM != nullptr)
+ {
+ delete[] this->AM;
+ this->AM = nullptr;
+ }
+ if (this->AM != nullptr)
+ {
+ delete[] this->grades;
+ this->grades = nullptr;
+ }
}
const char *
diff --git a/assignment-2.3-operoverloading/bin/operoverloading b/assignment-2.3-operoverloading/bin/operoverloading
Binary files differ.
diff --git a/assignment-2.3-operoverloading/obj/main.o b/assignment-2.3-operoverloading/obj/main.o
Binary files differ.
diff --git a/assignment-2.3-operoverloading/obj/student.o b/assignment-2.3-operoverloading/obj/student.o
Binary files differ.
diff --git a/assignment-2.3-operoverloading/src/student.cpp b/assignment-2.3-operoverloading/src/student.cpp
@@ -45,9 +45,21 @@ Student::Student(const Student& s)
Student::~Student()
{
- if (this->AM != nullptr) delete[] this->AM;
- if (this->grades != nullptr) delete[] this->grades;
- if (this->sc != nullptr) delete[] this->sc;
+ if (this->AM != nullptr)
+ {
+ delete[] this->AM;
+ this->AM = nullptr;
+ }
+ if (this->grades != nullptr)
+ {
+ delete[] this->grades;
+ this->grades = nullptr;
+ }
+ if (this->sc != nullptr)
+ {
+ delete[] this->sc;
+ this->sc = nullptr;
+ }
}
void
diff --git a/assignment-2.4-inheritance/bin/inheritance b/assignment-2.4-inheritance/bin/inheritance
Binary files differ.
diff --git a/assignment-2.4-inheritance/obj/app.o b/assignment-2.4-inheritance/obj/app.o
Binary files differ.
diff --git a/assignment-2.4-inheritance/obj/appsystem.o b/assignment-2.4-inheritance/obj/appsystem.o
Binary files differ.
diff --git a/assignment-2.4-inheritance/obj/main.o b/assignment-2.4-inheritance/obj/main.o
Binary files differ.
diff --git a/assignment-2.4-inheritance/src/app.cpp b/assignment-2.4-inheritance/src/app.cpp
@@ -23,11 +23,22 @@ App::App(const App& a)
App::~App()
{
- if (serialnum != nullptr) delete[] serialnum;
+ if (serialnum != nullptr)
+ {
+ delete[] serialnum;
+ serialnum = nullptr;
+ }
if (!reviews.empty())
{
- for (auto&& rev :reviews)
- delete rev;
+ for (auto&& rev : reviews)
+ {
+ if (rev != nullptr)
+ {
+ // double free bug
+ delete rev;
+ rev = nullptr;
+ }
+ }
reviews.clear();
}
}
diff --git a/assignment-2.4-inheritance/src/appsystem.h b/assignment-2.4-inheritance/src/appsystem.h
@@ -84,7 +84,7 @@ AppSystem::parse(std::ifstream& f)
if (!manfs.empty())
{
- for (auto& man : manfs)
+ for (const auto& man : manfs)
{
if (man->get_name() == manf)
{
@@ -112,7 +112,7 @@ AppSystem::parse(std::ifstream& f)
if (!manfs.empty())
{
- for (auto& man : manfs)
+ for (const auto& man : manfs)
{
if (man->get_name() == manf)
{
@@ -171,7 +171,7 @@ AppSystem::import_data(const char *fpath)
std::getline(f, username, ',');
std::getline(f, comment);
if (f.eof()) return true;
- for (auto& app : apps)
+ for (auto&& app : apps)
if (appname == app->get_name())
app->addrev(new Review(std::stoi(stars), username, comment));
}
@@ -202,7 +202,7 @@ AppSystem::export_data(const char *fpath)
if (std::is_same<T, Manufacturer>::value)
{
f << "SN,Name,Email\n";
- for (auto& manf : manfs)
+ for (const auto& manf : manfs)
f << manf->get_serialnum() << ',' <<
manf->get_name() << ',' <<
manf->get_email() << std::endl;
@@ -210,7 +210,7 @@ AppSystem::export_data(const char *fpath)
else if (std::is_same<T, App>::value)
{
f << "Type,SN,Name,OS,Manf,Price,Genre,Online,Extensions\n";
- for (auto& app : apps)
+ for (const auto& app : apps)
{
Manufacturer manf = app->get_manf();
Game *o = dynamic_cast<Game *>(app);
@@ -234,11 +234,11 @@ AppSystem::export_data(const char *fpath)
else if (std::is_same<T, Review>::value)
{
f << "AppName,Stars,Username,Comment\n";
- for (auto& app : apps)
+ for (const auto& app : apps)
{
const std::vector<Review *> revs = app->get_revs();
if (!revs.empty())
- for (auto& rev : revs)
+ for (const auto& rev : revs)
f <<
app->get_name() << ',' <<
rev->get_stars() << ',' <<
@@ -259,11 +259,18 @@ AppSystem::export_data(const char *fpath)
template<typename T> void
AppSystem::dealloc(std::vector<T *>& vec)
{
- for (auto& v : vec)
- if (v != nullptr)
- delete v;
if (!vec.empty())
+ {
+ for (auto&& v : vec)
+ {
+ if (v != nullptr)
+ {
+ delete v;
+ v = nullptr;
+ }
+ }
vec.clear();
+ }
}
#endif /* APPSYSTEM_H */
diff --git a/assignment-2.4-inheritance/src/main.cpp b/assignment-2.4-inheritance/src/main.cpp
@@ -43,7 +43,7 @@ operator<< (std::ostream& stream, const AppSystem& sys)
std::left << std::setw(10) << "Online" <<
std::left << std::setw(25) << "Extensions" << std::endl << std::endl;
std::vector<App *> apps = sys.get_apps();
- for (auto& app : apps)
+ for (const auto& app : apps)
app->print(stream);
return stream;
}
@@ -94,8 +94,8 @@ getapps(const AppSystem& sys)
{
const std::vector<Office *>& fapps = sys.get_freeapps();
const std::vector<Game *>& ggames = sys.get_goodgames();
- for (auto& fapp : fapps)
+ for (const auto& fapp : fapps)
std::cout << fapp->get_name() << std::endl;
- for (auto& ggame : ggames)
+ for (const auto& ggame : ggames)
std::cout << ggame->get_name() << std::endl;
}
diff --git a/assignment-2.5-spreadsheets/bin/spreadsheets b/assignment-2.5-spreadsheets/bin/spreadsheets
Binary files differ.
diff --git a/assignment-2.5-spreadsheets/obj/datahandler.o b/assignment-2.5-spreadsheets/obj/datahandler.o
Binary files differ.
diff --git a/assignment-2.5-spreadsheets/src/datahandler.h b/assignment-2.5-spreadsheets/src/datahandler.h
@@ -102,11 +102,18 @@ DataHandler::import_data(const char *fpath)
template<typename T> void
DataHandler::dealloc(std::map<lab::xstring, T *>& vec)
{
- for (auto&& v : vec)
- if (v.second != nullptr)
- delete v.second;
if (!vec.empty())
+ {
+ for (auto&& v : vec)
+ {
+ if (v.second != nullptr)
+ {
+ delete v.second;
+ v.second = nullptr;
+ }
+ }
vec.clear();
+ }
}
#endif /* DATA_HANDLER_H */