目录
UObject::ConditionalBeginDestroy()
Engine\Config \BaseEngine.ini 更改下面参数,设置销毁时间间隔
AddToRoot 和 RemoveFromRoot 标记不被GC/移除标记
Garbage Collection Overview | Unreal Engine Community Wiki (unrealcommunity.wiki)
UObject实例创建 | 虚幻引擎5.0文档 (unrealengine.com)
UE4 UPROPERTY与反射、垃圾回收 - DullSword's Blog
虚幻Object处理 | 虚幻引擎文档 (unrealengine.com)
UE4中的C++编程简介 | 虚幻引擎文档 (unrealengine.com)
"NewObject()"是最简单的UObject工厂方法。它接受一个可选的外部对象和类,并用自动生成的名称创建一个新实例。
- template< class T >
- T* NewObject
- (
- UObject* Outer=(UObject*)GetTransientPackage(),
- UClass* Class=T::StaticClass()
- )
指向指定类的生成实例指针。
- UCLASS()
- class TIPS_API UItemObject : public UObject
- {
- GENERATED_BODY()
- FString m_Name;
- public:
- UItemObject() {
- m_Name = GetName();
- UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" %s"), *m_Name);
- }
-
- ~UItemObject() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" %s"), *m_Name); }
- };
- UItemObject* Obj = NewObject
(); - UItemObject* Obj2 = NewObject
(this, TEXT("Obj2"));
UObject及其派生 具有被 UE4 垃圾回收机制管理,因而当指向对象的指针为 nullptr 后,将会被 UE4 自动回收掉
- Obj = NewObject
(this, TEXT("Obj")); - Obj = nullptr;
- Obj->ConditionalBeginDestroy();
- Obj = nullptr;
- Obj->MarkPendingKill();
- Obj = nullptr;
gc.TimeBetweenPurgingPendingKillObjects=60
UWorld::ForceGarbageCollection 弃用
GEngine->ForceGarbageCollection
GEngine->ForceGarbageCollection(true);
支持容器 TArray、TMap 的
- UPROPERTY()
- class UItemObject* m_ItemObject2;
-
- UPROPERTY()
- TArray<class UItemObject*> m_ObjList1;
构造时 和 AddToRoot 一起
析构时和 RemveFromRoot 一起
- //创建对象
- m_ItemObject1 = NewObject
(this,TEXT("m_ItemObject1")); - m_ItemObject1->AddToRoot();
-
- // 释放对象
- m_ItemObject1->RemoveFromRoot();
- m_ItemObject1 = nullptr;
Destroy() 方法DestroyComponent() 方法