I was able to run this from inside my UObject’s Constructor successfully.
// .h file
UCLASS(BlueprintType)
class TASGAME_API UMainContainerBase : public UObject
{
GENERATED_BODY()
public:
UMainContainerBase();
~UMainContainerBase();
BYTE* someArray = nullptr;
....
// .cpp file
UMainContainerBase::UMainContainerBase()
{
UE_LOG(LogTemp, Warning, TEXT("%p ctor start"), this);
UE_LOG(LogTemp, Warning, TEXT("%p Am I the CDO?: %d"), this, HasAnyFlags(EObjectFlags::RF_ClassDefaultObject));
{
someArray = new BYTE[100]{0};
someArray[0] = 3;
UE_LOG(LogTemp, Warning, TEXT("%p someArray[0]=%d"), this, someArray[0]);
}
UE_LOG(LogTemp, Warning, TEXT("%p ctor End"), this);
}
UMainContainerBase::~UMainContainerBase()
{
UE_LOG(LogTemp, Warning, TEXT("%p Am I the CDO?: %d"), this, HasAnyFlags(EObjectFlags::RF_ClassDefaultObject));
if (someArray)
{
UE_LOG(LogTemp, Warning, TEXT("%p dtor someArray[0]=%d"), this, someArray[0]);
delete someArray;
someArray = nullptr;
}
else
{
UE_LOG(LogTemp, Warning, TEXT("%p dtor someArray is null?"), this);
}
}
Here are my logs
// Started compiling
Warning: Starting HotReload took 0.0s.
...
LogTemp: Warning: 000001CEB92E3470 ctor start
LogTemp: Warning: 000001CEB92E3470 Am I the CDO? 1
LogTemp: Warning: 000001CEB92E3470 someArray[0]=3
LogTemp: Warning: 000001CEB92E3470 ctor End
...
LogTemp: Warning: 000001CEB92E0590 Am I the CDO? 1
LogTemp: Warning: 000001CEB92E0590 dtor someArray is null?
LogTemp: Warning: 000001CEC3FC65C0 Am I the CDO? 1
LogTemp: Warning: 000001CEC3FC65C0 dtor someArray[0]=3
...
// Finished compiling
Display: HotReload took 2.7s.
// I hit play
LogTemp: Warning: 000001CED7757BF0 ctor start
LogTemp: Warning: 000001CED7757BF0 Am I the CDO? 0
LogTemp: Warning: 000001CED7757BF0 someArray[0]=3
LogTemp: Warning: 000001CED7757BF0 ctor End
...
LogTemp: Warning: 000001CED7755120 ctor start
LogTemp: Warning: 000001CED7755120 Am I the CDO? 0
LogTemp: Warning: 000001CED7755120 someArray[0]=3
LogTemp: Warning: 000001CED7755120 ctor End
...
// I exited my game
LogTemp: Warning: 000001CED7755120 Am I the CDO? 0
LogTemp: Warning: 000001CED7755120 dtor someArray[0]=3
LogTemp: Warning: 000001CED7757BF0 Am I the CDO? 0
LogTemp: Warning: 000001CED7757BF0 dtor someArray[0]=3
...
Note: that I have some DTORs with addresses that there with no equivalent CTORs for, they are from my previous ‘compile’ when the earlier CDOs were made. For example, if I recompile my project, I see the dtor for 000001CEB92E3470.
Note: I tried using NULL and nullptr (no difference for me as expected)
Note: I tried using delete and delete] (no difference for me)
In my above example, I experienced no crashes or issues.
How familiar are you with the concept of CDO (Class Default Object) and UE4 UCLASS lifetime of an object?