everything but calling the destructor correctly is handled

This commit is contained in:
Pagwin 2026-01-16 16:00:41 -05:00
parent 6ef58754d2
commit deca24be12
No known key found for this signature in database
GPG key ID: 81137023740CA260
3 changed files with 35 additions and 11 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
*.o *.o
test test
test_gdb

View file

@ -1,3 +1,5 @@
CXX=clang++ CXX=clang++
test: SharedPtr_test.cpp SharedPtr.hpp test: SharedPtr_test.cpp SharedPtr.hpp
$(CXX) SharedPtr_test.cpp -Wall -fsanitize=address -g -o test $(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

View file

@ -28,8 +28,9 @@ public:
template <typename T> class ControlImpl : public Control { template <typename T> class ControlImpl : public Control {
T *val; T *val;
void destroy() override { void destroy() override {
delete val; // std::cerr << this << '\t' << val << std::endl;
delete this; // delete val;
// delete this;
} }
public: public:
@ -51,11 +52,15 @@ public:
} }
SharedPtr(const SharedPtr &p) : raw{p.raw}, underlying{p.underlying} { SharedPtr(const SharedPtr &p) : raw{p.raw}, underlying{p.underlying} {
this->underlying->increment(); if (this->underlying != nullptr) {
this->underlying->increment();
}
} }
template <typename U> template <typename U>
SharedPtr(const SharedPtr<U> &p) : raw{p.raw}, underlying{p.underlying} { SharedPtr(const SharedPtr<U> &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} { SharedPtr(SharedPtr &&p) : raw{p.raw}, underlying{p.underlying} {
@ -74,9 +79,13 @@ public:
return *this; return *this;
} }
this->raw = rhs.raw; this->raw = rhs.raw;
this->underlying->decrement(); if (this->underlying != nullptr) {
this->underlying->decrement();
}
this->underlying = rhs.underlying; this->underlying = rhs.underlying;
this->underlying->increment(); if (this->underlying != nullptr) {
this->underlying->increment();
}
return *this; return *this;
} }
template <typename U> SharedPtr<T> &operator=(const SharedPtr<U> &rhs) { template <typename U> SharedPtr<T> &operator=(const SharedPtr<U> &rhs) {
@ -84,9 +93,13 @@ public:
return *this; return *this;
} }
this->raw = rhs.raw; this->raw = rhs.raw;
this->underlying->decrement(); if (this->underlying != nullptr) {
this->underlying->decrement();
}
this->underlying = rhs.underlying; this->underlying = rhs.underlying;
this->underlying->increment(); if (this->underlying != nullptr) {
this->underlying->increment();
}
return *this; return *this;
} }
@ -95,7 +108,9 @@ public:
return *this; return *this;
} }
this->raw = rhs.raw; this->raw = rhs.raw;
this->underlying->decrement(); if (this->underlying != nullptr) {
this->underlying->decrement();
}
this->underlying = rhs.underlying; this->underlying = rhs.underlying;
rhs.raw = nullptr; rhs.raw = nullptr;
rhs.underlying = nullptr; rhs.underlying = nullptr;
@ -105,12 +120,18 @@ public:
return *this; return *this;
} }
this->raw = rhs.raw; this->raw = rhs.raw;
this->underlying->decrement(); if (this->underlying != nullptr) {
this->underlying->decrement();
}
this->underlying = rhs.underlying; this->underlying = rhs.underlying;
rhs.raw = nullptr; rhs.raw = nullptr;
rhs.underlying = nullptr; rhs.underlying = nullptr;
} }
~SharedPtr() { this->underlying->decrement(); } ~SharedPtr() {
if (this->underlying != nullptr) {
this->underlying->decrement();
}
}
void reset() { void reset() {
this->raw = nullptr; this->raw = nullptr;