Editor crash when stop play, after using GetDefaultObject

When i stop play from the editor, the editor will crash if AbilityStartSet is not empty. The code is used on beginplay and works fine while playing, but crashes the editor when the game stops.

In LevelSetup.h

UFUNCTION(BlueprintCallable, Category = LevelSetup)
TArray<UPlayerAbility*> GetStartSet();

UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = LevelSetup)
TArray<TSubclassOf<UPlayerAbility>> AbilityStartSet;

And in LevelSetup.cpp

TArray<UPlayerAbility*> ALevelSetup::GetStartSet(){
    TArray<UPlayerAbility*> temp;
	
    for (int32 i = 0; i < AbilityStartSet.Num(); i++)
    {		
	    UPlayerAbility *Ability = AbilityStartSet.Pop()->GetDefaultObject<UPlayerAbility>();
	    temp.Add(Ability);
    }

	return temp;
}

I use this code to be able to select some UPlayerAbility’s directly from the editor and then instantiate them with the GetStartSetMethod (which is called from a blueprint).

I’m not looking thoroughly at this, but I bet that your pop logic is faulty. Pop reduces the number of elements and removes the end element I believe, while you are iterating from 0 which is the beginning from a logical stand point. Mess around with your for loop and see what you get. Also why use pop? just normal for loop and temp.Add(AbilityStartSet[i]); You might need to do an if(isValid) check also.

I must admit i don’t quite get the inner workings of this, but i solved the problem by using StaticConstructObject instead of GetDefaultObject

for (int32 i = 0; i < AbilityStartSet.Num(); i++)
{		
	UObject* tempObject = StaticConstructObject(AbilityStartSet[i]);
	UPlayerAbility *Ability = Cast<UPlayerAbility>(tempObject);
	temp.Add(Ability);
}