I having an AActor (C++ class) and a UActorComponent (C++ class).
In the constructor of the Actor I create the Component. The Actor is once in the world.
After changing something inside of the TickFunction of the Component and try to compile and hot reload
the Tests: "StaticAllocateObjectErrorTests"
fails at
MyComponent = ObjectInitializer.CreateDefaultSubobject(this, TEXT("MyComponent"));
Does somebody know this behaviour or is my way wrong how to add an ActorComponent in C++?
Thanks.
Abstracted Code:
// //////////////////////////////////////
// Header file of ACTOR:
// //////////////////////////////////////
UCLASS()
class PROJECT_API AMyActor : public AActor
{
...
AMyActor(const FObjectInitializer& ObjectInitializer);
...
private:
UMyComponent* MyComponent;
}
// //////////////////////////////////////
// CPP file of ACTOR
// //////////////////////////////////////
AMyActor::AMyActor(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
MyComponent = ObjectInitializer.CreateDefaultSubobject<UMyComponent>(this, TEXT("MyComponent"));
}
// //////////////////////////////////////
// Header file of ActorComponent:
// //////////////////////////////////////
class ASphereNavigationMesh;
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class PROJECT_API UMyComponent : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UMyComponent(const FObjectInitializer& ObjectInitializer);
~UMyComponent();
/* Color of the nodes */
UPROPERTY(Category="Debug/Render", EditAnywhere, BlueprintReadWrite)
FColor NodeColor;
/* Color of the nodes */
UPROPERTY(Category="Debug/Render", EditAnywhere, BlueprintReadWrite)
FColor EdgeColor;
private:
AMyActor* Actor;
};
// //////////////////////////////////////
// CPP file of ActorComponent
// //////////////////////////////////////
UMyComponent::UMyComponent(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
bWantsInitializeComponent = true;
PrimaryComponentTick.bCanEverTick = true;
bTickInEditor = true;
Actor = Cast<AMyActor>(GetOuter());
NodeColor = FColor::Red;
EdgeColor = FColor::Green;
}
UMyComponent::~UMyComponent()
{
Actor = nullptr;
}
Concerning the call stack the reason for the crash starts in
Cast(GetOuter());
#0 0x00000001009fae95 in StaticAllocateObjectErrorTests(UClass*, UObject*, FName, EObjectFlags) ()
#1 0x00000001009fb6bd in StaticAllocateObject(UClass*, UObject*, FName, EObjectFlags, bool, bool*) ()
#2 0x00000001009f9cdd in StaticConstructObject(UClass*, UObject*, FName, EObjectFlags, UObject*, bool, FObjectInstancingGraph*) ()
#3 0x0000000100a03025 in FObjectInitializer::CreateDefaultSubobject(UObject*, FName, UClass*, UClass*, bool, bool, bool) const ()
#4 0x000000016b4f5433 in UMyComponent* FObjectInitializer::CreateDefaultSubobject<UMyComponent>(UObject*, FName, bool) const
#5 0x000000016b4ef97f in AMyActor::AMyActor(FObjectInitializer const&)
#6 0x000000016b4efa5d in AMyActor::AMyActor(FObjectInitializer const&)
#7 0x000000016b50bf3e in AMyActor::__DefaultConstructor(FObjectInitializer const&)
#8 0x000000016b50bef5 in void InternalConstructor<AMyActor>(FObjectInitializer const&)
#9 0x00000001009f9ffc in StaticConstructObject(UClass*, UObject*, FName, EObjectFlags, UObject*, bool, FObjectInstancingGraph*) ()
#10 0x0000000100864362 in UClass::HotReloadPrivateStaticClass(unsigned int, unsigned int, unsigned long long, wchar_t const*, void (*)(FObjectInitializer const&), void (*)(UObject*, FReferenceCollector&), UClass*, UClass*) ()
#11 0x000000016b50b048 in void GetPrivateStaticClassBody<AMyActor>(wchar_t const*, wchar_t const*, UClass*&, void (*)()) at /Users/Shared/UnrealEngine/4.7/Engine/Source/Runtime/CoreUObject/Public/UObject/Class.h:2270
#12 0x000000016b4fcec4 in AMyActor::GetPrivateStaticClass(wchar_t const*)
#13 0x000000016b4f8610 in AMyActor::StaticClass()
#14 0x000000016b4f85e9 in bool UObjectBaseUtility::IsA<AMyActor>()
#15 0x000000016b4f7146 in TCastImpl<UObject, AMyActor, (ECastType)0>::DoCast(UObject*) [inlined] at /Users/Shared/UnrealEngine/4.7/Engine/Source/Runtime/CoreUObject/Public/Templates/Casts.h:100
#16 0x000000016b4f713e in AMyActor* Cast<AMyActor, UObject>(UObject*) [inlined] at /Users/Shared/UnrealEngine/4.7/Engine/Source/Runtime/CoreUObject/Public/Templates/Casts.h:135
#17 0x000000016b4f7128 in UMyComponent::UMyComponent(FObjectInitializer const&)
#18 0x000000016b4f723d in UMyComponent::UMyComponent(FObjectInitializer const&)
#19 0x000000016b50f27e in UMyComponent::__DefaultConstructor(FObjectInitializer const&)
#20 0x000000016b50f215 in void InternalConstructor<UMyComponent>(FObjectInitializer const&) at /Users/Shared/UnrealEngine/4.7/Engine/Source/Runtime/CoreUObject/Public/UObject/Class.h:2237
#21 0x00000001009f9ffc in StaticConstructObject(UClass*, UObject*, FName, EObjectFlags, UObject*, bool, FObjectInstancingGraph*) ()
#22 0x0000000100864362 in UClass::HotReloadPrivateStaticClass(unsigned int, unsigned int, unsigned long long, wchar_t const*, void (*)(FObjectInitializer const&), void (*)(UObject*, FReferenceCollector&), UClass*, UClass*) ()
#23 0x000000016b508418 in void GetPrivateStaticClassBody<UMyComponent>(wchar_t const*, wchar_t const*, UClass*&, void (*)()) at /Users/Shared/UnrealEngine/4.7/Engine/Source/Runtime/CoreUObject/Public/UObject/Class.h:2270
#24 0x000000016b4fcbe4 in UMyComponent::GetPrivateStaticClass(wchar_t const*)
#25 0x000000016b4f6520 in UMyComponent::StaticClass()
#26 0x000000016b50f331 in TClassCompiledInDefer<UMyComponent>::Register() const at /Users/Shared/UnrealEngine/4.7/Engine/Source/Runtime/CoreUObject/Public/UObject/UObjectBase.h:295
#27 0x00000001009e79c4 in UClassRegisterAllCompiledInClasses() ()
#28 0x00000001009e7ea7 in ProcessNewlyLoadedUObjects() ()
#29 0x00000001008907c7 in TBaseStaticDelegateInstance<void ()>::ExecuteIfSafe() const ()
#30 0x00000001000e5ad8 in TBaseMulticastDelegate<void>::Broadcast() const ()
#31 0x00000001002a9720 in FModuleManager::LoadModuleWithFailureReason(FName, EModuleLoadResult&, bool) ()
#32 0x00000001002a8e15 in FModuleManager::LoadModule(FName, bool) ()
#33 0x000000010fa94310 in FHotReloadModule::DoHotReloadInternal(bool, ECompilationResult::Type, TArray<UPackage*, FDefaultAllocator>, TArray<FName, FDefaultAllocator>, FOutputDevice&) ()
#34 0x000000010fa9887e in FHotReloadModule::DoHotReloadFromIDE() ()
#35 0x000000010fa8a791 in FHotReloadModule::Tick(float) ()
#36 0x00000001000313d9 in FTicker::Tick(float) ()
#37 0x000000010001b799 in FEngineLoop::Tick() ()
#38 0x000000010001e503 in GuardedMain(wchar_t const*) ()
#39 0x000000010002c77f in -[UE4AppDelegate runGameThread:] ()
#40 0x000000010018f278 in -[FCocoaGameThread main] ()
#41 0x00007fff84b86dc2 in __NSThread__main__ ()
#42 0x00007fff8c153268 in _pthread_body ()
#43 0x00007fff8c1531e5 in _pthread_start ()
#44 0x00007fff8c15141d in thread_start ()