Constructor C++

My UE4 keeps crashing with this code, i keeped reading on more modules and looking at other forums should i be using NewObject? Goal is just to get the list of StaticMeshes found in the folder and add to actor as components.

FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");
    TArray<FAssetData> AssetData;
    AssetRegistryModule.Get().GetAssetsByPath("/Game/Blender/Ground", AssetData, true);
    AssetData.Sort();

    DummyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
    RootComponent = DummyRoot;

    for (int32 i = 0; i < AssetData.Num(); i++)
    {
        int ArrIndex = MeshArr.Add(CreateDefaultSubobject<UStaticMeshComponent>("StaticMesh_" + i));
        MeshArr[ArrIndex]->SetupAttachment(DummyRoot);



        UObject* AssetID = AssetData[ArrIndex].GetAsset();
        FString AssetName = *AssetID->GetFullName();
        AssetName.RemoveAt(10, 1);
        AssetName.InsertAt(10, "'");
        int32 leng = AssetName.Len();
        AssetName.InsertAt(leng, "'");

        UE_LOG(LogTemp, Warning, TEXT("%s"), *AssetName);

        auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(*AssetName);
        if (MeshAsset.Object != nullptr)
        {
            MeshArr[ArrIndex]->SetStaticMesh(MeshAsset.Object);
        }

    }

Hi!
Can you please provide further information of the crash? Maybe some logs?

It was something stupid but doesnt crash now wasnt adding into TArray with .Add()

New Solution and working:

FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");
	TArray<FAssetData> AssetData;
	AssetRegistryModule.Get().GetAssetsByPath("/Game/Blender/Ground", AssetData);
	AssetData.Sort();

	DummyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
	RootComponent = DummyRoot;

	 for (int num = 0; num < AssetData.Num(); num++)
	 {
	 	UObject* AssetID = AssetData[num].GetAsset();
	 	FName AssetFName = AssetID->GetFName();
	 	MeshSet = NewObject<UStaticMeshComponent>(this, AssetFName);
		FString AssetFullName = *AssetID->GetFullName();
		AssetFullName.RemoveAt(10, 1);
		AssetFullName.InsertAt(10, "'");
		int32 leng = AssetFullName.Len();
		AssetFullName.InsertAt(leng, "'");

		auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(*AssetFullName);
		if (MeshAsset.Object != nullptr)
		{
			UE_LOG(LogTemp, Warning, TEXT("%s"), *AssetFullName);
			MeshSet->SetStaticMesh(MeshAsset.Object);
		}
	 }

Could someone explain to me the difference between NewObject<> and CreateDefaultSubobject<>? To me it looks like CreateDefaultSubobject is for just good for adding 1 component.

Constructor in UObjects are used in special way, they are made to set up class default object (CDO), which holds default value states, they put in special sleep state, not initiating object fully. Because of that they are executed when your module is initiated and UE4 potentially can executed in other irregular moments, so by convention constructors on UObject should only set defaults.

If you use NewObject there full instance of class create, if you put it in constructor that full active instance is created during CDO creation and it’s fully active, where it should not be as role of CDO is just to hold defaults without executing. That why CreateDefaultSubobject was made, it creates objects as a part of CDO and when you create a object with NewObject elsewhere that object is created together with object that being initiated and values you set in subobject is being applied on top of existing defaults of subobject (since htey also UObjects). CreateDefaultSubobject is mainly used to declare components in actors, but it does not need to be used just for that, it can be used to create any class of object and it will be initated on object creation.

So in CreateDefaultSubobject just tells “oh hey, init this subobject on object creation, with those variables i set” in constructor while NewObject stright up inits the object. so in general you should not even use NewObject in constructors as it will create object irregularly as constructors is executed