Adding an Actor Component in C++ keeps crashing UE4

I have a custom Volume named CycloneDomain. I want to add a Procedural Mesh Component to this actor as soon as it is placed in the editor, but UE4 crashes every time the first line in the OnConstrustion function runs. I already added in procedural mesh dependencies in the .build.cs and .uproject files, and it compiles just fine as well.

.h file (just the parts you need to see :wink:

#pragma once

#include "CoreMinimal.h"
#include "ProceduralMeshComponent.h"
#include "Engine/Engine.h"
#include "GameFramework/Volume.h"
#include "Components/ActorComponent.h"
#include "CycloneDomain.generated.h"

UCLASS()
class CYCLONEFLUIDSIM_API ACycloneDomain : public AVolume
{
	GENERATED_BODY()

public:
	const UObject* WorldContextObject = ();
	virtual void OnConstruction(const FTransform& Transform) override;

private:
	UPROPERTY(VisibleAnywhere)
	UProceduralMeshComponent* mesh;

.cpp file (I only put up the relevant code)

#include "CycloneDomain.h"

void ACycloneDomain::OnConstruction(const FTransform& Transform) 
{
	mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
	RootComponent = mesh;

    // multi-threaded PhysX cooking.
	mesh->bUseAsyncCooking = true;
}

How do I change/fix this so it won’t crash the engine

I did what you said and it works. The editor doesn’t crash anymore, but now I can’t move, scale, or rotate the actor.

What I mean by that is the gimbal inside of the level editor moves and looks like it’s rotating/scaling/translating, but the volume no longer follows the gimbal around. Although, once the level is played, the volume transforms to its desired location/rotation/scale.

Do you know what’s happening or how to fix it?

(Sorry, I’m a bit of a newb when it comes to ue4)

You should declare components like that in constructor so engine will be aware of by default. If you don’t do that those component won’t be visible in blueprint component list.

Other that that if you get crash first thing you need to do is check logs in Saved/Logs directory of your project. Every crash, or else it uncontrolled one (like calling on null or invalid pointer where OS suddenly stops the UE4) prints out information about in logs, is most cases those are assertions fails which engine meet unexpected condition and to prevent instability it crash it self and it prints failed condition, which require some figureing out what happened:

But is some cases there are full descriptive error that can cause crash and they are printed in logs, specially once related to component initiation, so checking the logs alone can solve many issues

Check mobility vararable on component you made

If root component is static the entire actor becomes immobile