Hi,
When I use TArray with iterator, and selectively remove some of the elements, editor will crash during execution.
source code:
.h
class TestElem {
public:
int ID;
TestElem(int idx);
~TestElem();
};
TArray<TestElem *> TestTArray2;
.cpp
for (i = 0; i < 10; ++i)
{
TestTArray2.Add(new TestElem(i));
}
for (auto it : TestTArray2)
{
UE_LOG(LogSanguo, Warning, TEXT("index for tarray2 %d"), TestTArray2.Find(it));
if (it->ID == 3)
{
// TestTArray2.RemoveAll([&it](TestElem *& Element) { return Element->ID == it->ID; }); --> cause crash
// TestTArray2.RemoveAt(TestTArray2.Find(it), 1, true); --> cause crash, in the code I comment them all
}
UE_LOG(LogSanguo, Warning, TEXT("remove done for tarray2"));
}
All remove* has issues, takes Remove() as an example, in the array.h, it looks like:
int32 Remove(const ElementType& Item)
{
CheckAddress(&Item);
// Element is non-const to preserve compatibility with existing code with a non-const operator==() member function
return RemoveAll([&Item](ElementType& Element) { return Element == Item; });
}
the checkAddress function here is to “Checks that the specified address is not part of an element within the container.” Certainly it is in the range, and I want to remove it. So it will fail…
I believe removesingle() and remove*() has similar issue…
Could you help to confirm it?
BRs