- this post covers
- what is unique pointer in unreal engine
- how it is used
- restriction and caution on usage
- Unreal Engine (4.24)
- Visual Studio 2017
- Windows 10
as unreal engine handles objects inherits
UObject, we can use unreal engine easily. there are several benefits when the object inherits
- garbage collection
- reference update
then you might have one question,
“how we can handle the objects does not inherit
UObject ? should we use raw pointer for the objects ?”
well…there are 2 ways for this.
std::unique_ptrof cpp std library in
TUniquePtrof unreal API in
you can use
std::unique_ptr in unreal project, but unreal engine implements their own smart pointer library. and it is common that using
TUniquePtr in unreal project unless you do not need cpp std library.
as purpose and functionality are the same,
TUniquePtr is similar to
TUniquePtr also provides the unique ownership and other features. let us check out what it is and how it is used.
you can find some example in unreal engine code.
FSandboxPlatformFile is not a class inherits
UObject and it is possible to be indicated with
( conventionally, prefix
U is attached when the class inherits
UCookOnTheFlyServer is a class inhertis
UObject and it contains
FSandboxPlatformFile as member variable.
accessing the object through
TUniquePtr is the same on
-> operator, you can access the object as the normal pointer.
TUniquePtr is the same on raw pointer. if the value is zero, the
TUniquePtr is pointing
nullptr. you can use
check for ensuring whether the
TUniquePtr is valid.
FSandboxPlatformFile* as one of parameters. the type of parameter is not the
TUniquePtr, so we should convert
TUniquePtr::Get() if you need the raw pointer of
TUniquePtr, you can let
TUniquePtr release the object and have its value as
FSandboxPlatformFile takes one parameter as boolean.
TUniquePtr object and calls the constructor of the template class. in this case,
MakeUnique<FSandboxPlatformFile> takes one boolean value.
suppose you have a class like below
you should only use
TUniquePtr at the object, which exists only one thing. unless, you would get an exception for delete on
in this code,
CreateUserInformation will be called in
ReleaseUserInformation will be called in
UserInformation is a memeber variable with
TUniquePtr<UserInfo> type. another
TUniquePtr<UserInfo> exists in
CreateUserInformation, which gets
what happens when
CreateUserInformation is ended ?
AnotherPtr would disappear and its destructor would be called. when the destructor of
TUniquePtr is called, it releases memory that
TUniquePtr has pointed. as a result, variable
UserInformation would be a dangling pointer.
UserInformation has abnormal values.
because already the memory is released, an exception would be thrown when we execute
ReleaseUserInformation. it is why you have to use
TUniquePtr at the object only existing one thing and care about moving the ownership. moving the ownership with raw pointer is dangerous as we have seen.
let us move the ownership with
MoveTemp API. this code makes
AnotherPtr point the memory and
UserInformation set the
nullptr. so we can avoid the exception.
TUniquePtr is similar to
std::unique_ptr and its usage and restriction, too.
- initialization: you can initialize
TUniquePtrwith 2 ways
|using raw pointer||
- transfering ownership: for preventing side effect, you should use
TUniquePtr<T> PointerA = new T(...);
- release: there are various ways to release the
// way #1 : release implicitly