[imperfection]Why TArray::Empty() would invoke deconstructor of very element?

If the element allocated by user not by engine, I think control rights of memory must be given to user, so TArray::Empty() shouldn’t invoke deconstructor of elements.

Besides, the function clear() of **STL **constainer also give the control rights of memory to user, deconstructor of elements are not executed in it. I think the API of STL are pretty standard. so I thing Empty() of UE4 container is imperfect. I hope Epic to improve this API in the future. thx~

In STL clearing a vector does invoke the destructor of non-pointer types.

you’re right, it’s my wrong.

sorry, I need to reclaim my opinion: In STL clearing a vector does invoke the destructor of non-pointer types, but Empty of UE4 container also invoke the destructor of pointer types.

Are you using smart pointers for the contained objects? Smart pointers will have their destructors invoked because they’re not real pointers, and these might free the object they’re referencing.

Unfortunately this isn’t correct. I use TArrays to hold references to actors all the time and I call Empty() on them pretty often. My actors would be destroyed if the deconstructors were being called on Empty().

sorry, is my wrong. **Empty **would not invoke destructor of pointer types.

thx for your help. I have make a mistake.

Glad you sorted it out!

thx again!

It is quite disadvantageous to have container with non-pointer values… eg when you do sorting, the copy constructor is invoked and make the sorting operation a lot slower.

It’s not disadvantageous if you don’t want them sorted. Besides… you have to use non-pointer types if you’re using Unreal’s smart pointers. Which is a very common use case and the copy constructor of these smart pointers is quite fast anyway.