- Unreal Engine 4 Scripting with C++ Cookbook
- William Sherif Stephen Whittle
- 336字
- 2021-07-08 10:50:48
Destroying UObject-derived classes
Removing any UObject
derivative is simple in UE4. When you are ready to delete your UObject
-derived class, we will simply call a single function (ConditionalBeginDestroy()
) on it to begin teardown. We do not use the native C++ delete
command on UObject
derivatives. We show this in the following recipe.
Getting ready
You need to call ConditionalBeginDestroy()
on any unused UObject
-derived classes so that they get removed from memory. Do not call delete
on a UObject
-derived class to recoup the system memory. You must use the internal engine-provided memory management functions instead. The way to do this is shown next.
How to do it...
- Call
objectInstance->ConditionalBeginDestroy()
on your object instance. - Null all your references to
objectInstance
in your client code, and do not useobjectInstance
again afterConditionalBeginDestroy()
has been called on it.
How it works…
The ConditionalBeginDestroy()
function begins the destruction process by removing all internal engine linkages to it. This marks the object for destruction as far as the engine is concerned. The object is then destroyed some time later by destroying its internal properties, followed by actual destruction of the object.
After ConditionalBeginDestroy()
has been called on an object, your (client) code must consider the object to be destroyed, and must no longer use it.
Actual memory recovery happens some time later than when ConditionalBeginDestroy()
has been called on an object. There is a garbage collection routine that finishes clearing the memory of objects that are no longer referenced by the game program at fixed time intervals. The time interval between garbage collector calls is listed in C:\Program Files (x86)\Epic Games\4.11\Engine\Config \BaseEngine.ini
, and defaults to one collection every 60 seconds:
gc.TimeBetweenPurgingPendingKillObjects=60
Tip
If memory seems low after several ConditionalBeginDestroy()
calls, you can trigger memory cleanup by calling GetWorld()->ForceGarbageCollection(true)
to force an internal memory cleanup.
Usually, you do not need to worry about garbage collection or the interval unless you urgently need memory cleared. Do not call garbage collection routines too often, as this may cause unnecessary lag in the game.
- 摩登創客:與智能手機和平板電腦共舞
- 趣學Python算法100例
- Android 7編程入門經典:使用Android Studio 2(第4版)
- 深度學習:算法入門與Keras編程實踐
- Visual C++應用開發
- 軟件架構:Python語言實現
- Java EE 7 Performance Tuning and Optimization
- Mastering Unity 2D Game Development(Second Edition)
- 第一行代碼 C語言(視頻講解版)
- UNIX Linux程序設計教程
- LabVIEW虛擬儀器入門與測控應用100例
- 持續集成與持續交付實戰:用Jenkins、Travis CI和CircleCI構建和發布大規模高質量軟件
- Spring MVC+MyBatis開發從入門到項目實踐(超值版)
- Mastering ASP.NET Core 2.0
- 創新工場講AI課:從知識到實踐