(De)activating particle system causes crash

I have been exploring Unreal with C++ in a toy project, but this issue is driving me insane. For some reason calling either Activate() or Deactivate() on a UParticleSystemComponent crashes the editor.

In an APawn child class header, I have the following declaration:

UPROPERTY(Category = Particle, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
class UParticleSystemComponent* EngineFlameL;

The following is a part of the constructor in the corresponding .cpp file:

// Structure to hold one-time initialization
	struct FConstructorStatics
	{
		ConstructorHelpers::FObjectFinderOptional<UStaticMesh> CollisionMesh;
		ConstructorHelpers::FObjectFinder<UParticleSystem> EngineFlameL;
		FConstructorStatics()
			: CollisionMesh(TEXT("/Game/SB/Meshes/fighter/fighter_collider")),
			EngineFlameL(TEXT("ParticleSystem'/Game/SB/Particles/P_flame.P_flame'"))
		{
		}
	};
	static FConstructorStatics ConstructorStatics;

	// Create static mesh components
	CollisionMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CollisionMesh0"));
	CollisionMesh->SetStaticMesh(ConstructorStatics.CollisionMesh.Get());
	RootComponent = CollisionMesh;

	// attaching particle systems
	EngineFlameL = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("EFL0"));
	if (ConstructorStatics.EngineFlameL.Succeeded()) {
    EngineFlameL->Template = ConstructorStatics.EngineFlameL.Object;
	EngineFlameL->SetupAttachment(CollisionMesh);
	EngineFlameL->ActivateSystem();  // This line causes a crash for some reason
	}

None of the above variables are referenced anywhere else in the code. Everything works fine when the ActivateSystem() line above is commented out. The particle system is then rendered and fully functional.

If an ActivateSystem() or DeactivateSystem() is called anywhere in the constructor or Tick() functions, compilation will be successful, but the editor will crash as soon as I press the “Play” button. Afterwards, it will also crash instantly upon an attempted (re)start. The editor used to give a crash-report window, but I did not have the required debugging symbols for generating a proper error message. After downloading the symbols, the crash-window does not appear at all anymore.

I can get the project working again after removing the de(activation) calls and recompiling. I have tested this for different particle systems, but the problem persists for all of them.

Am I missing something obvious? Perhaps some kind of initialization? Calling InitializeSystem() first does not seem to help (but does not give issues, so the pointer seems to be valid).

Any help would be hugely appreciated!

Now that you have the debug symbols for the engine downloaded do you have a proper stack trace when the crash occurs?

For some reason I cannot reply to your post, Brian, so I am replying here:

The crash window has stopped showing up after I got the symbols. The log in the project’s Saved/Logs contains the following:

[2018.11.15-20.25.18:386][583]LogBlueprintUserMessages: Early PlayInEditor Detection: Level '/Game/FlyingCPP/Maps/FlyingExampleMap.FlyingExampleMap:PersistentLevel' has LevelScriptBlueprint '/Game/FlyingCPP/Maps/FlyingExampleMap.FlyingExampleMap:PersistentLevel.FlyingExampleMap' with GeneratedClass '/Game/FlyingCPP/Maps/FlyingExampleMap.FlyingExampleMap_C' with ClassGeneratedBy '/Game/FlyingCPP/Maps/FlyingExampleMap.FlyingExampleMap:PersistentLevel.FlyingExampleMap'
[2018.11.15-20.25.18:416][583]LogPlayLevel: PlayLevel: No blueprints needed recompiling
[2018.11.15-20.25.18:540][583]PIE: New page: PIE session: FlyingExampleMap (Nov 15, 2018, 8:25:18 PM)
[2018.11.15-20.25.18:541][583]LogPlayLevel: Creating play world package: /Game/FlyingCPP/Maps/UEDPIE_0_FlyingExampleMap
[2018.11.15-20.25.18:581][583]LogWindows: Windows GetLastError: The operation completed successfully. (0)
[2018.11.15-20.26.19:266][583]LogWindows: Error: === Critical error: ===
[2018.11.15-20.26.19:266][583]LogWindows: Error: 
[2018.11.15-20.26.19:266][583]LogWindows: Error: Assertion failed: GetWorld() [File:D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\Particles\ParticleComponents.cpp] [Line: 5842] 
[2018.11.15-20.26.19:266][583]LogWindows: Error: 
[2018.11.15-20.26.19:266][583]LogWindows: Error: 
[2018.11.15-20.26.19:266][583]LogWindows: Error: 
[2018.11.15-20.26.19:266][583]LogWindows: Error: 
[2018.11.15-20.26.19:342][583]LogExit: Executing StaticShutdownAfterError
[2018.11.15-20.26.20:238][583]LogWindows: FPlatformMisc::RequestExit(1)
[2018.11.15-20.26.20:296][583]Log file closed, 11/15/18 20:26:20

