TArray crash

I Have 2 classes.
LBranch and LTree . LTree have array of LBranch pointers.

void ALTree::AddBranch(UPARAM() FName name = ("Name"))
{
	UWorld* const World = GetWorld();
	if (World)
	{
	
		m_TreeBranch.Add(World->SpawnActor<ALBranch>(ALBranch::StaticClass()));
		(m_TreeBranch.Last())->AttachRootComponentToActor(this, NAME_None, EAttachLocation::KeepRelativeOffset, false);
		UE_LOG(LogTemp, Warning, TEXT("BranchAdded"));

	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Your message"));

	}

	m_BranchCount++;


}

in LTree.h

......
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LTree")
		TArray<class ALBranch*> m_TreeBranch;
.....

Of course first call is AddBranch then the UE_LOG.
I have crash when I call:

UE_LOG(LogTemp, Warning, TEXT("m_TreeBranch size %s"), m_TreeBranch.Num());

MachineId:A80F531F4560012039D7ACA9F9D39B11
EpicAccountId:0b1a4013cda84b91860223d2aa36c983

Access violation - code c0000005 (first/second chance not available)

""

ucrtbase
ucrtbase
ucrtbase
UE4Editor_Core!FMsg::Logf_Internal__VA() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\misc\outputdevice.cpp:569]
UE4Editor_MGR_4668!ALTree::execAddPoint() [c:\users\menos\desktop\magisterkaunreal\source\mgr\ltree.h:15]
UE4Editor_CoreUObject!UFunction::Invoke() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\coreuobject\private\uobject\class.cpp:4198]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:484]
UE4Editor_CoreUObject!UObject::ProcessContextOpcode() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:1755]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:698]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:608]
UE4Editor_CoreUObject!UObject::execVirtualFunction() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:1842]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:698]
UE4Editor_CoreUObject!UFunction::Invoke() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\coreuobject\private\uobject\class.cpp:4198]
UE4Editor_CoreUObject!UObject::ProcessEvent() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:1053]
UE4Editor_Engine!AActor::ProcessEvent() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\engine\private\actor.cpp:580]
UE4Editor_DetailCustomizations!FActorDetails::CallBlutilityFunction() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\editor\detailcustomizations\private\actordetails.cpp:726]
UE4Editor_DetailCustomizations!TMemberFunctionCaller<FActorDetails,FReply (__cdecl FActorDetails::*)(void) __ptr64>::operator()<>() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\public\delegates\delegateinstanceinterface_variadics.h:161]
UE4Editor_DetailCustomizations!TTupleImpl<TIntegerSequence<> >::ApplyAfter_ExplicitReturnType<FReply,TMemberFunctionCaller<FActorDetails,FReply (__cdecl FActorDetails::*)(void) __ptr64> >() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\public\delegates\tuple.h:113]
UE4Editor_DetailCustomizations!TBaseSPMethodDelegateInstance<0,FActorDetails,0,FReply __cdecl(void)>::Execute() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\public\delegates\delegateinstancesimpl_variadics.inl:282]
UE4Editor_Slate!TBaseDelegate<FReply>::Execute() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\public\delegates\delegatesignatureimpl_variadics.inl:440]
UE4Editor_Slate!SButton::OnMouseButtonUp() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\slate\private\widgets\input\sbutton.cpp:236]
UE4Editor_Slate!<lambda_4ba5b0c116ad939f50a9b616659f063a>::operator()() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\slate\private\framework\application\slateapplication.cpp:4276]
UE4Editor_Slate!FEventRouter::Route<FReply,FEventRouter::FToLeafmostPolicy,FPointerEvent,<lambda_4ba5b0c116ad939f50a9b616659f063a> >() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\slate\private\framework\application\slateapplication.cpp:214]
UE4Editor_Slate!FSlateApplication::RoutePointerUpEvent() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\slate\private\framework\application\slateapplication.cpp:4266]
UE4Editor_Slate!FSlateApplication::ProcessMouseButtonUpEvent() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\slate\private\framework\application\slateapplication.cpp:4663]
UE4Editor_Slate!FSlateApplication::OnMouseUp() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\slate\private\framework\application\slateapplication.cpp:4641]
UE4Editor_Core!FWindowsApplication::ProcessDeferredMessage() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\windows\windowsapplication.cpp:1416]
UE4Editor_Core!FWindowsApplication::DeferMessage() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\windows\windowsapplication.cpp:1721]
UE4Editor_Core!FWindowsApplication::ProcessMessage() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\windows\windowsapplication.cpp:719]
UE4Editor_Core!FWindowsApplication::AppWndProc() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\windows\windowsapplication.cpp:641]
user32
user32
UE4Editor_Core!FWindowsPlatformMisc::PumpMessages() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\core\private\windows\windowsplatformmisc.cpp:884]
UE4Editor!FEngineLoop::Tick() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\launch\private\launchengineloop.cpp:2391]
UE4Editor!GuardedMain() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\launch\private\launch.cpp:142]
UE4Editor!GuardedMainWrapper() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\launch\private\windows\launchwindows.cpp:200]
UE4Editor!__scrt_common_main_seh() [f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:264]
kernel32
ntdll

Before coming to the actual code, I’d first consider whether you really want to create an actor object for every branch of the tree - this is very expensive. Actor components are much better suited to your usecase; each branch could be an custom actorcomponent (derived from something like UStaticMeshComponent for example) and whatever functionality you wanted ABranch to have could be ported over to the custom component.

Also, attaching components to actors is also more intuitive and less bug-prone than attaching the rootcomponent of one actor to another as you’re doing. Child actor functionality in Blueprints is implemented using a special component -UChildActorComponent which is usable from C++ (with some quirks - answerhub has some posts this) but even then I think your best bet is to make each branch a component parented to the tree.

That said, to answer your actual question:

Are you sure your branch actor is being spawned correctly? Your crash could easily be coming from using m_TreeBranch.Last() without checking it first.

I would change this code of yours:

m_TreeBranch.Add(World->SpawnActor<ALBranch>(ALBranch::StaticClass()));
(m_TreeBranch.Last())->AttachRootComponentToActor(this, NAME_None, EAttachLocation::KeepRelativeOffset, false);

to something like:

auto myBranch = World->SpawnActor<ALBranch>(ALBranch::StaticClass());
if(myBranch)
{
    myBranch->AttachRootComponentToActor(this, NAME_None, EAttachLocation::KeepRelativeOffset, false);
    m_TreeBranch.Add(myBranch);
}
else
{
// <Add some appropriate logging code>
}

This will help you identify if your branch actor is actually being spawned.

If it isn’t, check your output logs - SpawnActor usually provides a lot of information as to why an actor wasn’t created whenever it fails. Eg: it could easily be failing because you’re not passing any transform data and the default spawn param ESpawnActorCollisionHandlingMethod might be preventing you from spawning due to collisions/etc (this just a guess).