Memory crash using EmplaceAt

Hey!

I may misunderstanding the usage of EmplaceAt but if I do something like this:

UTestObject* NewSlot = DuplicateObject<UTestObject>(DefaultSlot, this); InventorySlots.EmplaceAt(Index, NewSlot);The engine crashes.

UTestObject in an UObject instantiated inline (but I got this crash using a nullptr as well) and EmplaceAt Index is equals to 3 (any value different than zero should do the job).

Code is pretty simple so I may be doing something wrong, I thought that EmplaceAt would have reserved the amount of memory I need it but It’s crashing. I also tried call Reserve first with Index+1 and It changed the ArrayMax value but the crash is still present.

Would you mind to point me how I can inject an object in an array at the specified index?

Steps to Reproduce

  1. Create an TArray of an UObject child class.
  2. Use EmplaceAt with an index different than zero.
  3. Crash

Hi,

You cannot ‘EmplaceAt’ or ‘Insert’ at an index greater than TArray<>::Count(). Since you have an array of pointers, you can add a couple of null elements first like below. Reserving the memory would possibly avoid reallocation, but this doesn’t construct the array ‘type’ item in the container.

if (Index > InventorySlots.Count()) { InventorySlots.AddDefaulted(Index - InventorySlots.Count()); } InventorySlots.EmplaceAt(Index, DuplicateObject<UTestObject>(DefaultSlot, this));Regards,

Patrick