From 2bb0ddee5b2165128339f37b8991a87d12b7ad19 Mon Sep 17 00:00:00 2001 From: Redstone1024 <2824517378@qq.com> Date: Sat, 2 Nov 2024 23:07:05 +0800 Subject: [PATCH] fix(containers): fix TArray lost all elements after move assignment --- Redcraft.Utility/Source/Private/Testing/ContainersTesting.cpp | 4 ++-- Redcraft.Utility/Source/Public/Containers/Array.h | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Redcraft.Utility/Source/Private/Testing/ContainersTesting.cpp b/Redcraft.Utility/Source/Private/Testing/ContainersTesting.cpp index 785c44f..48c43be 100644 --- a/Redcraft.Utility/Source/Private/Testing/ContainersTesting.cpp +++ b/Redcraft.Utility/Source/Private/Testing/ContainersTesting.cpp @@ -29,7 +29,7 @@ void TestArrayTemplate() TArray ArrayB(4); TArray ArrayC(4, 4); TArray ArrayD(ArrayC); - TArray ArrayE(MoveTemp(ArrayB)); + TArray ArrayE(MoveTemp(ArrayC)); TArray ArrayF({ 0, 1, 2, 3 }); TArray ArrayG; @@ -40,9 +40,9 @@ void TestArrayTemplate() ArrayH = MoveTemp(ArrayE); ArrayI = { 0, 1, 2, 3 }; - always_check((ArrayC == TArray({ 4, 4, 4, 4 }))); always_check((ArrayD == TArray({ 4, 4, 4, 4 }))); always_check((ArrayG == TArray({ 4, 4, 4, 4 }))); + always_check((ArrayH == TArray({ 4, 4, 4, 4 }))); always_check((ArrayF == TArray({ 0, 1, 2, 3 }))); always_check((ArrayI == TArray({ 0, 1, 2, 3 }))); } diff --git a/Redcraft.Utility/Source/Public/Containers/Array.h b/Redcraft.Utility/Source/Public/Containers/Array.h index 389c2cc..d21a27d 100644 --- a/Redcraft.Utility/Source/Public/Containers/Array.h +++ b/Redcraft.Utility/Source/Public/Containers/Array.h @@ -189,7 +189,9 @@ public: Memory::Destruct(Impl.Pointer, Num()); Impl->Deallocate(Impl.Pointer); - Impl.Pointer = InValue.Impl.Pointer; + Impl.ArrayNum = InValue.Num(); + Impl.ArrayMax = InValue.Max(); + Impl.Pointer = InValue.Impl.Pointer; InValue.Impl.ArrayNum = 0; InValue.Impl.ArrayMax = InValue.Impl->CalculateSlackReserve(InValue.Num());