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
test
test_gdb

View file

@ -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

View file

@ -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;