I have not been able to find any other logs. Is there perhaps some other place I should be looking? Thanks for your help.

Since you have the debug symbols installed you’ll want to run the project from visual studio. Are you actually running the editor through visual studio while it is debugging? Visual studio should be able to catch the crash as it happens and provide information about the stack and memory at the moment of the crash.

This is the top of the call stack when the exception is thrown:

>	UE4Editor-Engine.dll!UParticleSystemComponent::DeactivateSystem() Line 5842	C++
 	UE4Editor-ace-6854.dll!AacePawn::AacePawn() Line 77	C++
 	UE4Editor-CoreUObject.dll!StaticConstructObject_Internal(UClass * InClass, UObject * InOuter, FName InName, EObjectFlags InFlags, EInternalObjectFlags InternalSetFlags, UObject * InTemplate, bool bCopyTransientsFromClassDefaults, FObjectInstancingGraph * InInstanceGraph, bool bAssumeTemplateIsArchetype) Line 3156	C++
 	UE4Editor-CoreUObject.dll!FDuplicateDataWriter::GetDuplicatedObject(UObject * Object, bool bCreateIfMissing) Line 138	C++
 	UE4Editor-CoreUObject.dll!FDuplicateDataWriter::operator<<(UObject * & Object) Line 65	C++
 	[Inline Frame] UE4Editor-Engine.dll!operator<<(FArchive &) Line 92	C++
 	UE4Editor-Engine.dll!operator<<(FArchive & Ar, TArray<AActor *,FDefaultAllocator> & A) Line 1109	C++
 	UE4Editor-Engine.dll!ULevel::Serialize(FArchive & Ar) Line 316	C++
 	UE4Editor-CoreUObject.dll!StaticDuplicateObjectEx(FObjectDuplicationParameters & Parameters) Line 2094	C++
 	UE4Editor-CoreUObject.dll!StaticDuplicateObject(const UObject * SourceObject, UObject * DestOuter, const FName DestName, EObjectFlags FlagMask, UClass * DestClass, EDuplicateMode::Type DuplicateMode, EInternalObjectFlags InternalFlagsMask) Line 2000	C++
 	UE4Editor-UnrealEd.dll!UEditorEngine::CreatePIEWorldByDuplication(FWorldContext & WorldContext, UWorld * InWorld, FString & PlayWorldMapName) Line 3656	C++
 	UE4Editor-Engine.dll!UGameInstance::InitializeForPlayInEditor(int PIEInstanceIndex, const FGameInstancePIEParameters & Params) Line 245	C++

It seems there is a problem with getting the world pointer in DeactivateSystem().

Does this only happen if you call Deactivate in the constructor? What happens if you wait to deactivate the system in the actor’s BeginPlay function? I think what is happening is the CDO does not have any sort of initialized world when it gets called.

(De)activating the system in the actor’s BeginPlay function works like a charm. Thanks for your help!

The crash occurred because (de)activating particle systems requires the world to be initialized. This could not be guaranteed at the time my actor’s constructor was called, which caused an assertion to fail. As suggested by Brian, calling the (de)activation later on (in the actor’s BeginPlay function) fixes the problem.