My class runs in PIE, but crash when deleting it from a BP or recompile the actor BP

I’ve got an atmosphere class that contains pointer to World Light, Atmosphere Fog, Exponential Height Fog and Audio Component:

header:

UPROPERTY(EditAnywhere) UDirectionalLightComponent* WorldLight = nullptr;		
UPROPERTY(EditAnywhere) UAtmosphericFogComponent* AtmosphereFog = nullptr;		
UPROPERTY(EditAnywhere) UExponentialHeightFogComponent* HeightFog = nullptr;	
UPROPERTY(EditAnywhere) UAudioComponent* AmbientSound = nullptr;			

cpp:

AAtmosphere::AAtmosphere()
{
 	PrimaryActorTick.bCanEverTick = false;

	AmbientSound = CreateDefaultSubobject<UAudioComponent>(FName(TEXT("Ambient Sound")));
	WorldLight = CreateDefaultSubobject<UDirectionalLightComponent>(FName(TEXT("World Light")));
	AtmosphereFog = CreateDefaultSubobject<UAtmosphericFogComponent>(FName(TEXT("Atmosphere Fog")));
	HeightFog = CreateDefaultSubobject<UExponentialHeightFogComponent>(FName(TEXT("Height Fog")));;
}

This class is used as a child to my AtomsphereController class.

header:

private:
	AAtmosphere* Atmosphere = nullptr;
    APlayerController* MyController = nullptr;														

cpp:

AtmosphereController::UAtmosphereController()
{
	bWantsBeginPlay = true;
	PrimaryComponentTick.bCanEverTick = false;		
	Atmosphere = CreateDefaultSubobject<AAtmosphere>(FName(TEXT("Atmosphere")));
}

Now, this AtmosphereController is attached to an actor in my game that is suppose to have control of the atmosphere. I’ve attached it to this blueprint, and can compile ONE time. The game will run fine in the editor, and excuse as expected. BUT, if I try to compile the BP a second time or delete the AtmosphereController from the BP… insta-crash. I know it has something to do with the AtmosphereController’s call to CreateDefaultSubobject, because I can comment it out, recompile the game, and then delete it from my BP. But once I uncomment, and recompile my BP for a second time, it starts crashing again.

Can someone explain WHY this is occurring, and what I should do to fix it?

Hello ,

If the editor is crashing, are you getting a Crash Reporter window? If so, could you copy/paste the callstack here? That should be able to give some information as to the nature of this crash.

If you’re deleting the Controller, this could be related to the pointers and the objects that they’re pointing to.

To clarify, the AAtmosphere class is not a child of the AtmosphereController class in this case, unless you’re leaving out that part. You’re creating a pointer to an AAtmosphere actor with the code you posted above rather than making it a child.

We haven’t heard from you in a while, . Are you still experiencing this issue? If so, was my last comment of any help? In the meantime, I’ll be marking this issue as resolved for tracking purposes.

I’m very sorry! I posted this up on the forums as well and forgot to follow up here.

Even so, your answer was very relevant. I can’t remember exactly where it was now, but it was causing a null pointer error when I recompiled or removed the component! :slight_smile: