AK: Unref old m_data in String's move assignment
We were overridding the data pointer without unreffing it, causing a memory leak when assigning a String.
This commit is contained in:
parent
741138c585
commit
58f5deba70
Notes:
sideshowbarker
2024-07-17 07:38:17 +09:00
Author: https://github.com/sppmacd Commit: https://github.com/SerenityOS/serenity/commit/58f5deba70 Pull-request: https://github.com/SerenityOS/serenity/pull/16385
2 changed files with 10 additions and 0 deletions
|
@ -181,6 +181,9 @@ String::String(String&& other)
|
|||
|
||||
String& String::operator=(String&& other)
|
||||
{
|
||||
if (!is_short_string())
|
||||
m_data->unref();
|
||||
|
||||
m_data = exchange(other.m_data, nullptr);
|
||||
return *this;
|
||||
}
|
||||
|
|
|
@ -24,6 +24,13 @@ TEST_CASE(construct_empty)
|
|||
EXPECT_EQ(empty, ""sv);
|
||||
}
|
||||
|
||||
TEST_CASE(move_assignment)
|
||||
{
|
||||
String string1 = MUST(String::from_utf8("hello"sv));
|
||||
string1 = MUST(String::from_utf8("friends!"sv));
|
||||
EXPECT_EQ(string1, "friends!"sv);
|
||||
}
|
||||
|
||||
TEST_CASE(short_strings)
|
||||
{
|
||||
#ifdef AK_ARCH_64_BIT
|
||||
|
|
Loading…
Add table
Reference in a new issue