Why is my UObject created with NewObject crashing the engine?

I am trying to instantiate an UObject subclass, but I have very strange errors when I do.

Fatal error: [File:D:\Build\++UE4+Release-4.18+Compile\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp] [Line: 2484] EEarlyZPass::MAX_68678 is not being constructed with either NewObject, NewNamedObject or ConstructObject.

KERNELBASE
UE4Editor_ApplicationCore
UE4Editor_Core
UE4Editor_Core
UE4Editor_CoreUObject
UE4Editor_Underwater_9207!UChunk::UChunk() [d:\projects\underwater\source\underwater\chunk.cpp:3]
UE4Editor_Underwater_9207!UMapGeneratorComponent::CreateChunk() [d:\projects\underwater\source\underwater\mapgeneratorcomponent.cpp:97]
UE4Editor_Underwater_9207!UMapGeneratorComponent::execGenerateMap() [d:\projects\underwater\source\underwater\mapgeneratorcomponent.h:18]
UE4Editor_CoreUObject
UE4Editor_CoreUObject

The error complains that I am not using NewObject on something called EEarlyZPass::MAX_68678 (No idea what that is).

But here is the code instantiating the object where it says it crashes:

void UMapGeneratorComponent::CreateChunk(int x, int y)
{
	FString ChunkName = GetChunkName(x, y);
	UE_LOG(LogTemp, Warning, TEXT("Creating new chunk %s: [%i,%i] - %i"), *ChunkName, x, y, ChunkCreationIndex);
	UChunk* NewChunk = NewObject<UChunk>();
	Chunks.Insert(NewChunk, ChunkCreationIndex);
	ChunkIDs.Add(ChunkName, ChunkCreationIndex);
	ChunkCreationIndex++;
}

As you can see, I am using UObject.

Just in case, I made sure the constructor is empty:

UChunk::UChunk() {

}

So this constructor is not calling anything, the object is just being created and stored in a TArray, nothing else happens. Yet, I get this error.

Also might be a relevant note, my class was initially a normal C++ class, which I turned into a UObject as there was no way of storing it in a TMap, then it turned out TMap didn’t keep my object in memory so now I am storing it in a TArray.

I’m just trying to instantiate a class and store it in an array, but somehow this seems impossible in Unreal.

This problem also only started happening once I started trying to store the instances in a TMap/TArray. I could instantiate my class fine, but once I tried to store it it wouldn’t work until I made it a UObject, and now I can’t find a way that works.

Also for reference, the class definition:

#pragma once

#include "CoreMinimal.h"
#include "PerlinNoiseComponent.h"
#include "RuntimeMeshComponent.h"
#include "FMapParameters.h"
#include "MeshParameters.h"
#include "Chunk.generated.h"

UCLASS( ClassGroup=(CUSTOM) )
class UNDERWATER_API UChunk : public UObject 
{
	GENERATED_BODY()

public:
	UChunk();
	void Generate(int ChunkID, int ChunkX, int ChunkY, FMapParameters Parameters, UPerlinNoiseComponent* Noise, URuntimeMeshComponent* RuntimeMesh);
	void GenerateVertices();
	void GenerateTriangles();
	void GenerateMesh();
	void InitMeshToCreate();
	FVector2D GetWorldPositionForGridPosition(float x, float y);
	FVector2D CalculateUV(float x, float y);
	float GetZPositionFromPerlinResult(float PerlinResult);
	int GetNoiseIndexForCoordinates(int x, int y);
	FVector GetNoiseValueAt(int x, int y);
	void SetNoiseValueAt(int x, int y, FVector value);

private:
	bool Generated;
	int ChunkX;
	int ChunkY;
	FMapParameters Parameters;
	UPerlinNoiseComponent* Noise;
	URuntimeMeshComponent* RuntimeMesh;
	int ChunkID;
	MeshParameters MeshToCreate;
};

Well according to the stack CreateChunk is called on constructor which might not like NewObject being called on that phase. Try moving code to diffrent event as you should not run initiaion code on constructor, or else you setting defult values in arrays.

You saying that you want to use something else the UObject yet something that works with TMap, you could use struct with USTRUCT(). Structs are no different from classes in C++, even thru UE4 reflection system only allows properties in structs, you can still make functions in C++ without UFUNCTION which are not allowed in structus which many structs in UE4 does, as those function will work normally in C++,. If you want to do blueprint nodes for those you simply make static functions from blueprint library or some different UObject.

I ended up wrapping it in a struct yeah, but my problem wasn’t actually coming from that.

It turns out that my scene was corrupted and code wasn’t properly hot reloading (which explains the weird error message that didn’t quite match my code).

It’s likely that my code actually did work, I just didn’t know it wasn’t loading in Unreal.

In the meantime, I had found an alternative to avoid using a UObject:

I wrapped my class in a USTRUCT and made my container a TMap.

This way Unreal was able to store it.