Developer Notes

New Item must be added to array before accessing it.

PuzzlePieces[Piece] = ObjectInitializer.CreateDefaultSubobject<...>(...);
This should be replaced with:
PuzzlePieces.Add(ObjectInitializer.CreateDefaultSubobject<...>(...));

Description

Using a TArray to store multiple components being setup inside a for loop of a constructor will crash on project open.

Crash Reporter:

[Link Removed]

Steps to Reproduce

1. Open UE4 Editor (any project)
2. Add code to project based on Actor (MyActor)
3. Add the following to the constructor declaration

 (const FObjectInitializer &ObjectInitialIzer)

4.. In the header file add the following:

UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Puzzle)
		int32 PuzzleWidth;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Puzzle)
		int32 PuzzleHeight;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Puzzle)
	float PieceWidth;
	
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Puzzle)
	int32 TotalPieces;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Puzzle)
	USceneComponent* Root;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Puzzle)
		TArray<UStaticMeshComponent*> PuzzlePieces;

5. In the source file add the following to the constructor:

PuzzleWidth = 3;
	PuzzleHeight = 3;
	PieceWidth = 2.f;
	TotalPieces = PuzzleHeight * PuzzleWidth;

	Root = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("Root"));

	for (int32 Piece = 0; Piece < TotalPieces; Piece++)
	{
		FString PieceName = "PieceMesh" + FString::FromInt(Piece);

		PuzzlePieces[Piece] = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, FName(*PieceName));
		PuzzlePieces[Piece]->AttachParent = RootComponent;
		PuzzlePieces[Piece]->AttachSocketName = FName(*PieceName);
	}

6. Compile
7. Open the project

Result:
Editor will crash on open. Running in debug causes breakpoint during loading process

Callstack
MachineId:9FA88CA44B7C623122B0039D6C8BA1B0

Unknown exception - code 00000001 (first/second chance not available)

"Assertion failed: (Index >= 0) & (Index < ArrayNum) [File:D:\Builds\Main\UE4\Engine\Source\Runtime\Core\Public\Containers\Array.h] [Line: 678] 
Array index out of bounds: 0 fr

KERNELBASE
UE4Editor_Core!FOutputDeviceWindowsError::Serialize() [d:\builds\main\ue4\engine\source\runtime\core\private\windows\windowsplatformoutputdevices.cpp:95]
UE4Editor_Core!FOutputDevice::Logf__VA() [d:\builds\main\ue4\engine\source\runtime\core\private\misc\outputdevice.cpp:144]
UE4Editor_Core!FDebug::AssertFailed() [d:\builds\main\ue4\engine\source\runtime\core\private\misc\outputdevice.cpp:360]
UE4Editor_ForLoopBreakTEST
UE4Editor_CoreUObject!UClass::CreateDefaultObject() [d:\builds\main\ue4\engine\source\runtime\coreuobject\private\uobject\class.cpp:2538]
UE4Editor_CoreUObject!UObjectLoadAllCompiledInDefaultProperties() [d:\builds\main\ue4\engine\source\runtime\coreuobject\private\uobject\uobjectbase.cpp:680]
UE4Editor_CoreUObject!ProcessNewlyLoadedUObjects() [d:\builds\main\ue4\engine\source\runtime\coreuobject\private\uobject\uobjectbase.cpp:768]
UE4Editor_CoreUObject!TBaseStaticDelegateInstance<void __cdecl(void)>::ExecuteIfSafe() [d:\builds\main\ue4\engine\source\runtime\core\public\delegates\delegateinstancesimpl_variadics.inl:921]
UE4Editor_Core!TBaseMulticastDelegate<void>::Broadcast() [d:\builds\main\ue4\engine\source\runtime\core\public\delegates\delegatesignatureimpl_variadics.inl:1031]
UE4Editor_Core!FModuleManager::LoadModuleWithFailureReason() [d:\builds\main\ue4\engine\source\runtime\core\private\modules\modulemanager.cpp:413]
UE4Editor_Projects!FModuleDescriptor::LoadModulesForPhase() [d:\builds\main\ue4\engine\source\runtime\projects\private\moduledescriptor.cpp:370]
UE4Editor_Projects!FProjectManager::LoadModulesForProject() [d:\builds\main\ue4\engine\source\runtime\projects\private\projectmanager.cpp:53]
UE4Editor!FEngineLoop::LoadStartupModules() [d:\builds\main\ue4\engine\source\runtime\launch\private\launchengineloop.cpp:1903]
UE4Editor!FEngineLoop::PreInit() [d:\builds\main\ue4\engine\source\runtime\launch\private\launchengineloop.cpp:1425]
UE4Editor!GuardedMain() [d:\builds\main\ue4\engine\source\runtime\launch\private\launch.cpp:110]
UE4Editor!GuardedMainWrapper() [d:\builds\main\ue4\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() [d:\builds\main\ue4\engine\source\runtime\launch\private\windows\launchwindows.cpp:249]

Have Comments or More Details?

There's no existing public thread on this issue, so head over to Questions & Answers just mention UE-14113 in the post.

0
Login to Vote

By Design
ComponentUE - Foundation - Core
Affects Versions4.8
Target Fix4.9
CreatedApr 20, 2015
ResolvedApr 22, 2015
UpdatedFeb 5, 2017
View Jira Issue