Fix missing destructor call and simplify code slightly

Although the destructor call is a no-op, it is more correct to
have it to ensure the compiler knows the lifetime of that object
ended.
This commit is contained in:
Maxime Coste 2024-02-28 21:48:58 +11:00
parent b8a151ab46
commit 74ce9f3cfe

View File

@ -37,7 +37,9 @@ private:
return; return;
if (r->refcount & interned_flag) if (r->refcount & interned_flag)
Registry::instance().remove(r->strview()); Registry::instance().remove(r->strview());
StringData::operator delete(r, sizeof(StringData) + r->length + 1); auto alloc_len = sizeof(StringData) + r->length + 1;
r->~StringData();
operator delete(r, alloc_len);
} }
static void ptr_moved(StringData*, void*, void*) noexcept {} static void ptr_moved(StringData*, void*, void*) noexcept {}
}; };
@ -60,11 +62,11 @@ public:
static Ptr create(ConvertibleTo<StringView> auto&&... strs) static Ptr create(ConvertibleTo<StringView> auto&&... strs)
{ {
const int len = ((int)StringView{strs}.length() + ...); const int len = ((int)StringView{strs}.length() + ...);
void* ptr = StringData::operator new(sizeof(StringData) + len + 1); void* ptr = operator new(sizeof(StringData) + len + 1);
auto* res = new (ptr) StringData(len); auto* res = new (ptr) StringData(len);
auto* data = reinterpret_cast<char*>(res + 1); auto* data = reinterpret_cast<char*>(res + 1);
auto append = [&](StringView str) { auto append = [&](StringView str) {
if (str.empty()) // memccpy(..., nullptr, 0) is UB if (str.empty()) // memcpy(..., nullptr, 0) is UB
return; return;
memcpy(data, str.begin(), (size_t)str.length()); memcpy(data, str.begin(), (size_t)str.length());
data += (int)str.length(); data += (int)str.length();