From 13b48593792830b20f5b4d888d9e0c08fa7c8359 Mon Sep 17 00:00:00 2001 From: tbobo Date: Thu, 15 Feb 2024 05:26:14 -0800 Subject: [PATCH] Remove redundant null check before delete in wrapper_class --- src/wrapper_class.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/wrapper_class.cpp b/src/wrapper_class.cpp index 1ab3aff..745e242 100644 --- a/src/wrapper_class.cpp +++ b/src/wrapper_class.cpp @@ -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 @@ -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;