NewObject in PostInitProperties causes crash on spawn!

The following causes a crash on player spawn. Debugging shows that the Reinstance manager somehow fails at duplicating the new objects, causing the object to have NULL pointer children and then crashing when checking all children and their blocking geometry when trying to find a spawn at the playerstart:

// Fill out your copyright notice in the Description page of Project Settings.

#include "CrashPOC.h"
#include "MyPawn.h"


void AMyPawn::PostInitProperties()
{
	Super::PostInitProperties();


	PlayerComponent = NewObject<UCapsuleComponent>(this);
	PlayerComponent->AttachTo(RootComponent);
}

// Sets default values
AMyPawn::AMyPawn()
{
	bReplicates = true;

 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	USceneComponent* dummyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
	RootComponent = dummyRoot;

}

// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AMyPawn::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	Super::SetupPlayerInputComponent(InputComponent);

}

Sample project is attached.link text

Hey Chaosgrid-

I opened the test project and added an instance of the MyPawn class and the PawnBP to the level before playing in the editor. In neither case did the project crash. Is there anything else that has to be done to case the crash in the project? Also, could you post the callstack and log files from you crash for additional information.

Cheers

Hey ,

it should be as simple as opening the project and pressing play (PIE). Dont drag anything into the level, make sure the gamemode default pawn is PawnBP.
I’ll retry on another machine to make sure it reproduces…

It appears the crash is caused because the capsule component you’re trying to add is conflicting with the capsule component that already exists on the character (UCapsuleComponent is included in the base character class that you inherit from). If you change the UCapsuleComponent in line 12 above to UStaticMeshComponent then there is no crash on PIE.

Cheers

So it is derived from Pawn and Pawn is empty as far as I have seen so that’s not really the reason.

The real problem seems to be using NewObject in any sort of constructor function, combined with BluePrints. This seems to be really buggy in general (also cannot save blueprint anymore due to serialization issues).
I dont know how I could still save my BP in another project but somehow it worked. The correct way is of course using CreateDefaultSubobject function, but it is not available in PostInitProperties (hence NewObject).

Anyway, I think this should be documented or the dev should even be warned by the compiler that NewObject should not be used at all in constructors and constructor-like functions (like PostInitProperties).
The alternative would be to make NewObject work in constructors…

Anyways, sorry for the weird issue, just writing here so that others who have the same problem have some pointers as to whats going on.

You’re correct that using CreateDefaultSubobject is the indented method for adding components inside the constructor. I did enter a bug report (UE-22555) about the crash occuring when NewObject is used inside PostInitProperties.