Exception when calling method, Object is valid

I am currently writing a Replication Graph for my Master’s thesis.

During the GatherActorListsForConnection calls, it crashes when calling the method for my custom ReplicationGraphNode (UReplicationGraphNode_Room).

Code:

for (auto RoomMapEntry : Rooms) {
	if (RoomMapEntry.Key == nullptr) {
		UE_LOG(LogTemp, Error, TEXT("Room is null"));
		continue;
	}
	if (InsideRoom(RoomMapEntry.Key->GetRoomInfo(), Params.Viewer.ViewLocation)) {
		if (IsValid(RoomMapEntry.Key) && IsValid(RoomMapEntry.Key->GetDynamicNode())
		&& &RoomMapEntry.Key != nullptr) {
			//Call fails, RoomMapEntry.Key-> is 0x0
			RoomMapEntry.Key->GatherActorListsForConnection(Params);
		}
	}
}

Rooms is a TMap: TMap<UReplicationGraphNode_Room*, TArray<UReplicationGraphNode_Room*>> Rooms;

The function call fails with the message RoomMapEntry.Key-> was 0x0 (or 0xFFFFFCF), even though I test for objects that could be null.
I create the Key entries as follows:

UReplicationGraphNode_Room* NewRoom = NewObject<UReplicationGraphNode_Room>();
		NewRoom->SetRoomInfo(FRoomInfo(Tuple.Key, Tuple.Value));

I don’t know what to search for, I cannot debug any further, the GatherActorListsForConnection function is never reached.
I also specifically implemented the function, turned off pragma optimization and debugged with the DebugGame-Editor, still the same error.
Why am I getting this exception even though I test for the object being null?

it’s indeed strange it passing 0x0 only way to explain it is that key get nulled after the check, but if you get 0xFFFFFCF that means it’s invalid pointer, and it will pass null check as it’s different value then null which in most cases is value 0. If thats the case your code will crash at RoomMapEntry.Key->GetRoomInfo() in if condition check right after.

Also in you use IsValid there no point of making null pointer check as IsValid does null check already, also by doing&RoomMapEntry.Key you creating pointer of a statically declared pointer variable which never will be null. So your code can be reduced to this:

 for (auto RoomMapEntry : Rooms) {
     if (IsValid(RoomMapEntry.Key)) {
         UE_LOG(LogTemp, Error, TEXT("Room is null or invalid"));
         continue;
     }
     if (InsideRoom(RoomMapEntry.Key->GetRoomInfo(), Params.Viewer.ViewLocation)) {
         if (IsValid(RoomMapEntry.Key->GetDynamicNode())) {
             RoomMapEntry.Key->GatherActorListsForConnection(Params);
         }
     }
 }

Now reason why you may have invalid pointers is usally UE4 not seeing the object reference in property or engine not seeing property at all and GC deletes the object as it see it as trash. It’s either missing UPROPERTY() on TMap or container you using don’t raport object reference (Which if i remember right it might be the case with TMap). Or you manually deleted it and if TMap indeed don’t cooperate with GC it will have invalid pointer

I’m also not sure about TArray in TMap as for example TArray in TArray is not supported.

Also make sure to check the logs as they might tell you more about crash, check Saved/Logs in your project directory.

SOLVED:
I now fixed the issue by changing the Map to a TArray of a struct containing the information and declaring that as a UPROPERTY().

Apparently nested containers (TArray in TMap) cannot be done (Error only appeared after declaring it a UPROPERTY) and maybe declaring it a UPROPERTY() got rid of memory/garbage collection issues…

I think you are right with the Map needing the UPROPERTY() macro and the nested containers.

I created a Top level answer describing how I fixed it.