Managing the lifetime of actors spawned in Consutrction script

Hey there, so my system is as follows:
I have several “level pieces” that contain various “obstacles”, some of which are spawned in the construction script. I have created several helper functions in C++ that spawn obstacles in particular layouts (Bezier curves, shapes etc.). These functions are called from the Blueprint Construction script of the level piece.

Previously I was using the Blueprint node “AddChildActorComponent” and it worked perfectly. Now I am changing this to C++ I am very confused as to what functions to actually use, and how to properly manage their lifetimes. I was using “GetWorld()->SpawnActor( ObstacleClass, Transform, SpawnParams)” which I would then add to an array of obstacles “TArray< AActor* > Obstacles” which was working correctly. I managed to delete / cleanup the array most of the time but it did not work when the level piece was deleted in the level editor (NOT during game run time). I tried using “OnDestroy”, “EndPlay” & “BeginDestroy” and have had no luck, often with it just crashing.

Is there a way I can just hand the spawned obstacle off to the owned parent (the level piece) so that it is automatically handled / destroyed when the level piece is? What are the correct spawning and destroying functions I should be using? Should I be using AChildActorComponent? It seems like this should be extremely simple but I am having great difficulty understanding the huge variance in ways of doing things and what I should be actually using for this situation.

Thank you!

Finally solved it:
UChildActorComponent* NewComponent = NewObject< UChildActorComponent >( this );
if( NewComponent )
{
NewComponent->SetChildActorClass( Class );
NewComponent->AttachTo( RootComponent );
NewComponent->RegisterComponent();
NewComponent->bCreatedByConstructionScript_DEPRECATED = true;
SpawnedChildObstacles.Add( NewComponent );
}

You can use
NewComponent->CreationMethod = EComponentCreationMethod::UserConstructionScript;
instead of
NewComponent->bCreatedByConstructionScript_DEPRECATED = true;