Bug in TUnion copy constructor

Adding the following C++ code anywhere (e.g: inside a AMyActor::Tick() function) will crash the editor:

TUnion<int, float> default_constructed;
TUnion<int, float> copy_constructed(default_constructed);

What I think is going on: the default constructor sets the value of the TUnion’s private member CurrentSubtypeIndex to -1; this value is later used in a switch case inside the copy constructor. However CurrentSubtypeIndex is unsigned, of type uint8, and due to integer promotion the switch case compares int32(-1), ie 0xFFFFFFFF with uint8(-1), ie 0xFF. The comparison fails and the default case triggers, which is a UE_LOG() with a Fatal log level, thus crashing the editor.

Hello .cat,

Is there anything else you’re including in your project when doing this? When attempting to compile a project with the two lines you provided, it will fail with a link error if the second line is present. To give you the exact error in case it may help:

MyActor.cpp.obj : error LNK2001: unresolved external symbol "struct FLogCategoryLogUnion LogUnion" (?LogUnion@@3UFLogCategoryLogUnion@@A)
2>C:\Users\.clark\Documents\Unreal Projects\4.10\MyProject2\Binaries\Win64\UE4Editor-MyProject2-1477.dll : fatal error LNK1120: 1 unresolved externals

Right, sorry, that is due to another bug, still open, that I forgot to mention:

You need to add
DEFINE_LOG_CATEGORY(LogUnion);
somewhere in your cpp file

Thank you for the report and for that clarification. I’m seeing the same issue so I’ve placed a bug report in for the issue. For reference, the number is UE-28427. I’ll be sure to let you know if any changes are made and keep you up to date.

Have a nice day!