UE4 crashes every time I hot reload

UE4 crashes every time I hot reload. When I build without the editing running, everything is fine. How can I fix this or at least determine why it’s crashing?

EDIT:
I believe it has something to do with CreateDefaultSubobject. When hot reloading, does the previous default subject not get destroyed?

EDIT #2:
I ran the editor in debug mode using the UE4 source from git. The UHierarchicalInstancedStaticMeshComponent class is trying to reference a null ptr when hot reloading. Here is the function. You can see that ClusterTreePtr is not always assigned a value. Why is this?

void UHierarchicalInstancedStaticMeshComponent::Serialize(FArchive& Ar)
{
	// On save, if we have a pending async build we should wait for it to complete rather than saving an incomplete tree
	if (Ar.IsSaving())
	{
		if (!IsTreeFullyBuilt())
		{
			BuildTree();
		}
	}

	Super::Serialize(Ar);

	if (Ar.IsLoading())
	{
		ClusterTreePtr = MakeShareable(new TArray<FClusterNode>);
	}
	TArray<FClusterNode>& ClusterTree = *ClusterTreePtr;
	ClusterTree.BulkSerialize(Ar);
	if (Ar.IsLoading() && !BuiltInstanceBounds.IsValid)
	{
		BuiltInstanceBounds = (ClusterTree.Num() > 0 ? FBox(ClusterTree[0].BoundMin, ClusterTree[0].BoundMax) : FBox(0));
	}
}

I have the exact same issue, ran in debug, throws an exception at the same place. I am hoping someone figures it out because i have to restart my editor every time i compile.

Yeah, changing header files or stuff in the constructor usually breaks hot reload for me, but I believe that’s just normal. Annoying when you just created the class and still adding 1 component after another. However, if you set up all variables and just changing code that is affected in runtime, it doesn’t break.

Is there an update on this issue? I’m having the same problem

I’m in UE4.22 and I am still having this issue. I am performing this line:

if (pATarget != nullptr)

and I get a read access violation. I’m starting to think that compiling from within Unreal isn’t the best way to compile. The object itself isn’t null and the name is even associated to a proper object in the level, yet when reading its own variables I get access violations. Is something being garbage collected without me knowing?

Same issue here in 2020 with UE4.24.

I created a class derived from UFoliageInstancedStaticMeshComponent and it crashes the editor every time I hot reload, in the same place the OP indicates, in D:\UE_4.24\Engine\Source\Runtime\Engine\Private\HierarchicalInstancedStaticMesh.cpp:2044:

void UHierarchicalInstancedStaticMeshComponent::Serialize(FArchive& Ar) {
...
ClusterTreePtr->BulkSerialize(Ar);
...
}

Probably because ClusterTreePtr is NULL for me too.

Could you provide some of your code that may be the cause of the crash? Preferably the parts that use HISM.
ClusterTreePtr always gets value. If it’s null, it seems that the reloading part doesn’t work as intended.

 if (Ar.IsLoading())
 {
     ClusterTreePtr = MakeShareable(new TArray<FClusterNode>);
 }

And btw, if you’re using 4.15 as stated, maybe they resolved whatever issue it might have.

I got the same issue with 4.24. I derived from UFoliageInstancedStaticMeshComponent and the editor crashed every time it hot reloaded my class.
The crash was in the same line the OP indicated 3 years ago: ClusterTreePtr probably being NULL.

I worked around it by implementing Serialize like this:


// Workaround for hot reloading
void MyClass::Serialize (FArchive& Ar)
{
	void *ptr = (void *)ClusterTreePtr.Get();
	if (ptr != NULL) {
		Super::Serialize (Ar);
	} else {
		UE_LOG (TmpLog, Warning, TEXT ("Ignored attempt to serialize foliage with NULL ClusterTreePtr"));
	}
}

This is probably very wrong, hiding a deeper problem somewhere else, but so far it does the trick. Sometimes I see the Warning, sometimes I don’t, but it hot reloads without issues now.