everything but calling the destructor correctly is handled
This commit is contained in:
parent
6ef58754d2
commit
deca24be12
3 changed files with 35 additions and 11 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
|||
*.o
|
||||
test
|
||||
test_gdb
|
||||
|
|
|
|||
2
Makefile
2
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
|
||||
|
|
|
|||
|
|
@ -28,8 +28,9 @@ public:
|
|||
template <typename T> 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,12 +52,16 @@ public:
|
|||
}
|
||||
|
||||
SharedPtr(const SharedPtr &p) : raw{p.raw}, underlying{p.underlying} {
|
||||
if (this->underlying != nullptr) {
|
||||
this->underlying->increment();
|
||||
}
|
||||
}
|
||||
template <typename U>
|
||||
SharedPtr(const SharedPtr<U> &p) : raw{p.raw}, underlying{p.underlying} {
|
||||
if (this->underlying != nullptr) {
|
||||
this->underlying->increment();
|
||||
}
|
||||
}
|
||||
|
||||
SharedPtr(SharedPtr &&p) : raw{p.raw}, underlying{p.underlying} {
|
||||
p.raw = nullptr;
|
||||
|
|
@ -74,9 +79,13 @@ public:
|
|||
return *this;
|
||||
}
|
||||
this->raw = rhs.raw;
|
||||
if (this->underlying != nullptr) {
|
||||
this->underlying->decrement();
|
||||
}
|
||||
this->underlying = rhs.underlying;
|
||||
if (this->underlying != nullptr) {
|
||||
this->underlying->increment();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
template <typename U> SharedPtr<T> &operator=(const SharedPtr<U> &rhs) {
|
||||
|
|
@ -84,9 +93,13 @@ public:
|
|||
return *this;
|
||||
}
|
||||
this->raw = rhs.raw;
|
||||
if (this->underlying != nullptr) {
|
||||
this->underlying->decrement();
|
||||
}
|
||||
this->underlying = rhs.underlying;
|
||||
if (this->underlying != nullptr) {
|
||||
this->underlying->increment();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
|
@ -95,7 +108,9 @@ public:
|
|||
return *this;
|
||||
}
|
||||
this->raw = rhs.raw;
|
||||
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;
|
||||
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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue