What’s the problem
The Garbage Collection seems not work as I wished in EditorSubsystem.When I set one of the UObject pointer property in my EditorSubsystem to nullptr,it does not get garbage collected(or at least not trigger the destructor) even I wait long enough.The destructor get called when I enter other map or using hot compile.
How to reproduce the problem
Just create a new project in UE5.2.1,and create a custom EditorSubsystem and UObject as below.
UCLASS(BlueprintType)
class TESTPROJECT_API UTestEditorSubsystem : public UEditorSubsystem
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
void CreateTestObject();
UFUNCTION(BlueprintCallable)
void DestoryTestObject();
protected:
UPROPERTY()
UTestGCObject* TestGCObject;
};
void UTestEditorSubsystem::CreateTestObject()
{
TestGCObject = NewObject<UTestGCObject>();
}
void UTestEditorSubsystem::DestoryTestObject()
{
TestGCObject = nullptr;
}
UCLASS()
class TESTPROJECT_API UTestGCObject : public UObject
{
GENERATED_BODY()
virtual ~UTestGCObject() override;
};
UTestGCObject::~UTestGCObject()
{
UE_LOG(LogTemp, Error, TEXT("Destroy Successfully!"))
}
And create a EditorUtilityWidget to call CreateTestObject and DestoryTestObject(such as bind them to a button),the UTestGCObject’s destructor just not get called until open other map or recompile or close unreal editor.