
Managed memory – deallocating memory
UObject
s are reference-counted and garbage-collected when there are no more references to the UObject
instance. Memory allocated on a UObject
class derivative using ConstructObject<>
or NewObject< >
can also be deallocated manually (before the reference count drops to 0) by calling the UObject::ConditionalBeginDestroy()
member function.
Getting ready
You'd only do this if you were sure you no longer wanted UObject
or the UObject
class derivative instance in memory. Use the ConditionalBeginDestroy()
function to release memory.
How to do it...
The following code demonstrates the deallocation of a UObject class
:
UObject *o = NewObject< UObject >( ... ); o->ConditionalBeginDestroy();
How it works…
The command ConditionalBeginDestroy()
begins the deallocation process, calling the BeginDestroy()
and FinishDestroy()
overrideable functions.
There's more…
Be careful not to call UObject::ConditionalBeginDestroy()
on any object still being referenced in memory by other objects' pointers.