diff --git a/.gitignore b/.gitignore index 76758b0..bd9cf3e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.o test +test_gdb diff --git a/Makefile b/Makefile index 20c0640..5239577 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ CXX=clang++ test: SharedPtr_test.cpp SharedPtr.hpp $(CXX) SharedPtr_test.cpp -Wall -fsanitize=address -g -o test +test_gdb: SharedPtr_test.cpp SharedPtr.hpp + $(CXX) SharedPtr_test.cpp -Wall -g -o test_gdb diff --git a/SharedPtr.hpp b/SharedPtr.hpp index 92f4872..d2324da 100644 --- a/SharedPtr.hpp +++ b/SharedPtr.hpp @@ -28,8 +28,9 @@ public: template class ControlImpl : public Control { T *val; void destroy() override { - delete val; - delete this; + // std::cerr << this << '\t' << val << std::endl; + // delete val; + // delete this; } public: @@ -51,11 +52,15 @@ public: } SharedPtr(const SharedPtr &p) : raw{p.raw}, underlying{p.underlying} { - this->underlying->increment(); + if (this->underlying != nullptr) { + this->underlying->increment(); + } } template SharedPtr(const SharedPtr &p) : raw{p.raw}, underlying{p.underlying} { - this->underlying->increment(); + if (this->underlying != nullptr) { + this->underlying->increment(); + } } SharedPtr(SharedPtr &&p) : raw{p.raw}, underlying{p.underlying} { @@ -74,9 +79,13 @@ public: return *this; } this->raw = rhs.raw; - this->underlying->decrement(); + if (this->underlying != nullptr) { + this->underlying->decrement(); + } this->underlying = rhs.underlying; - this->underlying->increment(); + if (this->underlying != nullptr) { + this->underlying->increment(); + } return *this; } template SharedPtr &operator=(const SharedPtr &rhs) { @@ -84,9 +93,13 @@ public: return *this; } this->raw = rhs.raw; - this->underlying->decrement(); + if (this->underlying != nullptr) { + this->underlying->decrement(); + } this->underlying = rhs.underlying; - this->underlying->increment(); + if (this->underlying != nullptr) { + this->underlying->increment(); + } return *this; } @@ -95,7 +108,9 @@ public: return *this; } this->raw = rhs.raw; - this->underlying->decrement(); + if (this->underlying != nullptr) { + this->underlying->decrement(); + } this->underlying = rhs.underlying; rhs.raw = nullptr; rhs.underlying = nullptr; @@ -105,12 +120,18 @@ public: return *this; } this->raw = rhs.raw; - this->underlying->decrement(); + if (this->underlying != nullptr) { + this->underlying->decrement(); + } this->underlying = rhs.underlying; rhs.raw = nullptr; rhs.underlying = nullptr; } - ~SharedPtr() { this->underlying->decrement(); } + ~SharedPtr() { + if (this->underlying != nullptr) { + this->underlying->decrement(); + } + } void reset() { this->raw = nullptr;