diff --git a/src/jemalloc_cpp.cpp b/src/jemalloc_cpp.cpp index 5cecfdbf..984c944b 100644 --- a/src/jemalloc_cpp.cpp +++ b/src/jemalloc_cpp.cpp @@ -118,11 +118,17 @@ void operator delete[](void *ptr, const std::nothrow_t &) noexcept void operator delete(void *ptr, std::size_t size) noexcept { + if (unlikely(ptr == nullptr)) { + return; + } je_sdallocx(ptr, size, /*flags=*/0); } void operator delete[](void *ptr, std::size_t size) noexcept { + if (unlikely(ptr == nullptr)) { + return; + } je_sdallocx(ptr, size, /*flags=*/0); } diff --git a/test/integration/cpp/basic.cpp b/test/integration/cpp/basic.cpp index 4a87a3ba..b208e1d1 100644 --- a/test/integration/cpp/basic.cpp +++ b/test/integration/cpp/basic.cpp @@ -6,6 +6,16 @@ TEST_BEGIN(test_basic) auto foo = new long(4); assert_ptr_not_null(foo, "Unexpected new[] failure"); delete foo; + // Test nullptr handling. + foo = nullptr; + delete foo; + + auto bar = new long; + assert_ptr_not_null(bar, "Unexpected new failure"); + delete bar; + // Test nullptr handling. + bar = nullptr; + delete bar; } TEST_END