no clue why a very basic piece of code crashes

Hello community,

I wanted to test something during my current project and started a clean project with a very little, basic code fragment.
Unfortunately, as soon as i create a blueprint class from it, place it in an empty level and hit ‘Play’ it crashes. Anyone able to explain the reason behind that crash to me? I cant find it.

.h:



UCLASS(Blueprintable)
class BASICCODE_API AGridNode : public AActor
{
	GENERATED_BODY()
public:	
	AGridNode(const FObjectInitializer& ObjectInitializer);

protected:
	UTextRenderComponent* m_north_text_component;
	USceneComponent* m_scene_component;
};


.cpp



AGridNode::AGridNode(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer),
	m_scene_component(0),
	m_north_text_component(0)
{
	m_scene_component = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));
	RootComponent = m_scene_component;

	m_north_text_component = CreateDefaultSubobject<UTextRenderComponent>(TEXT("NorthTextComp"));
	m_north_text_component->bVisible = true;
	m_north_text_component->bHiddenInGame = true;
	m_north_text_component->SetText(FString("North"));
	m_north_text_component->RelativeLocation = FVector(0, -100, 0);
	m_north_text_component->RelativeRotation = FRotator(90, 0, -90);
	m_north_text_component->AttachTo(m_scene_component);
}


Debugger attached and it crashes in USceneComponent.cpp / line 914 ‘Template Mismatch during attachment.’
edit: running v4.7.1

I’m really out of ideas here.
best regards,

  • Christoph -

try this:



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


I thought the same, but surprisingly, UObject does have a shortcut that redirects CreateDefaultSubobject to the relevant object initializer.

Mark your components with UPROPERTY(). Lacking this tag, those pointers are invisible to memory management and most likely are getting garbage collected.

wow - thanks guys, that was quick.

, your tip to declare it as UPROPERTY works. Would have never thought about that since components can be declared as regular pointers now.

thanks a bunch!

Yeah, its a new shortcut which was released in 4.7.0+. I’ve been having some finicky behavior when I update my code to not use the object initializers, so it might be the source of some issues.

Good catch on the UPROPERTY(). I wasn’t aware that caused variables to be garbage collected :open_mouth: I thought it just meant that those variables weren’t exposed to the editor in any way.