Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant null check before delete in wrapper_class #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/wrapper_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,9 @@ class IntPtrManager {
// resource that it is managing; in this case, the destructor deletes
// the pointer!
~IntPtrManager() {
// Note that since the move constructor marks objects invalid by setting
// their ptr_ value to nullptr, we have to account for this in the
// destructor. We don't want to be calling delete on a nullptr!
if (ptr_) {
delete ptr_;
}
// Note that this is safe even when the pointer is null!
// The language guarantees that delete does nothing when passed a null pointer.
delete ptr_;
}

// Move constructor for this wrapper class. Note that after the move
Expand All @@ -80,9 +77,7 @@ class IntPtrManager {
if (ptr_ == other.ptr_) {
return *this;
}
if (ptr_) {
delete ptr_;
}
delete ptr_;
ptr_ = other.ptr_;
other.ptr_ = nullptr;
return *this;
Expand Down